DEV Community

Fernando Doglio
Fernando Doglio

Posted on

Top 3 mistakes you're probably doing while unit testing

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:

  1. Testing more than one thing at the time. Having big test cases checking for several things at the same time can be a problem.

  2. Relying on external resources. How many times have we seen a "testing database" being created and destroyed during the tests execution?

  3. 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)

Collapse
 
afifsohaili profile image
Afif Sohaili • Edited

Relying on external resources. How many times have we seen a "testing database" being created and destroyed during the tests execution?

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.

Collapse
 
deleteman123 profile image
Fernando Doglio

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.

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.

Collapse
 
heindauven profile image
Hein Dauven

Another one I sometimes see is the testing of the underlying framework or tool chain in unit tests.

Collapse
 
deleteman123 profile image
Fernando Doglio

Absolutely! Taking it too far and ending up testing tools that should've been tested by others is a classic one! Thanks for sharing!