Tests should be as cohesive, independent, and isolated as possible. Ideally, a single test method should test a single functionality or behavior of the system. Fat tests (or, as the test smells community calls them, eager tests) exercise multiple functionalities and are often complex in terms of implementation. Complex test code reduces our ability to understand what is being tested at a glance and makes future maintenance more difficult. If you face such a test, break it into multiple smaller tests. Simpler and shorter tests are better.
Moreover, tests should not depend on other tests to succeed. The test result should be the same whether the test is executed in isolation or together with the rest of the test suite. It is not uncommon to see cases where test B only works if test A is executed first. This is often the case when test B relies on the work of test A to set up the environment for it. Such tests become highly unreliable.
If you have a test that is somewhat dependent on another test, refactor the test suite so each test is responsible for setting up the whole environment it needs. Another tip that helps make tests independent is to make sure your tests clean up their messes: for example, by deleting any files they created on the disk and cleaning up values they inserted into a database. This will force tests to set up things themselves and not rely on data that was already there.
Leave a Reply