Sometimes the task of writing unit tests is delegated to the new team member or the Jr. developer, in order to help them get to know the code base. That's at least the most convincing excuse I've heard
That being said, if the devs writing these tests don't have prior experience doing so, they can run into some classic mistakes. These are my top 3:
Testing more than one thing at the time. Having big test cases checking for several things at the same time can be a problem.
Relying on external resources. How many times have we seen a "testing database" being created and destroyed during the tests execution?
Finally, aiming for high coverage alone, without context. Coverage by the sake of coverage isn't really helpful, there are better metrics to try to improve and use to measure the success (or lack of thereof) of your tests.
Those are my top 3 mistakes, I've written more about them over here.
What about you? What are the top mistakes you've seen while reviewing (or writing your own) unit tests?
Top comments (4)
Correct me if I am wrong, but I think if the test is spinning up databases and making real writes and reads it would have been integration tests, not unit tests.
I would also add "mocking critical inputs" into the list. That prevents the tests from having any values, since we're asserting what we ourselves mock in the first place.
That is completely correct, that would be an integration test, not a unit test.
As for the mocking critical inputs, could you provide an example? I don't think I'm following.
Another one I sometimes see is the testing of the underlying framework or tool chain in unit tests.
Absolutely! Taking it too far and ending up testing tools that should've been tested by others is a classic one! Thanks for sharing!