首页 技术 正文
技术 2022年11月16日
0 收藏 378 点赞 2,122 浏览 1809 个字

Nunit是什么

  Nunit是一种TDD工具,和Junit一样的!


怎么样获取Nunit

  打开Visual Studio工具,然后在菜单栏中的工具->扩展管理器中的联机库中搜索”nunit”(必须要能够连接外网),然后点击安装就可以了,比较方便的。

Nunit测试工具使用


Nunit使用体验

  创建类库PlugInUnitTest,引用nunit.framework.dll,修改Class1.cs为TestFactory.cs,在类中添加特性[TestFixture],在类中添加特性[Test]

Nunit测试工具使用

  编译类库。

  如果没有看到Visual NUnit,可以使用Ctl+F7打开该界面。选择该实例之后可以点击Run按钮执行。


Nunit基础知识

[TestFixture]表示:类包含了测试代码(这个特性可以被继承)。这个类必须是公有的,这个类还必须有一个默认构造函数。

  [Test]表示它是一个测试方法。测试方法的返回值必须为void并且不能带有参数

  [SetUp]属性:用来标识方法,在开始所有测试之前执行,用来在测试前初始化一些资源,比如初始化类。

[TearDown]属性:用来标识方法,在所有测试完成之后执行,用来释放一些资源。

  [Ignore]属性:用来标识方法,指示这个方法由于某些原因暂时不需要测试(比如没有完成相关代码)


Nunit常用类和方法 

  Assert(断言):

  如果断言失败,方法将没有返回,并且报告一个错误。

  如果一个方法中包括了多个断言,在失败的断言之后的所有断言将不会被执行。基于这个原因,最好是为每个测试的断言使用try语句。

  1、测试二个参数是否相等

  Assert.AreEqual( int expected, int actual );
  Assert.AreEqual( decimal expected, decimal actual );
  。。。。

  2、测试二个参数是否引用同一个对象

  Assert.AreSame( object expected, object actual );
  Assert.AreNotSame( object expected, object actual );

  3、测试一个对象是否被一个数组或列表所包含

  Assert.Contains( object anObject, IList collection );

  比较断言:

  4、测试一个对象是否大于另一个对象

  Assert.Greater( int arg1, int arg2 );

  5、测试一个对象是否小于另一个对象

  Assert.Less( int arg1, int arg2 );

  类型断言:

  Assert.IsInstanceOfType( Type expected, object actual );

  条件测试:

  Assert.IsTrue( bool condition );
  Assert.IsFalse( bool condition);
  Assert.IsNull( object anObject );
  Assert.IsNotNull( object anObject );
  Assert.IsNaN( double aDouble );
  Assert.IsEmpty( string aString );
  Assert.IsNotEmpty( string aString );
  Assert.IsEmpty( ICollection collection );
  Assert.IsNotEmpty( ICollection collection );

  字符串断言(StringAssert):提供了许多检验字符串值的有用的方法

  StringAssert.Contains( string expected, string actual );
  StringAssert.StartsWith( string expected, string actual );
  StringAssert.EndsWith( string expected, string actual );
  StringAssert.AreEqualIgnoringCase( string expected, string actual );    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851