Do your unit tests easily reveal their intention? Can I look at the test method and easily reason why the assert should be correct?
Sometimes, in our quest to remove duplicate code and to use test fixtures and data builders we obscure the data in our test objects. If I need to navigate through builders and set-up methods to understand how the test works - I’ve reduced the test’s value.
Intentional tests combine the set-up, test execution and assertion in a logical manner so I can immediately understand what is being tested and why the test should pass.
In this first example I test a Story object’s ability to return all the hobbit characters. I use a test fixture for a list of characters. But, why are these these the hobbits we are looking for? We could make this test more intentional.
This example simply creates the list in the test method. This helps me reason about the test and easily understand why the expected result is correct. I still use some test fixtures by way of the character instances. But because these are well known characters I can leave out some of the details.
The final example shows that if we all understand who is in the group of characters I can lean on the test fixture a bit more. This is intentional because everyone on the team knows who is in the Fellowship of the Ring.
Intentional testing was explained to me by Korey E.
Thanks to AssertJ for their awesome assertions.