NUnit --- 从零开始

    既然是从零开始,就先介绍一下NUnit ( http://www.nunit.org/):一个.NET 框架下的单元测试框架,提供了类似于 JUnit的功能,也是 .NET 框架下开发者应用最广泛的单元测试框架之一(其他的还包括 CSUnit 等等)。

    它的基本原理是通过 .NET 的反射机制,利用代码中的元数据(Attribute)来辨识到底有哪些单元测试。单元测试(Unit Test)是测试驱动开发(Test-Driven Development,TDD)很重要的一环,而TDD又是敏捷开发方法(比如极限编程--eXtreme Programming)的重要组成部分…… 总之,单元测试很重要就对了。 ^_^ (有关TDD、XP的详细介绍在博客园的很多blog上就有,当然 google 上就更多了)

例子开始:

1。下载、安装 NUnit(最新版可能是 2.2.0)

2。很重要的步骤:测试一下 NUnit 是否安装成功。

    方法:打开 NUnit,File--Open--选 NUnit 安装目录下的bin目录中的 nunit.tests.dll。这时NUnit 主窗口左部的树型列表中会出现很多个测试的名字,然后点 Run 按钮,接着测试就开始运行了,直到 NUnit 主窗口左部的树型列表中所有的测试前面都变成 绿色,那就是成功了。(失败的测试会有 红色的提示,没有运行的测试会有 黄色的提示。在这一步中,有可能 Console Runner 那个测试集合会出现问题。万一出现问题,重启一下 NUnit 再 Run,一般都是没问题的)

gui-verify.gif


2。怎么在开发中使用 NUnit 框架?
    1)打开VS.NET 2003,新建一个 C# 的 Console 项目, 在项目的 References 添加 nunit.framework(References 在 Solution Explorer 窗口中,右键,Add Reference...)

    2)随便写一个类
None.gif public   class  Account     //  银行帐户类
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private float balance; // 账户的余额
InBlock.gif

InBlock.gif    
public void Deposit(float amount)    // 存钱
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        balance
+=amount;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void Withdraw(float amount)    //取钱
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        balance
-=amount;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void TransferFunds(Account destination, float amount) // 转账
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        destination.Deposit(amount);
InBlock.gif        Withdraw(amount);
ExpandedSubBlockEnd.gif    }

InBlock.gif 
InBlock.gif    
public float Balance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gifreturn balance;}
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void Main(string[] args) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Account source 
= new Account();    // 新建个账户
InBlock.gif
        source.Deposit(200.00F);    // 存200
InBlock.gif
        Account destination = new Account();    // 又建了一个
InBlock.gif
        destination.Deposit(150.00F);    // 存150
InBlock.gif
        source.TransferFunds(destination, 100.00F);    // 第一个账户转给第二个100
ExpandedSubBlockEnd.gif
    }

ExpandedBlockEnd.gif}
这个类很简单,编译通过,运行,一切ok。

   3)在同一个项目中,增加一个用来测试 Account 类中的方法的测试类(里面的几个Attribute是最关键的)

None.gif using  NUnit.Framework;     //  千万别忘了这一行
None.gif

None.gif[TestFixture] 
//  这个Attribute说明 AccountTest 类中包含有测试
None.gif
public   class  AccountTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [Test]    
// 这个Attribute说明了 TestTransferFunds() 方法就是用来做测试的
InBlock.gif    
// 一般测试方法的名字就是在被测试方法名前加上Test
InBlock.gif
    public void TestTransferFunds()    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// 准备工作
InBlock.gif
        Account source = new Account();
InBlock.gif        source.Deposit(
200.00F);
InBlock.gif        Account destination 
= new Account();
InBlock.gif        destination.Deposit(
150.00F);
InBlock.gif
InBlock.gif        source.TransferFunds(destination, 
100.00F);    // 转账
InBlock.gif
InBlock.gif        
// 利用 Nunit.Framework 中的 Assert 类看看转账以后两个账户的余额是否正确
InBlock.gif
        Assert.AreEqual(250.00F, destination.Balance);
InBlock.gif        Assert.AreEqual(
100.00F, source.Balance);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

然后编译一下,生成一个 exe 文件( 如果要生成 DLL 的话,更改一下这个这个项目的 Output Type属性,改成 Class Library就可以了。这个改动还是在Solution Explorer 窗口中,项目名上 右键--属性。 对于这个例子,生成DLL的话就不需要 Main() 方法了)。

    4)打开NUnit,File--Open,找到刚才编译生成的 exe。然后 Run,满眼可爱的绿色,就说明测试都成功了^_^。

testok.gif

    如果想看看测试失败的样子,可以把 Assert.AreEqual() 里面的值改一下……

例子中只用到了 Test Fixture 和 Test 这两个 Attribute, 其他更多的用法在 NUnit 文档中写得十分清楚,文档中也有些更好的例子……

自动化的单元测试有什么用? 答:省时省力。当一个系统需要测试的类/方法 成千上万时,手工的测试方法(用控制台打印出信息等等)的效率会比较低。

总结:NUnit 很好的利用了反射机制,单元测试十分方便。但是对于复杂的对象,写出低耦合的测试代码可能有一定难度,在这种情况下,MockObject 就能起到作用了,有关 .NET 下应用广泛的 NMock,待续……




 

转载于:https://www.cnblogs.com/anf/archive/2005/03/19/121796.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值