Nunit and testdriven. Net unit test learning

Source: Internet
Author: User

In cooperative development, you must ensure that the code you write passes unit tests when it is handed in, and you are responsible for the code you write. The following describes the unit test tools nunit under. Net and the unit test plug-in testdriven. Net of Visual Studio.

After nunit is installed, We can reference nunitframework in vs reference (This component is not available if it is not installed ). The use of nunit is not complex. Here we will briefly introduce several attributes.

<Testfixture> indicates a test case in the test class.

<Test> MARK Test Cases

The first two attributes are essential for nuit, and the last few attributes have the functions of code optimization and test optimization.

<Setup> the process defined under this attribute passes through this process before each test case in the test class runs. That is, we can put all the initialization conditions that everyone will use in it. There is a <testfixturesetup> attribute corresponding to setup. The process of this attribute is initialized only once during the entire test class. Setup is the sub process defined under each test case first.

Attribute <teardown> each test case is run to determine the resources to be recycled. The corresponding result is that <testfixtureteardown> is the sub defined by running all test cases before running them.

It can be said that these attributes are designed to make your code superior and reduce resource usage. When writing a test class, there may be many test cases. Instead of running these test cases at the same time, we will use the following attributes for test optimization.

<Ignore> indicates which test case does not run. <Explicit> indicates the ignore attribute, and the test case will never run. If the explicit it attribute is selected, the test case can be run.

In addition, we can group test cases. Use attribute <category ("group name")>. In this way, we can select a group in nunit for testing.

Nunit facilitates unit testing, but it is still not perfect. It cannot count the code coverage rate of our tests (a standard that tests whether my unit tests are in place ), you cannot debug the written test cases (errors are as easy as when writing the test code, and sometimes difficult to find if debugging is not possible. Testdriven. Net can do this, and all operations are performed in the Visual Studio integrated environment. Although testdriven. NET is based on nunit, we do not need to install nunit when using it because nunit installation is included while testdrive. NET is installed. However, when we reference nunitframework, we will find that this Assembly is not available, but we can use testdriven. find this Assembly under the installation directory of net (my installation directory: C:/program files/testdriven. net 3/nunit/2.5/net-2.0/framework), introduce it to the vs IDE for unit testing. Now our testing work is easier.

During the test, pay attention to the boundary points and the left and right points of the points. The test cases should be fully considered here. We also need to ensure that we do not change the preset state in the original program after testing. This item is especially prominent when operating the database. during testing, we may change the data in the database, after testing, remember to reply to the original data.

In short, the unit test further ensures the correctness of our code, and the unit test software simplifies our unit test work.

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.