NUnit使用详解(二)
转载:http://hi.baidu.com/grayworm/item/39aa11c5d9375d56bdef6990
五:常用断言
在NUnit中,断言是单元测试的核心。NUnit提供了一组丰富的断言,这些断言是Assert类的静态方法。如果一个断言失败,方法的调用不会返回值,并且会报告一个错误。如果一个测试包含多个断言,那些紧跟失败断言的断言都不会执行,因此,通常每个测试方法最好只有一个断言。
Assert类提供了最常用的断言。我们将Assert方法按如下分组:
a.同等(Equality)断言
b.一致性(Identity)断言
c.比较(Comparison)断言
d.类型(Type)断言
e.条件(Condition)测试
f.工具(Utility)方法
1.同等断言
主要包括Assert.AreEqual()、Assert.AreNotEqual()和Assert.IsNaN()
前两个方法测试2个参数是否相等。重载的方法支持普通的值类型。
Assert.AreEqual( int expected, int actual );
Assert.AreEqual( int expected, int actual, string message );
Assert.AreEqual( int expected, int actual, string message,params object[] parms );
Assert.AreEqual( uint expected, uint actual );
Assert.AreEqual( uint expected, uint actual, string message );
Assert.AreEqual( uint expected, uint actual, string message,params object[] parms );
Assert.AreEqual( decimal expected, decimal actual );
Assert.AreEqual( decimal expected, decimal actual, string message );
Assert.AreEqual( decimal expected, decimal actual, string message,params object[] parms );
Assert.AreEqual( float expected, float actual, float tolerance );
Assert.AreEqual( float expected, float actual, float tolerance,string message );
Assert.AreEqual( float expected, float actual, float tolerance,string message, params object[] parms );
Assert.AreEqual( double expected, double actual, double tolerance );
Assert.AreEqual( double expected, double actual, double tolerance,string message );
Assert.AreEqual( double expected, double actual, double tolerance,string message, params object[] parms );
Assert.AreEqual( object expected, object actual );
Assert.AreEqual( object expected, object actual, string message );
Assert.AreEqual( object expected, object actual, string message,params object[] parms );
Assert.AreNotEqual( int expected, int actual );
Assert.AreNotEqual( int expected, int actual, string message );
Assert.AreNotEqual( int expected, int actual, string message,params object[] parms );
Assert.AreNotEqual( uint expected, uint actual );
Assert.AreNotEqual( uint expected, uint actual, string message );
Assert.AreNotEqual( uint expected, uint actual, string message,params object[] parms );
Assert.AreNotEqual( decimal expected, decimal actual );
Assert.AreNotEqual( decimal expected, decimal actual, string message );
Assert.AreNotEqual( decimal expected, decimal actual, string message,params object[] parms );
Assert.AreNotEqual( float expected, float actual );
Assert.AreNotEqual( float expected, float actual, string message );
Assert.AreNotEqual( float expected, float actual, string message,params object[] parms );
Assert.AreNotEqual( double expected, double actual );
Assert.AreNotEqual( double expected, double actual, string message );
Assert.AreNotEqual( double expected, double actual, string message,params object[] parms );
Assert.AreNotEqual( object expected, object actual );
Assert.AreNotEqual( object expected, object actual, string message );
Assert.AreNotEqual( object expected, object actual, string message,params object[] parms );
不同类型的数值可以按期望的那样进行比较。下面的断言会成功:
Assert.AreEqual( 5, 5.0 );
float型和double型的数值通常使用一个附加参数来进行比较,这个参数代表一个误差,在这个误差范围内,它们视为相等。
如果2个一维数组有相同的长度,而且相应的数组元素也相等,那么通过调用Assert.AreEqual方法,这2个数组视为相等。
注: 多维数组,嵌套数组(数组的数组),以及其他集合类型,例如ArrayList目前还不支持。
2.一致性断言
Assert.AreSame()方法、Assert.AreNotSame方法。这两个方法主要判断两个参数引用的是否是同一个对象。
Assert.AreSame( object expected, object actual );
Assert.AreSame( object expected, object actual, string message );
Assert.AreSame( object expected, object actual, string message,params object[] parms );
Assert.AreNotSame( object expected, object actual );
Assert.AreNotSame( object expected, object actual, string message );
Assert.AreNotSame( object expected, object actual, string message,params object[] parms );
Assert.Contains()方法用来测试在一个数组或列表里是否包含该对象。
Assert.Contains( object anObject, IList collection );
Assert.Contains( object anObject, IList collection,string message );
Assert.Contains( object anObject, IList collection,string message, params object[] parms );
3.比较断言
Assert.Greater():测试一个对象是否大于另外一个。
Assert.Less():测试一个对象是否于小另外一个。
Assert.Greater( int arg1, int arg2 );
Assert.Greater( int arg1, int arg2, string message );
Assert.Greater( int arg1, int arg2, string message,object[] parms );
Assert.Greater( uint arg1, uint arg2 );
Assert.Greater( uint arg1, uint arg2, string message );
Assert.Greater( uint arg1, uint arg2, string message,object[] parms );
Assert.Greater( decimal arg1, decimal arg2 );
Assert.Greater( decimal arg1, decimal arg2, string message );
Assert.Greater( decimal arg1, decimal arg2, string message,object[] parms );
Assert.Greater( double arg1, double arg2 );
Assert.Greater( double arg1, double arg2, string message );
Assert.Greater( double arg1, double arg2, string message,object[] parms );
Assert.Greater( double arg1, double arg2 );
Assert.Greater( double arg1, double arg2, string message );
Assert.Greater( double arg1, double arg2, string message,object[] parms );
Assert.Greater( float arg1, float arg2 );
Assert.Greater( float arg1, float arg2, string message );
Assert.Greater( float arg1, float arg2, string message,object[] parms );
Assert.Greater( IComparable arg1, IComparable arg2 );
Assert.Greater( IComparable arg1, IComparable arg2, string message );
Assert.Greater( IComparable arg1, IComparable arg2, string message,object[] parms );
Assert.Less( int arg1, int arg2 );
Assert.Less( int arg1, int arg2, string message );
Assert.Less( int arg1, int arg2, string message,object[] parms );
Assert.Less( uint arg1, uint arg2 );
Assert.Less( uint arg1, uint arg2, string message );
Assert.Less( uint arg1, uint arg2, string message,object[] parms );
Assert.Less( decimal arg1, decimal arg2 );
Assert.Less( decimal arg1, decimal arg2, string message );
Assert.Less( decimal arg1, decimal arg2, string message,object[] parms );
Assert.Less( double arg1, double arg2 );
Assert.Less( double arg1, double arg2, string message );
Assert.Less( double arg1, double arg2, string message,object[] parms );
Assert.Less( float arg1, float arg2 );
Assert.Less( float arg1, float arg2, string message );
Assert.Less( float arg1, float arg2, string message,object[] parms );
Assert.Less( IComparable arg1, IComparable arg2 );
Assert.Less( IComparable arg1, IComparable arg2, string message );
Assert.Less( IComparable arg1, IComparable arg2, string message,object[] parms );
4.类型断言
Assert.IsInstanceOfType():判断一个对象的类型是否是期望的类型
Assert.IsNotInstanceOfType():判断一个对象的类型是否不是期望的类型
Assert.IsAssignableFrom():判断一个对象的类型是否属于某种类型
Assert.IsNotAssignableFrom():判断一个对象的类型是否不属于某种类型
Assert.IsInstanceOfType( Type expected, object actual );
Assert.IsInstanceOfType( Type expected, object actual,string message );
Assert.IsInstanceOfType( Type expected, object actual,string message, params object[] parms );
Assert.IsNotInstanceOfType( Type expected, object actual );
Assert.IsNotInstanceOfType( Type expected, object actual,string message );
Assert.IsNotInstanceOfType( Type expected, object actual,string message, params object[] parms );
Assert.IsAssignableFrom( Type expected, object actual );
Assert.IsAssignableFrom( Type expected, object actual,string message );
Assert.IsAssignableFrom( Type expected, object actual,string message, params object[] parms );
Assert.IsNotAssignableFrom( Type expected, object actual );
Assert.IsNotAssignableFrom( Type expected, object actual,string message );
Assert.IsNotAssignableFrom( Type expected, object actual,string message, params object[] parms );
5.条件测试断言
这些方法测试并把测试的值作为他们的第一个参数以及把一个消息作为第二个参数,第二个参数是可选的。本文提供了下面的方法:
Assert.IsTrue( bool condition );
Assert.IsTrue( bool condition, string message );
Assert.IsTrue( bool condition, string message, object[] parms );
Assert.IsFalse( bool condition);
Assert.IsFalse( bool condition, string message );
Assert.IsFalse( bool condition, string message, object[] parms );
Assert.IsNull( object anObject );
Assert.IsNull( object anObject, string message );
Assert.IsNull( object anObject, string message, object[] parms );
Assert.IsNotNull( object anObject );
Assert.IsNotNull( object anObject, string message );
Assert.IsNotNull( object anObject, string message, object[] parms );
Assert.IsNaN( double aDouble );
Assert.IsNaN( double aDouble, string message );
Assert.IsNaN( double aDouble, string message, object[] parms );
Assert.IsEmpty( string aString );
Assert.IsEmpty( string aString, string message );
Assert.IsEmpty( string aString, string message,params object[] args );
Assert.IsNotEmpty( string aString );
Assert.IsNotEmpty( string aString, string message );
Assert.IsNotEmpty( string aString, string message,params object[] args );
Assert.IsEmpty( ICollection collection );
Assert.IsEmpty( ICollection collection, string message );
Assert.IsEmpty( ICollection collection, string message,params object[] args );
Assert.IsNotEmpty( ICollection collection );
Assert.IsNotEmpty( ICollection collection, string message );
Assert.IsNotEmpty( ICollection collection, string message,params object[] args );
6.StringAssert断言
StringAssert.Contains( string expected, string actual );
StringAssert.Contains( string expected, string actual,string message );
StringAssert.Contains( string expected, string actual,string message, params object[] args );
StringAssert.StartsWith( string expected, string actual );
StringAssert.StartsWith( string expected, string actual, string message );
StringAssert.StartsWith( string expected, string actual,string message, params object[] args );
StringAssert.EndsWith( string expected, string actual );
StringAssert.EndsWith( string expected, string actual,string message );
StringAssert.EndsWith( string expected, string actual,string message, params object[] args );
StringAssert.AreEqualIgnoringCase( string expected, string actual );
StringAssert.AreEqualIgnoringCase( string expected, string actual,string message );
StringAssert.AreEqualIgnoringCase( string expected, string actual,string message params object[] args );
7.实用方法
Fail()和Ignore(),是为了让我们对测试过程有更多的控制:
Assert.Fail();
Assert.Fail( string message );
Assert.Fail( string message, object[] parms );
Assert.Ignore();
Assert.Ignore( string message );
Assert.Ignore( string message, object[] parms );
Assert.Fail方法为你提供了创建一个失败测试的能力:
public void AssertStringContains( string expected, string actual )
{
AssertStringContains( expected, actual, string.Empty );
}
public void AssertStringContains( string expected, string actual,string message )
{
if ( actual.IndexOf( expected ) < 0 )
Assert.Fail( message );
}
Assert.Ignore方法为你提供在运行时动态忽略一个测试或者一个测试套件(suite)的能力。它可以在一个测试,一个setup或fixture setup的方法中调用。我们建议你只在无效的案例中使用。它也为更多扩展的测试包含或排斥提供了目录能力,或者你可以简单将不同情况下运行的测试运行分解到不同的程序集。
NUnit使用详解(二)的更多相关文章
- .NET DLL 保护措施详解(二)关于性能的测试
先说结果: 加了缓存的结果与C#原生代码差异不大了 我对三种方式进行了测试: 第一种,每次调用均动态编译 第二种,缓存编译好的对象 第三种,直接调用原生C#代码 .net dll保护系列 ------ ...
- PopUpWindow使用详解(二)——进阶及答疑
相关文章:1.<PopUpWindow使用详解(一)——基本使用>2.<PopUpWindow使用详解(二)——进阶及答疑> 上篇为大家基本讲述了有关PopupWindow ...
- Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)
[Android布局学习系列] 1.Android 布局学习之——Layout(布局)详解一 2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数) 3.And ...
- logback -- 配置详解 -- 二 -- <appender>
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- 爬虫入门之urllib库详解(二)
爬虫入门之urllib库详解(二) 1 urllib模块 urllib模块是一个运用于URL的包 urllib.request用于访问和读取URLS urllib.error包括了所有urllib.r ...
- [转]文件IO详解(二)---文件描述符(fd)和inode号的关系
原文:https://www.cnblogs.com/frank-yxs/p/5925563.html 文件IO详解(二)---文件描述符(fd)和inode号的关系 ---------------- ...
- Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)
View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇 ...
- HTTPS详解二:SSL / TLS 工作原理和详细握手过程
HTTPS 详解一:附带最精美详尽的 HTTPS 原理图 HTTPS详解二:SSL / TLS 工作原理和详细握手过程 在上篇文章HTTPS详解一中,我已经为大家介绍了 HTTPS 的详细原理和通信流 ...
- Linux dts 设备树详解(二) 动手编写设备树dts
Linux dts 设备树详解(一) 基础知识 Linux dts 设备树详解(二) 动手编写设备树dts 文章目录 前言 硬件结构 设备树dts文件 前言 在简单了解概念之后,我们可以开始尝试写一个 ...
- pika详解(二) BlockingConnection
pika详解(二) BlockingConnection 本文链接:https://blog.csdn.net/comprel/article/details/94592348 版权 Blocki ...
随机推荐
- 在User Profile Service中配置AD的同步连接
转:http://www.360sps.com/Item/ConfigureSynchronizationConnections.aspx 如果要将Active Directory.LDAP 目录和业 ...
- SQL Server查询性能优化——覆盖索引(一)
覆盖索引又可以称为索引覆盖. 解释一: 就是select的数据列只用从索引中就能够取得,不必从数据表中读取,换句话说查询列要被所使用的索引覆盖. 解释二: 索引是高效找到行的一个方法,当能通过检索索引 ...
- SSAS数据挖掘算法简介
决策树分析算法:以二叉树的形式展现,分析出影响某种行为(如购买自行车)的因素,并对这些因素排序. 聚类分析算法:物以类聚,人以群分.分析特定群体所共同含有的属性(因素). 未完,待续..
- oracle创建库和表
SQL> create user midamtemp identified by ty1234 default tablespace midamtemp; User created. --分配用 ...
- poj 2528 线段树 离散化的小技巧
题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...
- HUD-4602 Partition 排列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4602 把n等效为排成一列的n个点,然后就是取出其中连续的k个点.分两种情况,一种是不包含两端,2^( ...
- rx tx
- php5.5 yum源
PHP官网在下载页面中只有源代码下载,当然使用编译安装也是可以的,但是使用二进制包安装会非常快速.虽然PHP官网没有提供二进制安装包,但是它推荐了两个第三方的YUM源(CentOS中默认的YUM安装的 ...
- Redis_基本类型介绍和指令___2
1.hash Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿). ...
- jQuery EasyUI, datagrid, treegrid formatter 参数比较 row index
如题: datagrid中,见官方文档: formatter function The cell formatter function, take three parameter:value: the ...