Coding Practice: What do I do when my unit tests take more than 6 seconds to run?
Unit tests should take less than 1 second to run per test. When recently looking at a leading open source eCommerce project, I took note of how long the unit tests took to run and sure enough the 300 pure unit tests took about 8 seconds to run (0.02 seconds per test). There were another 200 unit tests that took about 4 minutes to run (1.20 seconds per test), and these tests were making round trips to the database. These longer tests (1.20 seconds) don’t seem long on the surface, but the time adds up and could eventually lead to the developer skipping tests during the development process, which defeats the value of unit tests. Indeed, if I were to make a contribution to that open source project, I would break out their long running unit tests into a special folder called “Integration Tests”, and only execute those when checking end to end features.Keep in mind that unit tests should help you localize problems at the most granular level.
Quote:
“A test is not a unit test if:
– It talks to the database
– It communicates across the network
– It touches the file system
– It can’t run at the same time as any of your other unit tests
– You have to do special things to your environment (such as editing config files) to run it.”
– Michael Feathers
Application:
If your unit tests takes more than 1 second to run, you probably have not identified the smallest testable part while replacing dependencies with mocks.If you find that you have to do too much setup of the test, then that indicates that your code should be refactored to have fewer dependencies.
If your unit tests encompasses multiple objects and their interaction, then you probably are dealing with an integration test. Integration tests are important, but they should be kept separate from unit tests and run with less frequency.
References:
Working Effectively with Legacy Code
Growing Object-Oriented Software Guided By Tests