From my experience with writing/reading unit tests for years, here’s a little guideline to keep ’em readable:
- A unit test needs to fit entirely (including setups) on a screen without scrolling.
- It is OK to have a little bit of duplication in unit tests for readability.
- Avoid nesting of contexts beyond 2 levels. Instead, use methods to setup and flatten.
- Do not use if/else/loops in a unit test.
- If your test needs too much setup/mocking/stubbing, time to refactor the code.
A simple readable unit test is only achievable when the code itself is simple. Adhereing to these guideline will probably make the code simpler as a direct impact!