Learn how to use nunit in detail

Source: Internet
Author: User

It seems that I have heard of nunit before, but I have never used it. It is a very unqualified programmer. So I have to spend some time to complete it.

Refer to the confach article in the blog:

Http://www.cnblogs.com/confach/archive/2005/06/20/177817.html

Below are some notes for study.

 

1. TDD (test driver development)

There are two principles:

. Unless you have an automated test that fails, never write a single line of code.

. Duplicate organizations

This is the original story in the article. The second principle is well understood, and code repetition should be avoided as much as possible. First, I don't know if it's the reason for translation.

The author's explanation is vague: Do not write code without failing an automatic test, because the test is embedded in the requirement that the Code must meet, as shown in

If there is no need, there is no need to implement anything, so this principle prevents us from implementing features that are not tested and not needed in the solution.

 

Two types of test:

Customer testing: Recommendation framework-fit

Test by the programmer: nunit

2. nunit

Nunit is for. net.

So that you do not forget: http://www.nunit.org/

I will not introduce how to reference the tool interface, but I will be familiar with it several times later. Record some common attributes:

Testfixtureattribute

Purpose: Mark a class inclusion test

The marked class also requires some restrictions:

  • Must be public
  • There must be a default constructor
  • Constructor should have no side effects, because nunit often constructs this class multiple times during runtime.

A general class has a default constructor. As for the side effects, I don't know if it refers to some operations of other objects, such as deletion, insertion, and calculation.

Computing...

Testattribute

Purpose: A method marked as testfixture can be tested.

Limitations of the test method:

  • Must be public
  • No parameter is required
  • No parameters are returned.

These two attributes are the most basic. The test method does not have any parameters and cannot return parameters. Generally, we use assert (assert) for comparison.

3. Use nunit in. net

Note the following when setting nunit as the startup program. In vs2008, right-click the corresponding project and select "attribute ".

In "debug", modify the value of "Start external program.

4. Other core concepts

Setup/teardown attributes

Role: Regular initialization/cleanup

If some test methods have repeated initialization code, you can extract the code to an independent method and mark the method

Setup. Of course, the method must be public. After testing, we find that every test method will actually execute this Initialization Method

It is not the kind of initialization only once.

Expectedexception

Purpose: capture the expected exception type during execution.

For example, if the divisor is 0 in Division, an exception occurs. In this case, we expect the exception type to be dividebyzeroexception and mark it on the method.

Expectedexception attribute. The specified type is dividebyzeroexception. If another exception type is specified, an error is returned for the test method because

The expected exception type is different from the actual one.

Ignore

Purpose: Do not run the test method for the moment.

It can also be appended to the entire test class, so that all tests in the class will be ignored.

Testfixturesetup/testfixtureteardown

Purpose: mark it as the entire testfixture to initialize/release resources

The entire testfixture is executed only once, so you do not need to execute this method when executing each test method.

Suite

Role: Set of testcase or other test suite

The testsuite class in the test code is not found, so this attribute is still to be studied .....

Category

Purpose: classify tests.

Test classification. In this way, we can select to perform tests for only one or more classes.

Explicit

Purpose: ignore a test and test fixture until they explicitly choose to execute.

The test shows that if the attribute is set to ignore, it is displayed as a yellow progress bar and cannot be executed manually. If the property is explicit

Ignore this method, but you can manually select to execute it.

5. Test the lifecycle contract

The setup/teardown method provides the purpose of isolation test. Setup ensures that the shared resources are correctly initialized before each test run. teardown is true.

To prevent the legacy side effects of running the test. Testfixturesetup/testfixtureteardown is similar, but in the range of testfixture, the four

Together, the test runner and life-Cyle contract of the test framework are formed)

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.