1、Net单元测试工具 NUnit下载:http://www.nunit.org/index.php?p=download,最新的为NUnit-2.6.0.12051.msi,下载安装。
或者115网盘下载,下载地址:http://115.com/file/beaatdiu
2、VS2010 NUnit 整合插件 Visual Nunit 2010下载:http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099/file/9701/9/VisualNunitPackage.vsix,下载安装完毕就能在 VS2010 的 view->Other Windows中看到 Visual Nunit了(或使用快捷键Ctrl + F7),打开该视图,将之拖到合适的位置。
1、新建名为 NUnitSample 的C#控制台应用程序。在References中增加References:nunit.framework,浏览打开:C:\Program Files \NUnit 2.6\bin\framework\nunit.framework.dll,确定即可。
2、删除 Class1.cs,增加两个C#文件:NUnitSample.cs 和NUnitSampleTest.cs。其内容分别为:
using System; using System.Collections.Generic; using System.Linq; using System.Text; //文件:NUnitSample.cs //被测试的源代码 namespace NUnitSample { public class MathUtil { public int Add(int a, int b) { return a + b; } static void Main(string[] args) { } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; //文件:NUnitSampleTest.cs 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 按钮就能运行测试用例了。从下图可以看到失败的测试用例显示的比较详细的信息。
view->Other Windows中的 Visual Nunit(或使用快捷键Ctrl + F7),打开该视图;
选中Case(用例)点击“Run”即可看到一个成功,一个失败。