- 浏览: 15940 次
- 性别:
- 来自: 成都
-
最新评论
TestFixtureSetUp/TestFixtureTearDown
有时,一组测试需要的资源太昂贵.例如,数据库连接可能是一个关键资源,在一个test fixture的每个测试中,打开/关闭数据库连接可能非常慢.这就是我在开始提到的问题.如何解决?NUnit有一对类似于前面讨论的SetUp/TearDown的属性: TestFixtureSetUp/TestFixtureTearDown.正如他们名字表明的一样,这些属性用来标记为整个test fixture初始化/释放资源方法一次的方法.
例如,如果你想为所有test fixture的测试共享相同的数据库连接对象,我们可以写一个打开数据库连接的方法,标记为TestFixtureSetUp属性,编写另外一个关闭数据库连接的方法,标记为TestFixtureTearDown属性.这里是描述这个的例子.
1using NUnit.Framework; 2
3
[TestFixture] 4
public 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
1
namespace NUnit.Tests
2

{
3
using 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
Category属性
1
namespace NUnit.Tests
2

{
3
using 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

2



3

4

5

6

7

8

9



10

11

12



13

14



15

16

17

18

19

20

21

22

23

24

对于测试来说,你有的时候需要将之分类,此属性正好就是用来解决这个问题的。
你可以选择你需要运行的测试类目录,也可以选择除了这些目录之外的测试都可以运行。在命令行环境里 /include 和/exclude来实现。在GUI环境下,就更简单了,选择左边工作域里的Catagories Tab,选择Add和Remove既可以了。
在上面的例子上做了一些改善,代码如下:
NUnit-GUI界面如图5-2:1using System; 2
using NUnit.Framework; 3
4
namespace 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
[Category("Numbers")] 20
public void AddTwoNumbers() 21
![]()
{ 22
int sum=a+b; 23
Assert.AreEqual(sum,3); 24
} 25
26
[Test] 27
[Category("Exception")] 28
[ExpectedException(typeof(DivideByZeroException))] 29
public void DivideByZero() 30
![]()
{ 31
int zero = 0; 32
int infinity = a/zero; 33
Assert.Fail("Should have gotten an exception"); 34
} 35
[Test] 36
[Ignore("Multiplication is ignored")] 37
[Category("Numbers")] 38
public void MultiplyTwoNumbers() 39
![]()
{ 40
int product = a * b; 41
Assert.AreEqual(2, product); 42
} 43
44
} 45
![]()
图5-2:使用Catagories属性的界面
Explicit属性
本属性忽略一个test和test fixture,直到它们显式的选择执行。如果test和test fixture在执行的过程中被发现,就忽略他们。所以,这样一来进度条显示为黄色,因为有test或test fixture忽略了。
例如:
1
2
[Test,Explicit]
3
[Category("Exception")]
4
[ExpectedException(typeof(DivideByZeroException))]
5
public void DivideByZero()
6
{
7
int zero = 0;
8
int infinity = a/zero;
9
Assert.Fail("Should have gotten an exception");
10
}
11

2

3

4

5

6



7

8

9

10

11

Expected Exception属性
期望在运行时抛出一个期望的异常,如果是,则测试通过,否则不通过。
参看下面的例子:
1
[Test]
2
[ExpectedException(typeofInvalidOperationException))]
3
public void ExpectAnException()
4
{
5
int zero = 0;
6
int infinity = a/zero;
7
Assert.Fail("Should have gotten an exception");
8
9
}
10

2

3

4



5

6

7

8

9

10

