How to name a unit test?

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.