原创作者:kongxx

原文地址:http://blog.csdn.net/kongxx/archive/2004/07/29/55742.aspx

介绍
NUnit是目前比较流行的.Net平台的测试工具,以下就简单介绍一下他的开发。

准备
要使用NUnit,首先要确保您的机器上有NUnit的开发包,您可以从http://www.nunit.org/

地方获取并安装(目前版本是NUnit v2.1.91)。正确安装后会在开始菜单下添加一个NUnit 2.2项目。

属性说明
在开始写例子之前,先把NUnit的属性说明一下:

TestFixture (NUnit2.0)
标识当前类是一个包含测试方法的类。

注意:这个类必须有一个默认的构造方法,并且也必须声明成Public。

例如

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    // ...  }}

Test (NUnit2.0)
标识一个使用TestFixture标识的类中的方法是一个需要测试的方法。

注意:方法的签名被标识为无返回值。

例如:

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {[Test]public void Add()    { /* ... */ }    public void TestSubtract()    { /* backwards compatibility */ }  }}

SetUp/TearDown
TestFixtureSetUp/SetUp用来在运行测试方法之前构造环境;

TestFixtureTearDown/TearDown用来在运行测试方法之后还原环境。

注意:一个测试类(标识TestFixture)中只可以有一对标记。

TestFixtureSetUp/TestFixtureTearDown (NUnit2.1)
例如:

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    [TestFixtureSetUp] public void Init()    { /* ... */ }    [TestFixtureTearDown] public void Dispose()    { /* ... */ }    [Test] public void Add()    { /* ... */ }  }}

SetUp/TearDown (NUnit2.0)
例如:

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    [SetUp] public void Init()    { /* ... */ }    [TearDown] public void Dispose()    { /* ... */ }    [Test] public void Add()    { /* ... */ }  }}

ExpectedException (NUnit2.0)
指定一个测试将要抛出的异常。

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    [Test]    [ExpectedException(typeof(InvalidOperationException))]    public void ExpectAnException()    { /* ... */ }  }}

Category (NUnit2.2)
标识在一组测试中选中的测试。

当使用此属性是,只有选中的Category才会被调用。

例如:

在TestFixture上使用Category属性

namespace NUnit.Tests{  using System;  using NUnit.Framework;  [TestFixture]  [Category("LongRunning")]  public class LongRunningTests  {/*…*/}}

在Test上使用Category属性

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    [Test]    [Category("Long")]    public void VeryLongTest()    { /* ... */ }}

Explicit(NUnit2.2)
指定一个Test或TestFixture被排除在测试选中的测试中。

例如:

在TestFixture上使用Explicit属性

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture, Explicit]  public class ExplicitTests  {/* ... */}}

在Test上使用Explicit属性

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  public class SuccessTests {    [Test, Explicit]    public void ExplicitTest()    { /* ... */ }}

Suite (NUnit2.0)
标识一个测试单元。

????

例如:

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 Assemblies.AssemblyTests());        suite.Add(new AssertionTest());        return suite;      }    }  }}

Ignore(NUnit2.0)
在一定的时间内标识Test或TestFixture不运行。

例如:

namespace NUnit.Tests {  using System;  using NUnit.Framework;  [TestFixture]  [Ignore("Ignore a fixture")]  public class SuccessTests{/* ... */}}

简单例子
首先用VS.Net建立一个控制台应用程序项目(TestNUnit),然后在项目中添加引用,选中nunit.framework(可以在NUnit的安装目录下找到),然后添加两个类,一个是待测试的类,一个是测试类,

//---待测试的类内容如下 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 ; } } } //---测试类内容如下 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() { Assert.IsNotNull(this.sample.GetHelloWorld()); Assert.AreEqual("Hello World!" ,this.sample.GetHelloWorld()); } [Test] public void TestAdd() { Assert.AreEqual(2,this.sample.Add(1,1)); Assert.AreEqual(3,this.sample.Add(1,2)); Assert.AreEqual(4,this.sample.Add(2,2)); } [Test] public void TestMinus() { Assert.AreEqual(0,this.sample.Minus(1,1)); Assert.AreEqual(1,this.sample.Minus(2,1)); Assert.AreEqual(-1,this.sample.Minus(1,2)); } } }

注意测试类(TestSample1)中几个特殊的地方:

1表示当前类是一个测试类;

2表示测试启动前要执行的操作;

3表示测试后要执行的操作;

4表示具体的每个测试方法,这里每个方法都对应要测试类中的方法。

编译以上类,运行NUnit的GUI界面(开始->NUnit 2.2->Nunit-Gui),选择File->Open,打开刚才编译项目生成的文件,这里选中TestNUnit.exe(根据具体应用可以是DLL或者别的类型)文件,此时出现一下窗口:


点击Run按钮,出现以下界面:


当运行栏全部是绿色的时候,表示写的测试全部通过,如果出现运行栏显示红色,表示测试出现问题,需要我们修改。


此时表示有两个方法(TestAdd,TestMinus)测试出现问题,需要我们去检查修改,然后重复以上的操作,直到运行栏全部不在显示红色为止。

Mock测试
简介
Mock测试就是在测试过程中,对于某个不容易构造或获取的对象,用一个虚假的对象来创建以方便测试的测试方法。

要使用Mock测试,需要有专门的Mock测试开发包支持,以下使用Nmock来说明。要获得Nmock可以从http://www.nmock.org/获得,目前使用NMock V1.1。

示例
添加引用
在项目中添加引用nmock.dll,可以从http://www.nmock.org/获得。