5 . 测试生命周期合约
如果记得test case的定义,其中一个属性是测试的独立性或隔离性.SetUp/TearDown方法提供达到测试隔离性的目的.SetUp确保共享的资源在每个测试运行前正确初始化,TearDown确保没有运行测试产生的遗留副作用. TestFixtureSetUp/TestFixtureTearDown同样提供相同的目的,但是却在test fixture范围里,我们刚才描述的内容组成了测试框架的运行时容器(test runner)和你写的测试之间的生命周期合约(life-cycle contract).
为了描述这个合约,我们写一个简单的测试来说明什么方法调用了,怎么合适调用的.这里是代码:
1using System; 2
using NUnit.Framework; 3
[TestFixture] 4
public class LifeCycleContractFixture 5
{ 6
[TestFixtureSetUp] 7
public void FixtureSetUp() 8
![]()
{ 9
Console.Out.WriteLine("FixtureSetUp"); 10
} 11
12
[TestFixtureTearDown] 13
public void FixtureTearDown() 14
![]()
{ 15
Console.Out.WriteLine("FixtureTearDown"); 16
} 17
18
[SetUp] 19
public void SetUp() 20
![]()
{ 21
Console.Out.WriteLine("SetUp"); 22
} 23
24
[TearDown] 25
public void TearDown() 26
![]()
{ 27
Console.Out.WriteLine("TearDown"); 28
} 29
30
[Test] 31
public void Test1() 32
![]()
{ 33
Console.Out.WriteLine("Test 1"); 34
} 35
36
[Test] 37
public void Test2() 38
![]()
{ 39
Console.Out.WriteLine("Test 2"); 40
} 41
42
} 43
44
![]()
当编译和运行这个测试,可以在System.Console窗口看到下面的输出:
FixtureSetUp
SetUp
Test 1
发表评论
-
Visual C++ 2010(2008)创建Ribbon界面
2011-07-01 22:28 1068http://hayyoungsue.blog.163.com ... -
C++0x FAQ中文版
2011-06-27 21:47 806http://space.itpub.net/trackbac ... -
Windows用户模式与内核模式
2011-06-01 09:18 963从Intel80386开始,出于安 ... -
Windows用户模式与内核模式
2011-06-01 09:18 11从Intel80386开始,出于安 ... -
MFC 消息类型
2011-03-30 10:19 7801、命令消息(WM_COMMAND) ... -
Css处理负数需要position的配合【及IE6CSS负数遮挡处理】
2010-12-30 22:25 1442有8个像素是负数,在IE6下会被遮挡,因此这个时候,必须用po ... -
NHibernate之旅系列文章导航
2010-12-10 22:35 727http://www.cnblogs.com/lyj/arch ... -
NUnit2.0详细使用方法 (一)
2010-12-10 22:24 8071. TDD的简介 首先什么是TDD呢?Kent Beck ... -
JAVA基础:Hibernate外键关联与HQL语法
2010-12-05 23:22 853例如对于TUser类 1.实体查询 String hql ... -
对象生死劫 - 构造函数和析构函数的异常
2010-11-19 08:40 884构造函数和析构函数分 ... -
Fck编辑器的完整详解
2010-11-17 15:07 924javascript调用方式: --------------- ... -
java抽象类和接口和继承之间关系
2010-11-09 14:52 1414有时候,我们可能想要构造一个很抽象的父类对象,它可能仅仅代表一 ...
相关推荐
### NUnit2.0详细使用方法 #### TDD与NUnit概览 测试驱动开发(TDD)是一种编程实践,其中开发者先编写测试用例,再编写能够通过这些测试的代码。这种方法有助于确保代码的质量和功能的正确性。TDD的核心原则包括: ...
本文将详细介绍NUnit2.0的使用方法,帮助开发者更好地理解和运用这个强大的工具。 **一、安装与引入** 首先,你需要从NUnit的官方网站下载NUnit2.0的安装包,它通常包含一个dll文件,如nunit.framework.dll,这是...
这个问题促使我相对NUnit做一个详细的介绍.因为我们大家都知道NUnit是在.NET进行TDD的利器. 如果你已经知道很多关于NUnit的应用,请指出我的不对之处和提出一些建议,使本文更加完善.如果你对NUnit还不是很了解的话,...
NUNIT2.0 软件测试用具(C#.NET)
C#的一个学习软件,只希望与大家一起分享
使用NUnit进行测试时,开发者可以创建测试类,每个类包含多个测试方法。这些测试方法通常以"Test"开头,以便NUnit识别它们。通过使用[NUnit Test Attributes]...
通过使用[NUnit GUI Runner](https://docs.nunit.org/articles/nunit-2.5/nunit-gui-runner.html)或[NUnit Console Runner](https://docs.nunit.org/articles/nunit-2.5/nunit-console-runner.html),开发者可以轻松...
5. **测试结果报告**:NUnit能够生成详细的测试报告,包括成功、失败、错误和忽略的测试。这些报告有助于快速定位问题,理解测试覆盖率。 6. **扩展性**:NUnit有一个插件系统,允许开发自定义的测试适配器、约束和...
3. **NUnit2.pdf**:这可能是NUnit的用户手册或者指南,详细介绍了如何使用NUnit进行单元测试,包括创建测试、运行测试套件、断言方法和其他高级特性。 4. **license.txt**:这是NUnit的许可协议,规定了软件的使用...
NUnit是一个用于.NET应用程序的单元测试框架,它支持.NET Framework的多个版本,包括.NET 2.0以上。通过NUnit,开发者可以编写可重复运行的测试用例,以验证代码的各个模块是否按预期工作。NUnit提供了丰富的断言...
随包提供的“使用说明.htm”和“使用说明.txt”文件,是NUnit的使用指南,可能包含如何配置、编写和运行测试的详细步骤。这些文档通常会涵盖如何创建测试类、定义测试方法、组织测试集以及如何读取和理解测试结果等...
NUnit Test Generator是一个自动生成测试用例的工具,可以编辑生成用例的模板,直接双击KellermanSoftware.NUnit-Test-Generator.exe文件就可以使用,需要的朋友可以下载。如使用中出现问题请直接给留言。
NUnit是一个开源的、广泛使用的单元测试框架,它允许开发人员编写代码来测试自己的.NET应用程序或库的功能。 描述中的“用于.NET测试的不错的软件”进一步确认了NUnit在.NET生态系统中的重要角色,作为一个高效的...
NUnit支持多种类型的测试,包括方法级别的断言,如Assert.AreEqual()来验证预期结果,以及测试固定装置来设置测试环境和清理操作。它还支持测试的分类和过滤,方便大型项目的管理和执行。此外,NUnit还提供了强大的...
"NUnit2.pdf"很可能是NUnit的使用指南或教程,涵盖了如何安装、配置和使用NUnit进行单元测试的详细步骤。通常,这样的文档会包含以下内容: 1. **安装与配置**:解释如何下载并安装NUnit,包括如何运行提供的msi...
NUnit 是一个广泛使用的开源测试框架,主要用于 .NET 开发中的单元测试。它是 .NET 社区中的首选工具,因为它提供了简单易用的API和丰富的断言库,使得开发人员可以方便地对代码进行验证,确保其功能正确无误。 在...
在使用NUnit进行测试驱动开发(TDD)时,首先编写测试用例,然后编写最小量的生产代码来使测试通过。这种开发方式有助于确保代码的健壮性和可维护性,因为它强制开发者思考各种可能的边界条件和错误情况。 NUnit...
在使用NUnit2.5.9时,开发者通常会创建一个测试项目,然后在项目中添加待测试的代码引用,接着编写测试fixture和测试方法,最后运行NUnit测试宿主来执行这些测试。如果所有测试都通过,那么可以有信心地说,代码的这...
在进行单元测试时,了解如何编写 NUnit 测试用例、使用断言方法(如 `Assert.AreEqual`、`Assert.Throws` 等)以及如何组织测试结构(如测试 fixture、测试方法等)都是必要的技能。NUnit 提供了丰富的文档和示例,...