[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 按钮就能运行测试用例了。从下图可以看到失败的测试用例显示的比较详细的信息。

posted @ 2012-03-07 19:10  飘飘白云  阅读(2535)  评论(0编辑  收藏  举报
本博客遵循 Creative Commons License “署名-非商业用途-保持一致”创作共用协议。 与我联系