代码
//---需要被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 } } } //---被测试的类 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); } } } //---测试类 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"); Assert.IsNotNull(this.sample.GetHelloWorld()); Assert.AreEqual(this.sample.GetHelloWorld() ,"Hello World!" ); } [Test] public void TestGenerateHelloWorld() { IsEqual hello = new IsEqual("Hello"); IsAnything name = new IsAnything(); IsEqual symbol = new IsEqual("!"); this.mock.ExpectAndReturn("DoSoming","Hello kongxx!" ,hello, name, symbol); this.mock.ExpectNoCall("DoNoing"); Assert.AreEqual("Hello kongxx!",sample.GenerateHelloWorld("Hello","kongxx","!")); } } }

运行
编译以上类,然后运行NUnit的图形界面,载入编译过的程序(exe或dll),出现以下界面:


然后运行,出现以下界面:


如果运行栏全部显示绿色表示通过测试,否则检查错误,修改,编译,运行直到全部运行成功为止。

参考资料
1NUnit http://www.nunit.org/

2Nmock http://www.nmock.org/index.html

3mockobjects http://www.mockobjects.com/FrontPage.html

使用NUnit进行DotNet程序测试相关推荐

  1. Win2008 R2 RemoteApp深度体验之四,RemoteApp程序测试

    RemoteApp程序测试<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...

  2. “提高一下dotnet程序的效率一”中关于exception的问题

    看了提高一下dotnet程序的效率一中关于exception的部分和相关的评论,我觉得有些知识点可能大家平时没有注意到或没有深入的了解: Exception在没有产生exception的时候,换句话说 ...

  3. 网络bcc程序测试方案

    网络bcc程序测试方案 1. 具体做法 1.1 准确性测试 1.2 性能测试 2. 数据模拟工具 3. 传统工具举例 3.1 iftop实时流量监控工具(此工具可用来测试网络流量指标程序) 3.2 n ...

  4. 单元测试01:nunit 安装与代码测试

    1.nunit 下载与安装 a.下载 下载地址: http://nunit.org/download/ b.添加到系统环境变量 解压下载包后,添加两个路径到环境变量,如: D:\nunitD:\nun ...

  5. DotNet程序员是不是最不幸福?

    自我学C#和ASp.net时就来逛DotNet区,总是时不时有人问"学.net是不是没有前途?",总是有人"报怨.net程序人收入比某某要低".还有人说&quo ...

  6. 前端构建工具与应用程序测试

    1.前端构建工具 什么是前端构建? 什么是构建工具? 自动构建工具 Npm Scripts(推荐) Npm Scripts(NPM脚本)是一个任务执行者.NPM是安装Node时附带的一个包管理器,Np ...

  7. 缺少微信小程序测试经验?这篇文章带你从0开始

    微信小程序已经越来越普遍,但目前接触小程序的项目相对较少,对小程序的特性也不了解,缺少小程序测试实战经验. 本文主要通过对微信小程序特性和测试点进行总结,储备测试知识,提高测试效率. 小程序发布审核 ...

  8. 并行网络测试软件,并行程序测试

    写一个正确的并行程序要比写顺序执行程序困难. 其原因是并行程序中潜在的风险和错误的种类更多 -- 首先,在一个顺序执行程序中的错误同样会发生在并行程序中:其次,并行程序比顺序执行程序需要关注更多的风险 ...

  9. 定义一个名为 Circle的类,编写程序测试这个圆类的所有方法。

    定义一个名为 Circle的类,其中含有double型的成员变量centerX和centerY表 示圆心坐标,radius 表示圆的半径.定义求圆面积的方法getArea0方法和求圆周长的方法getP ...

最新文章

  1. C#事件与委托的区别
  2. 【每周CV论文】初学GAN图像风格化必须要读的文章
  3. 用python画钢铁侠_用自动铅笔素描画钢铁侠盔甲
  4. Camel 2.11 –没有Spring的Camel Web应用程序
  5. 十七、二分查找法(java)
  6. bzoj2242 [SDOI2011]计算器 exgcd+ksm+bsgs
  7. php中数组的指针函数参数传递参数,循环语句、函数的参数及作用域、数组键值及指针操作函数(8月23日作业)...
  8. lucene全文检索的概念
  9. kubernetes apiserver认证 1
  10. [读书笔记]iOS 7 UI设计 对比度
  11. 9.程序员的自我修养---Windows下的动态链接
  12. 数据结构乐智教学百度云_数据结构 百度网盘分享
  13. winxp关闭系统音频服务器,xp系统显示没有音频设备怎么办 xp系统音频驱动异常或者未安装如何解决...
  14. 如何在图片上加水印?图片添加、去除水印方法
  15. 图谱实战 | 谈元鹏:电力领域知识图谱技术进展与应用实践
  16. 【马仔创业感悟】公司售前和售后维护制度思考
  17. div可拖拽移动js方法
  18. php微信支付宝第三方接口开发平台,帝国CMS第三方个人支付接口微信支付宝免签约即时到账api_帝国网站管理系统插件...
  19. CCRC信息安全服务资质。
  20. 成都拓嘉辰丰:拼多多新手开店需掌握的运营方向

热门文章

  1. 案例-翻转的导航栏(CSS3)
  2. vue项目中input框默认获得焦点,回车选中输入文本
  3. html选择器 并列,CSS 中的选择器 (二)- 组合选择器
  4. 东北林业大学c语言期末考试题,东北林业大学 2008年C语言考试试卷及答案.doc
  5. 在计算机中 ascii码是几位二进制编码,ASCII码采用多少位二进制编码
  6. 整理了js数组去重4种方法
  7. Step by Step 创建一个WCF Service
  8. Cisco访问控制列表
  9. js中的浅拷贝深拷贝深入理解
  10. 中间件配置文件-nginx