How to name a unit test?
If we want to write down our unit tests, we may need to have a good layout and plan how to create the class and unit test method for unit tests, the following is a brief introduction.
1: Separate integration test and unit test
If we put the integration test and unit test together, if there are many possible causes of problems, we will directly separate them and create two sets of class libraries. For example
2: map the test class to the test code.
2.1: Each tested class corresponds to a test class.
In the test project, use the name of the tested class and the suffix UnitTests. If the tested class is Login, the test class created in the test project is LoginUnitTests. This is a test class for each class.
2.2: each function corresponds to a test class
It is to create a test class for each method, but because there are many methods, there will be a large number of test classes, so we need to weigh it, compared to some functions in a project that require a lot of test cases, you need to add a test class for this separate function. Compared to the GetAuthory method, there are many test cases, so we need to put the test case in a class separately. At this time, we need to create two test classes: AuthoryUnitTests_GetAuthory and AuthoryUnitTests. The former includes GetAuthory and the latter includes all other tests.
3: How to name a method
In unit testing, we should try to write less comments so that we do not write them. Therefore, we need to write a high-quality test name, so we can takePrepare objects-operation objects-Assertions.
Preparation object: it is the object you want to test for him. To put it bluntly, it is your method name, such as AddUser and DeleteUser.
Operation object: What kind of operation will you perform on this object, such as a valid user name or invalid user name?
Assertion: it is to make a judgment on the result. This operation will throw an exception, this operation will be normal, this operation will fail, this value will change, etc.
The following is an example.
AddUser_ValidUserInfo_ReturnsTrue
Returns True if a valid user is added.
AddUser_IdIsNullOrEmpty_Throws
When a user is added, the Id is Null or Empty, and an exception is thrown back.
Some calls do not return values, but the status changes can be seen.
Add_WhenCalled_StateChange (Add is an example)
That is, when the Add method is called, The system status changes.
The above is only a suggestion.