DotNet program test with NUnit
DotNet program test with NUnit
Introduction
NUNIT is currently more popular .NET platform test tools, the following brief introductions to his development.
ready
To use NUnit, first make sure you have a NUNIT development package on your machine, you can from http://www.nunit.org/
The place is acquired and installed (the current version is NUNIT V2.1.91). Add a NUnit 2.2 project after proper installation.
Property description
Before you start writing an example, first describe the properties of NUnit:
TestFixTure (NUNIT2.0)
Identify the current class is a class that contains test methods.
Note: This class must have a default constructor, and must also be declared into public.
E.g
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
// ...
}
}
Test (NUNIT2.0)
Method in identifying a class that uses the TestFixTure identifier is a method that requires test.
Note: The signature of the method is identified as a non-return value.
E.g:
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
[TEST]
Public void add ()
{/ * ... * /}
Public void testsubtract ()
{/ * backwards comparative * /}
}
}
Setup / Teardown
TestFixTureTup / Setup is used to construct the environment before running test methods;
TestFixTureTeartown / Teardown is used to restore the environment after running test methods.
Note: You can only have a pair of tags in a test class (Identifier TestFixture).
TestfixTureTup / TestFixTureTeadown (NUnit2.1)
E.g:
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
[Testfixtureetup] public void init ()
{/ * ... * /}
[TestfixTureTeadown] Public Void Dispose ()
{/ * ... * /}
[TEST] public void add ()
{/ * ... * /}
}
}
Setup / Teardown (NUNIT2.0)
E.g:
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
[Setup] public void init ()
{/ * ... * /}
[Teardown] public void dispose ()
{/ * ... * /}
[TEST] public void add ()
{/ * ... * /}
}
}
EXPECTEDEXCEPTION (NUNIT2.0)
Specifies an exception to be thrown.
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successstests {[test]
[EXPECTEDEXCEPTION (TypeOperationException)]]
Public void expectanexception ()
{/ * ... * /}
}
}
Category (NUNIT2.2)
Identify the test in a set of tests.
When using this property, only the selected category is called.
E.g:
Use the Category property on TestFixTure
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
[Category ("longrunning")]]
Public Class LongrunningTests
{/ * ... * /}
}
Use the category attribute on TEST
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
[TEST]
[Category ("long")]]]
Public void VerylongTest ()
{/ * ... * /}
}
Explicit (NUNIT2.2)
Specify a TEST or TESTFIXTURE to be excluded in the test selected.
E.g:
Use the explicit attribute on TestFixTure
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure, Explicit]
Public Class Explicittests
{/ * ... * /}
}
Use the explicit attribute on TEST
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
Public class successs {
[TEST, EXPLICIT]
Public void explicittest ()
{/ * ... * /}
}
Suite (NUNIT2.0)
Identify a test unit.
????
E.g:
Namespace NUnit.tests {
Using system;
Using nunit.framework;
Public class alltests {
[Suite]
Public static testsuite suite {
Get {
Testsuite Suite = New Testsuite ("All Tests");
Suite.Add (New OnetestCase ());
Suite.Add (New assembly.assemblytests ());
Suite.Add (new assertiontest ());
Return suite;
}
}
}
}
Ignore (NUNIT2.0)
Identify Test or TestFixTure not running within a certain amount of time.
E.g:
Namespace NUnit.tests {
Using system;
Using nunit.framework;
[TestFixTure]
[Ignore ("Ignore A FixTure)]]]]]]]]
Public Class SuccessTests
{/ * ... * /}
}
Simple example
First build a console application project (TestNunit) with VS.NET, then add a reference to the project, select NUnit.Framework (can be found in NUnit installation directory), then add two classes, one is the class to be tested One is the test class, the content of the class to be tested is 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 is as follows:
Using system;
Using nunit.framework;
Namespace testnunit.sample1 {
[TestFixTure] / / -------------------------------------------- 1
Public class testsample1 {
PRIVATE SAMPLE1 SAMPLE;
[Setup] / / -------------------------------------------- 2
Public void init () {
this.sample = new Sample1 () ;;
}
[TEARDOWN] / / -------------------------------------------- 3
Public void teardown () {
THIS.SAMPLE = NULL;
}
[TEST] / / -------------------------------------------- 4
Public void testGethelloWorld () {
AskERT.Inotnull (this.sample.getHelloWorld ());
Assert.Arequal ("Hello World!", This.sample.getHelloWorld ());
}
[TEST]
Public void testAdd () {
AskERT.Arequal (2, this.sample.add (1, 1));
AskERT.Arequal (3, this.sample.add (1, 2));
AskERT.Arequal (4, this.sample.add (2, 2));
}
[TEST]
Public void testminus () {
AskERT.Arequal (0, this.sample.minus (1, 1));
AskERT.Arequal (1, this.sample.minus (2, 1));
AskERT.Arequal (-1, this.sample.minus (1, 2));
}
}
}
Pay attention to several special places in test class (Testsample1):
1 means that the current class is a test class;
2 Indicates the operation to be executed before the test is started;
3 indicates the operation to be executed after the test;
4 Represents the specific method of test, where each method corresponds to the method in the test class.
Compile the above class, run the NUNIT's GUI interface (start-> NUNIT 2.2-> NUNIT-GUI), select File-> Open, open the file you just compiled, this is selected with TestNunit.exe (depending on the specific application can be DLL or other Type) file, the window appears: Click the RUN button, appear the following interface:
When the running bar is green, the write test is all passed, and if the runtime is displayed, it indicates that the test has a problem, we need to modify it.
At this point, there are two methods (TestAdd, Testminus) testing problems, you need to check the modifications, then repeat the above operation until the running column is not displayed in red.
MOCK test
Introduction
The MOCK test is in the test process, for an object that is not easy to construct or acquire, create a test method for easy testing with a false object.
To use the Mock test, you need to have a dedicated MOCK test development package support, and the following uses nmock will be described. To get NMOCK, you can get from http://www.nmock.org/, currently use NMOCK V1.1.
Example
Add a reference
Add a reference nmock.dll in the project, you can get from http://www.nmock.org/.
Code
Need to be category of MOCK:
Using system;
Namespace testnunit.mocksample {
Public class bigclass {
Public Virtual String Dosoming (String Hello, String Name, String Symbol) {
Return "Hello" " Name Symbol;
}
Public virtual void donoing () {
// Todo
}
}
}
Test class:
Using system;
Namespace testnunit.mocksample {
PUBLIC CLASS SAMPLE {
PRIVATE BIGCLASS BC;
Public Sample (Bigclass BC) {
THIS.BC = Bc;
}
Public string getHelloWorld () {
Return "Hello World!";
}
Public String GenerateHelloWorld (String Hello, String Name, String Symbol) {
Return this.bc.dosoming ("Hello", Name, Symbol;
}
}
}
Test class:
Using system;
Using nunit.framework;
Using nMock;
Using nMock.constraints;
Namespace testnunit.mocksample {
[TestFixTure]
Public class testmocksample {
Private mock mock;
PRIVATE SAMPLE SAMPLE;
[Setup]
Public void init () {
THIS.MOCK = New DynamicMock (TypeOf (Bigclass));
This.sample = new sample (bigclass) mock.mockinstance;
}
[Teardown]
Public void teardown () {
Mock.Verify ();
[TEST]
Public void testGethelloWorld () {
THIS.MOCK.EXPECTNOCALL ("Dosoming", TypeOf (String), TypeOf (String), TypeOf (String);
this.mock.expectnocall ("donoing");
AskERT.Inotnull (this.sample.getHelloWorld ());
AskERT.Arequal (this.sample.getHelloWorld (), "Hello World!");
}
[TEST]
Public void testgeneratehelloworld () {
ISEQUAL HELLO = New ISEqual ("Hello");
Isanysthing name = new isanysthang ();
ISEQUAL SYMBOL = New ISEQUAL ("!");
THIS.MOCK.EXPECTRETURN ("Dosoming", "Hello Kongxx!", Hello, Name, Symbol;
this.mock.expectnocall ("donoing");
Assert.Arequal ("Hello Kongxx!", Sample.GenerateHelloWorld ("Hello", "Kongxx", "!"));
}
}
}
run
Compile the above class, then run the NUNIT graphical interface, load the compiled program (EXE or DLL), appear the following interface:
Then run, the following interface appears:
If the running bar displays green representation, if you check the error, modify, compile, and run until all run success.
Reference
1NUnit http://www.nunit.org/
2nmock http://www.nmock.org/index.html
3MockObjects http://www.mockobjects.com/frontpage.html