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 ...
随机推荐
- DNS----域名解析系统
DNS就是域名解析系统,它可以将IP转换成域名,也可以将域名转换成IP 1. 安装DNS服务 开始—〉设置—〉控制面板—〉添加/删除程序—〉添加/删除Windows组件—〉“网络服务”—〉 ...
- 在内部架设NuGet服务器
在公司内部有很多基础框架或者基础组件,甚至对于使用SOA架构的公司来说,会有大量的业务组件的契约程序集,对于这些框架或组件的引用管理有的人使用源代码管理工具,但是NuGet相比源代码管理工具更方便: ...
- [codevs1380]没有上司的舞会
本题地址 http://www.luogu.org/problem/show?pid=1352 http://codevs.cn/problem/1380/ 题目描述 某大学有N个职员,编号为1~N. ...
- C#学习:集合、迭代、泛型(1)
一.System.Collections名称空间下几个接口表征着集合的功能: 1.IEnumerable:表征着迭代功能 public interface IEnumerable { IEnumera ...
- 12款有助于简化CSS3开发的工具
网站开发者能通过CSS3为网站设计增添很多时尚元素,CSS3 对CSS规范做了很大的改进.现在,本文将介绍12款有助于简化CSS3开发的工具. 1.CSS3 Pie: 允许在IE上使用CSS3绝大部 ...
- ACM编程技巧--常用字符操作函数
字符串与基本数据类型的转换 int sscanf(buff,"%d%d",&a,&b); //返回值是参数个数 int sprintf(buff,"%d% ...
- HW3.5
import java.util.Scanner; public class Solution { public static void main(String[] args) { int n1 = ...
- HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索
转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5568822.html 第一次遇到迷宫搜索,给我的感觉是十分惊喜的:搞懂这个的话,感觉自己又掌握了一项技能~ 个人 ...
- WPF布局系统[转]
转自:http://www.cnblogs.com/niyw/archive/2010/10/31/1863908.html前言 前段时间忙了一阵子Google Earth,这周又忙了一阵子架构师论文 ...
- NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)"
原文: http://stackoverflow.com/questions/19874935/afnetworking-2-0-post-issue-cocoa-error-3840json-tex ...