5 (Five) unit tests. That’s how much unit tests each method deserves!
Here are your options:
- 0 unit tests: obviously not the best choice. How do you know your code even works?
- 1 unit test: you are probably looking to increase your code coverage and don’t care that much about testing. Either your team or your manager made you write that unit test. Or maybe you just want to feel good about what your are doing and brag about how you are unit testing.
- 2 unit tests: they ensure basic coverage as any method should have at least a positive and a negative test.
- 3 – 4 unit tests: you are taking your code seriously and you may feel both proud and confident, but you shouldn’t stop here…
- 5 unit tests: always think about edge cases, null variables, exceptions thrown by APIs you consider solid and unbreakable. Have a healthy dose of paranoia and spend time refining your code, and testing those cases your users are sure to find.
Perhaps these numbers are a bit too precise. Of course each method is different, and each developer/team has his/its own goals and know how much they must/should test.
However, always ask yourself “Are there any other tests I can write?” And if there are, go ahead and write them!
The most important thing is to feel confident about your test. They are the ones allowing you to write code faster, eliminate bugs before the code reaches production and in the end build better apps.
PS: If you find yourself writing more than 5 unit tests you should consider refactoring the method you are testing and splitting it in two or more methods. The tests will become simpler, easier to read, and will cover less functionality, making them easier to maintain.