Use Unique Values

The readability and usefulness of tests are enhanced when the values used to construct fixtures are mutually distinct. Within a test, unique values reinforce the differences between various assertions and help highlight when the implementation has miswired parameters or properties. Between iterations of a data-driven test, distinct values help to highlight the failing test case more clearly. The same effect applies between tests, in which case unique values also help to identify the effects of shared state.

Assertion failures generally provide good feedback on the values involved and the location in the code. However, some assertions, like assertTrue, do not display their contributing expression values. Assertions in loops will share a location for multiple data values. Custom assertions and assertions in fixtures or utility methods will show deeper stack traces than you would normally expect. In each of these cases, unique values help to localize the failure more quickly.

Data-driven tests magnify the shared location property of assertions in loops. The entire test is executed repeatedly rather than just the few lines in the loop. Unique values within a row help to distinguish the deviant property. Unique values within a column isolate the test cases regardless of which assertion fails.

Although best practice guides us away from tests that interact with each other, various shared resources ranging from static variables to singletons to files or databases can cause crossover, especially when tests are run in parallel. Unique values allow us to more easily notice when a value shows up in the wrong test or context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.180