Effective Testing - Test Structure
One way to make sure your tests are readable is to have them all adhere to the same structure.
By far, the most common structure is “Given - When - Then” (aka “Arrange, Act, Assert”). It goes like this:
- On Given: We create the objects and set up the needed state.
- On When: We perform the action we want to test.
- On Then: We validate the state changed as expected.
The comments explaining each section are optional, and can be omitted on trivial scenarios like the one shown here.
Note how we use whitespace to clearly separate each section. Anybody familiar with the structure will be able to easily identify each section at a glance.
Some testing libraries like Kotest support a style that already includes the Given, When and Then keywords, making the structure explicit.
This post is part of the Effective Testing Series.