[C#]整合VS2010和NUnit

整合VS2010和NUnit

罗朝辉 (http://www.cnblogs.com/kesalin/)

本文遵循“署名-非商业用途-保持一致”创作公用协议

软件下载

.Net单元测试工具 NUnit下载:http://www.nunit.org/index.php?p=download,最新的为NUnit-2.6.0.12051.msi,下载安装。

VS2010 NUnit 整合插件 Visual Nunit 2010下载:http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099,下载安装完毕就能在 VS2010 的 view->Other Windows中看到 Visual Nunit了(或使用快捷键Ctrl + F7),打开该视图,将之拖到合适的位置。

 

测试程序

1,新建名为 NUnitSample 的C#控制台应用程序。在References中增加References:nunit.framework,路径默认为:C:\Program Files \NUnit 2.6\bin\framework\nunit.framewor.dll。

2,删除 Class1.cs,增加两个C#文件:NUnitSample.cs 和NUnitSampleTest.cs。其内容分别为:

// File: NUnitSample.cs
//
using System;

namespace NUnitSample
{
public class MathUtil
{
public int Add(int a, int b)
{
return a + b;
}

static void Main(string[] args)
{
}
}
}

// File: NUnitSampleTest.cs
//
using System;
using NUnit.Framework;

namespace NUnitSample
{
[TestFixture]
public class NUnitSampleTest
{
[Test]
public void AddTest()
{
MathUtil util = new MathUtil();
int result = util.Add(4, 4);
Assert.AreEqual(8, result);
}

[Test]
public void AddTestFailure()
{
MathUtil util = new MathUtil();
int result = util.Add(4, 4);
Assert.AreEqual(6, result);
}
}
}


在上面的代码中引用 NUnit.Framework,使用[TestFixture]表明这是用于测试的类,在其中使用 [Test]表示具体的测试用例,在这里添加了两个测试用例:一个成功的和一个失败的测试。

3,编译,然后点击 Visula Unit中的 Run 按钮就能运行测试用例了。从下图可以看到失败的测试用例显示的比较详细的信息。

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值