What are we Testing Again?

This is a general case around Test Data in the Test Smells.

Let’s have a look at a test snippet:

@Test
void dayPercentile() {
    assertThat(DayPercentileCalculator.calculate(EXAMPLE_1))
        .isEqualTo(EXAMPLE1_EXPECTED);
}

What does this test tell us?

Here’s what I think it tells me:

  • There is a DayPercentileCalculator
  • It has a calculate method
  • We’re looking at the output of that method
  • We’re asserting on equality

Structurally, these is a perfectly clean test. It’s missing one vital ingredient though…

What The Hell Is The Use Case!?

The difficulty here is that you can’t see what the code under test is supposed to do from the test itself.

Comparatively, from this one assertion:

assertThat(StringWrangler.wrangle("hello world"))
   .isEqualTo("hElLo wOrLd");

You can probably guess that the StringWrangler, whatever that is, alternately capitalizes letters in a string.

Don’t Make The Test Data Obscure

This is where we have conflicting priorities writing code for tests. On the one hand, we feel like we should extract constants to represent magic values. On the other hand, by doing so, the assertions/execution code in our tests starts to look particularly cryptic.

If the original example were written:

@Test
void dayPercentile() {
    assertThat(DayPercentileCalculator.calculate(new Time(12, 0, 0))
        .isEqualTo("50%");
}

Ignore the made up Time type here.

We can understand with a minimum of guesswork how this code must surely operate.

Summary

Tests are an important piece of documentation about your code. Test data should be simple, concrete and visible.

Hiding data behind a badly named constant won’t do.

It’s hard to name a constant well!

As data gets more complex, or reused, it’s necessary to consider methods like test data factories or constants, or even data files. However, the principle to focus on is how well the test explains the use case being tested.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s