A List of Best Practices for Writing Tests
While writing tests, these guidelines will serve as the best practices to follow, which can lead to better-quality tests.
Join the DZone community and get the full member experience.
Join For FreeBelow is a list of best practices to follow when writing tests. These are general guidelines one should follow irrespective of the type of tests they are working on.
Keep Tests Independent
Each test should be independent of others, ensuring that a failure in one test doesn't impact the execution or outcome of another. This improves test reliability, enables test parallelization, and allows for easier debugging.
Don't share the state between tests. Set up the state before the execution of the test and tear it down after it executes. This will keep tests isolated.
Write Tests Early
Start writing tests as early as possible in the development process. Early tests help identify issues sooner and provide feedback early in the development lifecycle. As we move towards the later stages of the development lifecycle, the cost of fixing the issue increases. For example, finding and fixing an issue during local development is easy and cheap, but if the same issue is moved to production, then it wouldn't be easy to find the issue, and it may potentially cost the organization to lose money, users, or trust.
Test Behavior, Not Implementation
Test application through the public behavior exposed by the application. For example, for a Web API, if tests are invoking Controller's method, then these tests are testing the implementation, not the behavior, because this is not the public behavior exposed by the Web API. The public behavior is HTTP API, and tests should be written at that level and not any layer below it. This keeps your tests decoupled from implementation, and this gives you the ability to change the implementation however you like as long as your public behavior is consistent.
Manage Complex State Through Builders
Often to test a scenario, we need first to build a state. And this sometimes requires quite a bit amount of code to set up. Manage and simplify this state created through the use of Design Patterns such as Builders.
Write Focused Tests
A test should verify only one thing. It should not verify multiple statements one after the other. Keeping tests small and focused will help in maintaining the tests as well as when it fails, you will know exactly what failed. But if a test contains multiple verification steps, then you would have to log in to verify what exactly has failed in the test.
Use Multiple Levels
Have a test suite with multiple levels. For example, unit tests, integration tests, and end-to-end tests. And then, when you want to write a test, think at which level it could be tested better and then write the test at that level. For example, testing a public library method could be done in unit tests, whereas a database interaction could be done in integration tests.
Opinions expressed by DZone contributors are their own.
Comments