Unit Testing Anti-Pattern: Code Matching

This series goes through anti-patterns when writing tests. Yes, there are and will be many. 
TDD without refactoringLogic in testsMisleading testsNot asserting
Code matchingData transformationAsserting on not nullPrefixing test names with "test"

Sometimes, tests imitate code, and not in a good way.

Here’s our tested code:

And here’s our test:

Hard not to notice the similarity, right?

I’ve seen this anti-pattern many times, and it usually comes down to laziness. It may not seem such a problem, but it is.

First of all, we’re not testing anything here. If we’re checking anything at all, it is whether the compiler or the processor works correctly. This is not our job. Usually.

It does go deeper, though.

If the topic wasn’t test design, we’d talk about separating interface from implementation. Similarly, in tests we check the effect of the operation, regardless of its the implementation. One of the advantages we get from the separation, is that we can refactor code, without breaking the test, which means in the eyes of the observer, behavior has not changed.

However, the way our example test is written doesn’t imply just behavior, it implies intent of implementation. When we read the test, we already know what to expect in terms of result, and how the operation is performed.

As with most problematic tests, the problem is not about when we write the tests, and they pass. Let’s jump ahead, say six months.

Somebody changes the implementation and breaks the test. Now they needs resolve the problem, based on how “it’s supposed to work”. After all, the intent was already documented in the test by the original writer. What was she thinking? Are we in the right? Is there something we’re missing?

Tests are already a snapshot of behavior, locking code down. Let’s try to limit the lock down effect as much as we can.


One thought on “Unit Testing Anti-Pattern: Code Matching

  1. My reaction to that test depends heavily on how the “requirement” was written and one if the test is really a “unit test” or one of the many other forms of tests [each of which have different value propositions].

Leave a Reply

Your email address will not be published. Required fields are marked *