我们仔细一看,不对,有重复的代码,如何去除重复的代码呢?我们可以提取这些代码到一个独立的方法,然后标志这个方法为
SetUp属性,这样2个
测试方法可以共享对操作数的初始化了,这里是改动后的代码:
1using System;
2using NUnit.Framework;
3
4namespace NUnitQuickStart
5{
6 [TestFixture]
7 public class NumersFixture
8 {
9 private int a;
10 private int b;
11 [SetUp]
12 public void InitializeOperands()
13 {
14 a = 1;
15 b = 2;
16 }
17
18 [Test]
19 public void AddTwoNumbers()
20 {
21 int sum=a+b;
22 Assert.AreEqual(sum,3);
23 }
24 [Test]
25 public void MultiplyTwoNumbers()
26 {
27 int product = a * b;
28 Assert.AreEqual(2, product);
29 }
30
31 }
32}
33 这样NUnit将在执行每个测试前执行标记SetUp属性的方法.在本例中就是执行InitializeOperands()方法.记住,这里这个方法必须为public,不然就会有以下错误:Invalid Setup or TearDown method signature
ExpectedException
这里是一个验证这个假设的测试.有的时候,我们知道某些操作会有异常出现,例如, 在实例中增加除法,某个操作被0除,抛出的异常和.NET文档描述的一样.参看以下源代码.
1[Test]
2[ExpectedException(typeof(DivideByZeroException))]
3public void DivideByZero()
4{
5 int zero = 0;
6 int infinity = a/zero;
7 Assert.Fail("Should have gotten an exception");
8}
9
除了[Test]属性之外,DivideByZero方法有另外一个客户属性:ExpectedException.在这个属性里,你可以在执行过程中捕获你期望的异常类型,例如在本例就是DivideByZeroException.如果这个方法在没有抛出期望异常的情况下完成了,这个测试失败.使用这个属性帮助我们写程序员测试验证边界条件(Boundary Conditions).
Ignore属性
由于种种原因,有一些测试我们不想运行.当然,这些原因可能包括你认为这个测试还没有完成,这个测试正在重构之中,这个测试的需求不是太明确.但你有不想破坏测试,不然进度条可是红色的哟.怎么办?使用Ignore属性.你可以保持测试,但又不运行它们.让我们标记MultiplyTwoNumbers测试方法为Ignore属性:
1[Test]
2[Ignore("Multiplication is ignored")]
3public void MultiplyTwoNumbers()
4{
5 int product = a * b;
6 Assert.AreEqual(2, product);
7}
运行测试,现在产生了下面的输出(在图5-1显示):
图 5-1: 在一个程序员测试中使用 Ignore属性
Ignore属性可以附加到一个独立的测试方法,也可以附加到整个测试类(TestFixture).如果Ignore属性附加到TestFixture,所有在fixture的测试都被忽略.
TestFixtureSetUp/TestFixtureTearDown
有时,一组测试需要的资源太昂贵.例如,数据库连接可能是一个关键资源,在一个test fixture的每个测试中,打开/关闭
数据库连接可能非常慢.这就是我在开始提到的问题.如何解决?NUnit有一对类似于前面讨论的
SetUp/
TearDown的属性:
TestFixtureSetUp/
TestFixtureTearDown.正如他们名字表明的一样,这些属性用来标记为整个test fixture初始化/释放资源方法一次的方法.
例如,如果你想为所有test fixture的测试共享相同的数据库连接对象,我们可以写一个打开数据库连接的方法,标记为TestFixtureSetUp属性,编写另外一个关闭数据库连接的方法,标记为TestFixtureTearDown属性.这里是描述这个的例子.
1using NUnit.Framework;
2
3[TestFixture]
4public class DatabaseFixture
5{
6 [TestFixtureSetUp]
7 public void OpenConnection()
8 {
9 //open the connection to the database
10 }
11
12 [TestFixtureTearDown]
13 public void CloseConnection()
14 {
15 //close the connection to the database
16 }
17
18 [SetUp]
19 public void CreateDatabaseObjects()
20 {
21 //insert the records into the database table
22 }
23
24 [TearDown]
25 public void DeleteDatabaseObjects()
26 {
27 //remove the inserted records from the database table
28 }
29
30 [Test]
31 public void ReadOneObject()
32 {
33 //load one record using the open database connection
34 }
35
36 [Test]
37 public void ReadManyObjects()
38 {
39 //load many records using the open database connection
40 }
41}
42
43
Test Suite
Test Suite是test case或其他test suite的集合. 合成(Composite),模式描述了test case和test suite之间的关系.
参考来自NUnit的关于Suite的代码
Suite Attribute
1namespace NUnit.Tests
2{
3using System;
4 using NUnit.Framework;
5
6
7
8 public class AllTests
9 {
10 [Suite]
11 public static TestSuite Suite
12 {
13 get
14 {
15 TestSuite suite = new TestSuite("All Tests");
16 suite.Add(new OneTestCase());
17 suite.Add(new Assemblies.AssemblyTests());
18 suite.Add(new AssertionTest());
19 return suite;
20 }
21 }
22 }
23}
24