Using NUnit for Dotnet program testing

Source: Internet
Author: User
Tags dotnet

using NUnit for dotnet program testing

Author: kongxx introduce

NUnit is currently the more popular test tool for the. NET platform, the following is a brief introduction to his development. Ready to

To use NUnit, first make sure that you have a NUnit development package on your machine, and you can start with the http://www.nunit.org/

Place to obtain and install (current version is NUnit v2.1.91). After proper installation, a NUnit 2.2 item is added under the Start menu. Attribute Description

Before you start writing examples, explain the attributes of NUnit: testfixture (NUnit2.0)

Identifies the current class as a class that contains test methods.

Note: This class must have a default construction method and must also be declared public.

For example

}

Test (NUnit2.0)

Identifying a method in a class that uses the Testfixture identity is a method that needs to be tested.

Note: The signature of the method is identified as having no return value.

For example:

Setup/teardown

Testfixturesetup/setup is used to construct the environment before running the test method;

Testfixtureteardown/teardown is used to restore the environment after the test method is run.

Note: A test class (identity testfixture) can have only one pair of tags. Testfixturesetup/testfixtureteardown (NUnit2.1)

For example:

Setup/teardown (NUnit2.0)

For example:

expectedexception (NUnit2.0)

Specifies the exception that a test will throw.

}

Category (NUnit2.2)

Identifies the tests selected in a set of tests.

When this property is used, only the selected category are invoked.

For example:

Using the Category property on Testfixture

Using the Category property on test

}

Explicit (NUnit2.2)

Specifies that a test or testfixture is excluded from test-checked tests.

For example:

Using the explicit property on Testfixture

Using the explicit property on test

Suite (NUnit2.0)

Identifies a test unit.

????

For example:

}

Ignore (NUnit2.0)

Identity test or testfixture is not running for a certain amount of time.

For example:

}

Simple Example

First build a console Application project (Testnunit) with vs.net, then add a reference to the project, select nunit.framework(which can be found under the NUnit installation directory), and then add two classes, one for the test class, One is the test class,

The contents of the class to be tested are as follows:

Using System;

Namespace Testnunit.sample1

{

public class Sample1 {

Public Sample1 () {

}

Public String Gethelloworld () {

Return "Hello world!";

}

public int Add (int i1, int i2) {

return i1 + i2;

}

public int minus (int i1, int i2) {

return i1-i2;

}

}

}

The test class content is as follows:

Using System;

Using Nunit.framework;

Namespace Testnunit.sample1 {

[Testfixture]//--------------------------------------------1

public class TestSample1 {

Private Sample1 sample; &n

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.