Start with "Given, When, Then" tests
Whether or not you’re practicing test-driven development, it’s helpful to begin writing tests with the “Given, When, Then” structure.
// Given we have a queued order; // When we cancel it; // Then the order should be cancelled.
Getting the structure in with comments first has several benefits.
Firstly, it lowers the mental effort to required to start. You only need to think about the desired behaviour in the simplest terms. What situation is this relevant to; what happens; what should the result be? Once you’ve got that written out it’s often easier to go on to the next steps.
Secondly, it ensures the test is clear. If you start by implementing the test logic, it’s tempting to just get it working and then leave it at that. Later, the purpose of the test is less clear – is this part describing an essential behaviour, or is it just some side logic for set up? Tests are a kind of documentation, so clarity is valuable.
Finally, if you are writing this test before implementing the code (which is ideal), it encourages a better design. “Given, When, Then” provides a good approach to designing a sensible interface by focusing on what it does, and not how it does it.