使用NUnit进行DotNet程序测试
作者:kongxx
介绍
NUnit是目前比较流行的.Net平台的测试工具,以下就简单介绍一下他的开发。
准备
要使用NUnit,首先要确保您的机器上有NUnit的开发包,您可以从http://www.nunit.org/
地方获取并安装(目前版本是NUnit v2.1.91)。正确安装后会在开始菜单下添加一个NUnit 2.2项目。
属性说明
在开始写例子之前,先把NUnit的属性说明一下:
TestFixture (NUnit2.0)
标识当前类是一个包含测试方法的类。
注意:这个类必须有一个默认的构造方法,并且也必须声明成Public。
例如
namespace NUnit.Tests {<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
// ...
} }
|
Test (NUnit2.0)
标识一个使用TestFixture标识的类中的方法是一个需要测试的方法。
注意:方法的签名被标识为无返回值。
例如:
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[Test]
public void Add()
{ /* ... */ }
public void TestSubtract()
{ /* backwards compatibility */ }
}
}
|
SetUp/TearDown
TestFixtureSetUp/SetUp用来在运行测试方法之前构造环境;
TestFixtureTearDown/TearDown用来在运行测试方法之后还原环境。
注意:一个测试类(标识TestFixture)中只可以有一对标记。
TestFixtureSetUp/TestFixtureTearDown (NUnit2.1)
例如:
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[TestFixtureSetUp] public void Init()
{ /* ... */ }
[TestFixtureTearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
|
SetUp/TearDown (NUnit2.0)
例如:
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[SetUp] public void Init()
{ /* ... */ }
[TearDown] public void Dispose()
{ /* ... */ }
[Test] public void Add()
{ /* ... */ }
}
}
|
ExpectedException (NUnit2.0)
指定一个测试将要抛出的异常。
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void ExpectAnException()
{ /* ... */ }
} }
|
Category (NUnit2.2)
标识在一组测试中选中的测试。
当使用此属性是,只有选中的Category才会被调用。
例如:
在TestFixture上使用Category属性
namespace NUnit.Tests{
using System;
using NUnit.Framework;
[TestFixture]
[Category("LongRunning")]
public class LongRunningTests
{/*…*/}
}
|
在Test上使用Category属性
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[Test]
[Category("Long")]
public void VeryLongTest()
{ /* ... */ } }
|
Explicit(NUnit2.2)
指定一个Test或TestFixture被排除在测试选中的测试中。
例如:
在TestFixture上使用Explicit属性
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture, Explicit]
public class ExplicitTests
{/* ... */}
}
|
在Test上使用Explicit属性
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
public class SuccessTests {
[Test, Explicit]
public void ExplicitTest()
{ /* ... */ }
}
|
Suite (NUnit2.0)
标识一个测试单元。
????
例如:
namespace NUnit.Tests {
using System;
using NUnit.Framework;
public class AllTests {
[Suite]
public static TestSuite Suite {
get {
TestSuite suite = new TestSuite("All Tests");
suite.Add(new OneTestCase());
suite.Add(new Assemblies.AssemblyTests());
suite.Add(new AssertionTest());
return suite;
}
}
} }
|
Ignore(NUnit2.0)
在一定的时间内标识Test或TestFixture不运行。
例如:
namespace NUnit.Tests {
using System;
using NUnit.Framework;
[TestFixture]
[Ignore("Ignore a fixture")]
public class SuccessTests
{/* ... */} }
|
简单例子
首先用VS.Net建立一个控制台应用程序项目(TestNUnit),然后在项目中添加引用,选中nunit.framework(可以在NUnit的安装目录下找到),然后添加两个类,一个是待测试的类,一个是测试类,
待测试的类内容如下:
using System; namespace TestNUnit.Sample1 { public class Sample1 { public Sample1() { } public String GetHelloWorld() { return "Hello World!"; } public int Add(int i1 ,int i2) { return i1 + i2 ; } public int Minus(int i1 ,int i2) { return i1 - i2 ; } } }
|
测试类内容如下:
using System; using NUnit.Framework; namespace TestNUnit.Sample1 { [TestFixture] //--------------------------------------------1 public class TestSample1 { private Sample1 sample ; [SetUp] //--------------------------------------------2 public void Init() { this.sample = new Sample1();; } [TearDown] //--------------------------------------------3 public void TearDown() { this.sample = null ; } [Test] //--------------------------------------------4 public void TestGetHelloWorld() { Assert.IsNotNull(this.sample.GetHelloWorld()); Assert.AreEqual("Hello World!" ,this.sample.GetHelloWorld()); } [Test] public void TestAdd() { Assert.AreEqual(2,this.sample.Add(1,1)); Assert.AreEqual(3,this.sample.Add(1,2)); Assert.AreEqual(4,this.sample.Add(2,2)); } [Test] public void TestMinus() { Assert.AreEqual(0,this.sample.Minus(1,1)); Assert.AreEqual(1,this.sample.Minus(2,1)); Assert.AreEqual(-1,this.sample.Minus(1,2)); } } }
|
注意测试类(TestSample1)中几个特殊的地方:
1表示当前类是一个测试类;
2表示测试启动前要执行的操作;
3表示测试后要执行的操作;
4表示具体的每个测试方法,这里每个方法都对应要测试类中的方法。
编译以上类,运行NUnit的GUI界面(开始->NUnit 2.2->Nunit-Gui),选择File->Open,打开刚才编译项目生成的文件,这里选中TestNUnit.exe(根据具体应用可以是DLL或者别的类型)文件,此时出现一下窗口:
点击Run按钮,出现以下界面:
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype>
当运行栏全部是绿色的时候,表示写的测试全部通过,如果出现运行栏显示红色,表示测试出现问题,需要我们修改。
此时表示有两个方法(TestAdd,TestMinus)测试出现问题,需要我们去检查修改,然后重复以上的操作,直到运行栏全部不在显示红色为止。
Mock测试
简介
Mock测试就是在测试过程中,对于某个不容易构造或获取的对象,用一个虚假的对象来创建以方便测试的测试方法。
要使用Mock测试,需要有专门的Mock测试开发包支持,以下使用Nmock来说明。要获得Nmock可以从http://www.nmock.org/获得,目前使用NMock V1.1。
示例
添加引用
在项目中添加引用nmock.dll,可以从http://www.nmock.org/获得。
代码
需要被Mock的类:
using System; namespace TestNUnit.MockSample { public class BigClass { public virtual string DoSoming(string hello,string name,string symbol){ return "hello" + " " + name + symbol; } public virtual void DoNoing() { //TODO } } }
|
被测试的类:
using System; namespace TestNUnit.MockSample { public class Sample { private BigClass bc ; public Sample(BigClass bc) { this.bc = bc ; } public string GetHelloWorld() { return "Hello World!"; } public string GenerateHelloWorld(string hello,string name ,string symbol) { return this.bc.DoSoming("Hello",name,symbol); } } }
|
测试类:
using System; using NUnit.Framework; using NMock ; using NMock.Constraints; namespace TestNUnit.MockSample { [TestFixture] public class TestMockSample { private Mock mock ; private Sample sample ; [SetUp] public void Init() { this.mock = new DynamicMock(typeof(BigClass)); this.sample = new Sample((BigClass)mock.MockInstance); } [TearDown] public void TearDown() { mock.Verify(); } [Test] public void TestGetHelloWorld() { this.mock.ExpectNoCall("DoSoming", typeof(String),typeof(String),typeof(String)); this.mock.ExpectNoCall("DoNoing"); Assert.IsNotNull(this.sample.GetHelloWorld()); Assert.AreEqual(this.sample.GetHelloWorld() ,"Hello World!" ); } [Test] public void TestGenerateHelloWorld() { IsEqual hello = new IsEqual("Hello"); IsAnything name = new IsAnything(); IsEqual symbol = new IsEqual("!"); this.mock.ExpectAndReturn("DoSoming","Hello kongxx!" ,hello, name, symbol); this.mock.ExpectNoCall("DoNoing"); Assert.AreEqual("Hello kongxx!",sample.GenerateHelloWorld("Hello","kongxx","!")); } } }
|
运行
编译以上类,然后运行NUnit的图形界面,载入编译过的程序(exe或dll),出现以下界面:
然后运行,出现以下界面:
如果运行栏全部显示绿色表示通过测试,否则检查错误,修改,编译,运行直到全部运行成功为止。
参考资料
1NUnit http://www.nunit.org/
2Nmock http://www.nmock.org/index.html
3mockobjects http://www.mockobjects.com/FrontPage.html
分享到:
相关推荐
除了运行单个的程序集之外,NUnit也支持由多个程序集组织的测试,也提供把测试作为NUnit测试工程进行创建和运行。 对于那些安装Visual Studio的并在Windows系统使用NUnit的用户来说,Visual Studio支持是可用的。
这两个许可证都允许在不受限制的免费和商业应用程序和库中使用NUnit。NUnit项目NUnit由几个项目组成。 报告问题时,请尝试报告正确项目中的问题。核心项目-用于编写NUnit测试的测试框架(此存储库) -从命令行运行...
NUnit是所有.NET语言的单元测试框架。 最初是从JUnit移植的,当前的生产版本3已被完全重写,具有许多新功能并支持广泛的.NET平台。 目录 资料下载 NUnit控制台的最新稳定版本 , ,也可以。 预发布版本。 控制台/...
NUnit:编写单元测试的工具 NDoc:创建代码文档的工具 NAnt:生成解决方案的工具 CodeSmith:代码生成工具 FxCop:用于监视代码的——代码警察 Snippet Compiler:小型代码段编译工具 两个...
NUnit:编写单元测试的工具 NDoc:创建代码文档的工具 NAnt:生成解决方案的工具 CodeSmith:代码生成工具 FxCop:用于监视代码的——代码警察 Snippet Compiler:小型代码段编译工具 两个不同的转换器工具,...
NUnit:编写单元测试的工具 NDoc:创建代码文档的工具 NAnt:生成解决方案的工具 CodeSmith:代码生成工具 FxCop:用于监视代码的——代码警察 Snippet Compiler:小型代码段编译工具 两个不同的转换器工具,...
dotnet新的nunit -o myPro.myCoreTest 新建测试项目 dotnet sln添加myPro.App 向解决方案增加项目 dotnet添加myPro.App参考myPro.Core 增加项目引用 dotnet添加软件包Newtonsoft.Json 在项目增加包引用 dotnet运行-p...
dotnet GUI测试自动化平台一个非常简单的平台,可在WinForms,WPF,GTK#和Xamarin.Mac应用程序上实施GUI测试。 单击以下图像观看该系统工作的视频!动机这是我们内部用于开发Plastic SCM的自动化GUI测试系统,我们...
网络 这是一个.NET / C#库,可让您轻松地在自己的C#应用程序中集成TFTP客户端或TFTP服务器。 如果您正在寻找成熟的GUI客户端,则可能应该考虑其他...使用NUnit进行单元测试的代码 样本TFTP服务器 样本TFTP客户端
使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的...
医疗系统是使用ASP.NET 5和EF来记录医疗记录的小型应用程序。 用户可以使用其Web浏览器在此应用程序中存储医生,患者和咨询信息。 这是我们如何将微服务与ASP.NET 5结合使用的概念证明。 这受 CI CD状态 模块 地位 ...
对于前者,我先进行压缩,然后再备份之前对其进行加密(省略了加密技术)。 对于后者,我创建一个zip,其中省略了bin和obj文件夹,然后将其备份。 输入来自文本文件SourceFolderPaths.txt,该文件包含需要备份的...
魔法 ...MAGES本身没有任何依赖关系,但是,测试依赖于NUnit,基准测试使用BenchmarkDotNet。 通常,应通过NuGet软件包源安装MAGES。 如果这对您不起作用,请克隆源并自己构建MAGES。 确保所有单元测试
Nuget发布版本 贡献 该项目采用了。 有关更多信息,请参见或与联系,并提出其他任何问题或意见。...针对测试程序集执行NUnit控制台运行程序 PM> Install-Package Azure.Data.Wrappers 查看Wiki以了解如何使用它。
在为应用程序编写测试时,通常希望避免访问数据库。 扩展允许您通过创建上下文(具有测试定义的行为)来实现此目的,该上下文利用内存中的数据。我什么时候应该使用它? 如果您有类似以下代码的内容: var query = _...