Write the Assertion First
April 24, 2018
When writing automated tests (whether in a TDD fashion or otherwise) I find it helpful to write the assertion first, before any setup code. There are two main reasons I think it is helpful.
One, I think it's useful to begin with the end in mind. Writing the assertion first helps me to focus on what piece of the feature I'm testing.
Two, it allows me to think my way through a complicated test setup. Complicated setups involving multiple objects, mocks, etc. are a test smell. However, sometimes they cannot be avoided. When working with untested legacy code, it is sometimes necessary to begin with writing coarse-grained tests that involve multiple objects. Writing the test assertion first allows me to backtrack through the various dependency points which then enables me to start thinking about which dependencies can be broken out.