| NUnit 2.4.6下载: | |
| win .net 1.1 | NUnit-2.4.6-net-1.1.msi |
| win .net 2.0 | NUnit-2.4.6-net-2.0.msi |
| bin .net 1.1 | NUnit-2.4.6-net-1.1.zip |
| bin .net 2.0 | NUnit-2.4.6-net-2.0.zip |
| src | NUnit-2.4.6-src.zip |
| doc | NUnit-2.4.6-doc.zip |


using NUnit.Framework;
namespace MyTest.Tests
{

public void MethodName()
using System;
using NUnit.Framework;
namespace MyTest.Tests
*/ }



using System;
using NUnit.Framework;
namespace NUnitQuickStart
{
int a=1;
int b=2;
int sum=a+b;
Assert.AreEqual(sum,3);
}
}
}
[img=32,32]file:///C:/Documents%20and%20Settings/Administrator/My%20Documents/My%20Webs/NUnit.5.gif[/img]
图 4-3:将NUnit-Gui 作为工程的测试运行器
第5步.编译运行测试. 现在编译solution。成功编译后,开始应用程序。NUnit-Gui测试运行器出现。当你第一次开始NUnit-Gui,它打开时没有测试加载。从File菜单选择Oprn,浏览NUnitQuickStart.dll的路径。当你加载了测试的程序集,测试运行器为加载的程序集的测试产生一个可见的表现。在例子中,测试程序集仅有一个测试,测试程序集的结构如图4-4所示:
using System;
using NUnit.Framework;
namespace NUnitQuickStart 

{
[TestFixture]
public
{
[Test]
public
{
int a=1;
int b=2;
int sum=a+b;
Assert.AreEqual(sum,3);
}
[Test]
public
{
int a =
int b =
int product = a * b;
Assert.AreEqual(2, product);
}
}
}
using System;
using NUnit.Framework;
namespace NUnitQuickStart 

{
[TestFixture]
public
{
private
private
[SetUp]
public
{
a =
b =
}
[Test]
public
{
int sum=a+b;
Assert.AreEqual(sum,3);
}
[Test]
public
{
int product = a * b;
Assert.AreEqual(2, product);
}
}
} 
[Test]
[ExpectedException(typeof(DivideByZeroException))]
public

{
int zero =
int infinity = a/zero;
Assert.Fail("Should have gotten an exception");
}
[Test]
[Ignore("Multiplication is ignored")]
public

{
int product = a * b;
Assert.AreEqual(2, product);
}
using NUnit.Framework;
[TestFixture]
public

{
[TestFixtureSetUp]
public
{
//open the connection to the database
}
[TestFixtureTearDown]
public
{
//close the connection to the database
}
[SetUp]
public
{
//insert the records into the database table
}
[TearDown]
public
{
//remove the inserted records from the database table
}
[Test]
public
{
//load one record using the open database connection
}
[Test]
public
{
//load many records using the open database connection
}
}
namespace NUnit.Tests

{
using System;
using NUnit.Framework;


public
{
[Suite]
public
{
get
{
TestSuite suite =
suite.Add(new OneTestCase());
suite.Add(new Assemblies.AssemblyTests());
suite.Add(new AssertionTest());
return suite;
}
}
}
} 
using System;
using NUnit.Framework;
namespace NUnitQuickStart 

{
[TestFixture]
public
{
private
private
[SetUp]
public
{
a =
b =
}
[Test]
[Category("Numbers")]
public
{
int sum=a+b;
Assert.AreEqual(sum,3);
}
[Test]
[Category("Exception")]
[ExpectedException(typeof(DivideByZeroException))]
public
{
int zero =
int infinity = a/zero;
Assert.Fail("Should have gotten an exception");
}
[Test]
[Ignore("Multiplication is ignored")]
[Category("Numbers")]
public
{
int product = a * b;
Assert.AreEqual(2, product);
}
} 
图5-2:使用Catagories属性的界面 
[Test,Explicit]
[Category("Exception")]
[ExpectedException(typeof(DivideByZeroException))]
public
{
int zero =
int infinity = a/zero;
Assert.Fail("Should have gotten an exception");
}
[Test]
[ExpectedException(typeofInvalidOperationException))]
public
{
int zero =
int infinity = a/zero;
Assert.Fail("Should have gotten an exception");
} 
using System;
using NUnit.Framework;
[TestFixture]
public

{
[TestFixtureSetUp]
public
{
Console.Out.WriteLine("FixtureSetUp");
}
[TestFixtureTearDown]
public
{
Console.Out.WriteLine("FixtureTearDown");
}
[SetUp]
public
{
Console.Out.WriteLine("SetUp");
}
[TearDown]
public
{
Console.Out.WriteLine("TearDown");
}
[Test]
public
{
Console.Out.WriteLine("Test 1");
}
[Test]
public
{
Console.Out.WriteLine("Test 2");
}
}

FixtureSetUp
SetUp
Test 1
TearDown
SetUp
Test 2
TearDown
FixtureTearDown