如何在.NET中应用NUnit

Tia ·
更新时间:2024-09-21
· 966 次阅读

条件:安装NUnit,默认安装即可

第1步:创建一个class,增加一个NUnit框架引用到工程

在Microsoft Visual Studio .NET里创建这个例子时,你需要增加一个NUnit.framework.dll引用,

具体为:

1、 右键点击reference->add reference->选择nunit.framework,

2、 然后在webTest文件中添加一个引用:using NUnit.Framework。当然,添加引用的前提是已经安装了NUnit了

3、在工程中创建一个App.config,如下

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <configSections>     <sectionGroup name="NUnit">       <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>     </sectionGroup>   </configSections>   <NUnit>     <TestRunner>       <!-- Valid values are STA,MTA. Others ignored. -->       <add key="ApartmentState" value="STA" />     </TestRunner>   </NUnit> </configuration>

第2步.为工程加一个类.

为工程加一个NumbersFixture类。这里是这个例子的代码。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework;

namespace number {    [TestFixture]     public class Numbertest     {        [Test]         public void AddOneNumber()         {             int a = 1;             int b = 2;             int sum = a + b;             Assert.AreEqual(sum, 3);             Console.WriteLine(sum);                    }     } }

[TestFixture]是NUnit的一个重要的属性,意思是这个类包含了测试代码。

[Test]是需要测试的方法

第3步:用NUnit运行

直接打开NUnit运行该dll文件或者建立你的Visual Studio工程,运用NUnit测试

一、直接打开NUnit运行该dll文件 打开NUnit,点击file->open,选择刚刚编译的dll文件,这样把文件加载到NUnit了,在它的界面的左边,我们可以看到刚才编写的测试函数AddOneNumber,选择工程项目,或是文件,或是测试函数,然后点击界面右边的Run,如果一路绿灯,OK,测试通过。

配置WAtin和NUnit一起使用,框架配置如下: 按上面步骤配置NUnit后,配置Watin 1:在工程中,References处右键选择Add Reference,Browse选择WatiN安装目录" WatiN-2.0.20.1089-net-2.0in"下的WatiN.Core.dll 2:添加一个引用:usingWatiN.Core;   如图

此时完成配置了。可以用NUnit当控制器运行watin脚本了。



net .NET nunit

需要 登录 后方可回复, 如果你还没有账号请 注册新账号