Simply put: one unit test should cover a separate function of your program. That is all that can be said. That's why they are called unit tests.
Of course, what we understand by attribute may differ. Think about the smallest parts of your program that might break or not work properly. Think about the business requirements of your code. These are the parts that you want each of them to be covered with a dedicated unit test.
Unit tests are usually small, isolated, and atomic. They must be understandable , they must refuse / pass independently of each other , and perform quickly . A pretty good indication of the right unit tests is one statement - if you find that you are writing more, you are probably testing too much, and this is a sign that you need more than one test for this function. However, this is not a strict rule - the more complex the code, the more complex unit tests.
When writing tests, it’s easy to break down the functionality of the code and check those individual parts (this should give you an idea of the atomicity of your tests). For example, if you have a method that checks the input, it calls the service and finally returns the result, you usually want all three (check, call, return) to be covered.
source share