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 |