单元测试之NUnit三
NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档。初次写博客,望大家指点。
导航:
单元测试之NUnit一
单元测试之NUnit二
单元测试之NUnit三
除了Assert断言外,NUnit还提供了其它直接断言的方法,比如string、directory、file等。另外,如有需要还可自行扩展。
String Assert
实现了string 类型的断言,包含以下方法:
public class StringAssertTest
{
[Test]
public void Contains_Test()
{
string expected = "a";//期望值。被包含
string actual = "acb";//真是值。包含
StringAssert.Contains(expected, actual);
expected = "d";
StringAssert.DoesNotContain(expected, actual);
}
[Test]
public void StartsWith_Test()
{
string actual = "dcc";
string expected = "d";
StringAssert.StartsWith(expected, actual);
expected = "c";
StringAssert.DoesNotStartWith(expected, actual);
}
[Test]
public void EndsWith_Test()
{
string actual = "dcc";
string expected = "c";
StringAssert.EndsWith(expected, actual);
expected = "d";
StringAssert.DoesNotEndWith(expected, actual);
}
[Test]
public void EqualIgnoringCase_Test()
{
string actual = "adc";
string expected = "adc";
StringAssert.AreEqualIgnoringCase(expected, actual);
expected = "a";
StringAssert.AreNotEqualIgnoringCase(expected, actual);
}
[Test]
public void Match_Test()
{
string actual = "adc";
string expected = "adc";
StringAssert.IsMatch(expected, actual);
expected = "g";
StringAssert.DoesNotMatch(expected, actual);
}
}
Collection Assert
CollectionAssert 提供为实现了IEnumerable的集合比较提供一系列方法:
[Test]
public void Equal_Test()
{
List<int> expected = new List<int> { 1, 2, 3 };
List<int> actual = new List<int> { 2, 3, 1 };
CollectionAssert.AreNotEqual(expected, actual);//集合元素位置不同,不相等
actual = new List<int> { 1, 2, 3 };
CollectionAssert.AreEqual(expected, actual);
}
[Test]
public void ItemsOfType_Test()
{
List<object> cols = new List<object> { "1", "2", "b" };
CollectionAssert.AllItemsAreInstancesOfType(cols, typeof(string));
}
更多方法如下:
CollectionAssert.AllItemsAreInstancesOfType(IEnumerable collection, Type expectedType);
CollectionAssert.AllItemsAreInstancesOfType(
IEnumerable collection, Type expectedType, string message, params object[] args);
CollectionAssert.AllItemsAreNotNull(IEnumerable collection);
CollectionAssert.AllItemsAreNotNull(
IEnumerable collection, string message, params object[] args);
CollectionAssert.AllItemsAreUnique(IEnumerable collection);
CollectionAssert.AllItemsAreUnique(
IEnumerable collection, string message, params object[] args);
CollectionAssert.AreEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEqual(
IEnumerable expected, IEnumerable actual, string message, params object[] args);
CollectionAssert.AreEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreEquivalent(
IEnumerable expected, IEnumerable actual, string message, params object[] args);
CollectionAssert.AreNotEqual(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEqual(
IEnumerable expected, IEnumerable actual, string message, params object[] args);
CollectionAssert.AreNotEquivalent(IEnumerable expected, IEnumerable actual);
CollectionAssert.AreNotEquivalent(
IEnumerable expected, IEnumerable actual, string message, params object[] args);
CollectionAssert.Contains(IEnumerable expected, object actual);
CollectionAssert.Contains(
IEnumerable expected, object actual, string message, params object[] args);
CollectionAssert.DoesNotContain(IEnumerable expected, object actual);
CollectionAssert.DoesNotContain(
IEnumerable expected, object actual, string message, params object[] args);
CollectionAssert.IsSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsSubsetOf(
IEnumerable subset, IEnumerable superset, string message, params object[] args);
CollectionAssert.IsNotSubsetOf(IEnumerable subset, IEnumerable superset);
CollectionAssert.IsNotSubsetOf(
IEnumerable subset, IEnumerable superset, string message, params object[] args);
CollectionAssert.IsEmpty(IEnumerable collection);
CollectionAssert.IsEmpty(
IEnumerable collection, string message, params object[] args);
CollectionAssert.IsNotEmpty(IEnumerable collection);
CollectionAssert.IsNotEmpty(
IEnumerable collection, string message, params object[] args);
CollectionAssert.IsOrdered(IEnumerable collection);
CollectionAssert.IsOrdered(
IEnumerable collection, string message, params object[] args);
CollectionAssert.IsOrdered(IEnumerable collection, IComparer comparer);
CollectionAssert.IsOrdered(IEnumerable collection,
IComparer comparer, string message, params object[] args);
Directory Assert
文件夹断言:
[Test]
public void Directory_Test()
{
string basePath = AppDomain.CurrentDomain.BaseDirectory;
DirectoryAssert.Exists(basePath + "/Directory"); // 判断文件夹是否存在
}
DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreEqual(DirectoryInfo expected, DirectoryInfo actual,
string message, params object[] args);
DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual);
DirectoryAssert.AreNotEqual(DirectoryInfo expected, DirectoryInfo actual,
string message, params object[] args);
DirectoryAssert.Exists(DirectoryInfo actual);
DirectoryAssert.Exists(DirectoryInfo actual,
string message, params object[] args);
DirectoryAssert.Exists(string actual);
DirectoryAssert.Exists(string actual,
string message, params object[] args);
DirectoryAssert.DoesNotExist(DirectoryInfo actual);
DirectoryAssert.DoesNotExist(DirectoryInfo actual,
string message, params object[] args);
DirectoryAssert.DoesNotExist(string actual);
DirectoryAssert.DoesNotExist(string actual,
string message, params object[] args);
File Assert
文件断言:
[Test]
public void File_Test()
{
// 判断文件是否存在
FileAssert.Exists(AppDomain.CurrentDomain.BaseDirectory + "/Directory/file.txt");
}
FileAssert.AreEqual(Stream expected, Stream actual); //判断两个文件流是否相同
FileAssert.AreEqual(
Stream expected, Stream actual, string message, params object[] args);
FileAssert.AreEqual(FileInfo expected, FileInfo actual); // 判断两个文件是否同一路径的同一文件
FileAssert.AreEqual(
FileInfo expected, FileInfo actual, string message, params object[] args);
FileAssert.AreEqual(string expected, string actual);
FileAssert.AreEqual(
string expected, string actual, string message, params object[] args);
FileAssert.AreNotEqual(Stream expected, Stream actual);
FileAssert.AreNotEqual(
Stream expected, Stream actual, string message, params object[] args);
FileAssert.AreNotEqual(FileInfo expected, FileInfo actual);
FileAssert.AreNotEqual(
FileInfo expected, FileInfo actual, string message, params object[] args);
FileAssert.AreNotEqual(string expected, string actual);
FileAssert.AreNotEqual(
string expected, string actual, string message, params object[] args);
FileAssert.Exists(FileInfo actual); //判断文件是否存在
FileAssert.Exists(
FileInfo actual, string message, params object[] args);
FileAssert.Exists(string actual);
FileAssert.Exists(
string actual, string message, params object[] args);
FileAssert.DoesNotExist(FileInfo actual);
FileAssert.DoesNotExist(
FileInfo actual, string message, params object[] args);
FileAssert.DoesNotExist(string actual);
FileAssert.DoesNotExist(
string actual, string message, params object[] args);
扩展
NUnit本身提供非常全面的功能,一般来说完全满足我们的项目需求。即使不能满足需求,NUnit也是可扩展的,包括:属性、约束条件、断言等的扩展。文档导航
单元测试之NUnit三的更多相关文章
- 单元测试之NUnit二
NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档.初次写博客,望大家指点. 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 本文介绍常用的NUni ...
- 单元测试之NUnit一
NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档.初次写博客,望大家指点. 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 NUnit是什么? N ...
- spring boot单元测试之RestTemplate(三)——api详解
本篇内容来自翟永超的<Springcloud微服务实战>,转载请注明. 一.GET请求 在RestTemplate中,对GET请求可以通过如下两个方法进行调用实现. 第一种:getForE ...
- [转载]单元测试之道(使用NUnit)
首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而又忐忑的心情点击界面上的 ...
- 单元测试之道(使用NUnit)
首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而 又忐忑的心情点击界面上 ...
- 补习系列(8)-springboot 单元测试之道
目录 目标 一.About 单元测试 二.About Junit 三.SpringBoot-单元测试 项目依赖 测试样例 四.Mock测试 五.最后 目标 了解 单元测试的背景 了解如何 利用 spr ...
- .NET Core单元测试之搞死开发的覆盖率统计(coverlet + ReportGenerator )
.NET Core单元测试之搞死开发的覆盖率统计 这两天在给项目补单元测试,dalao们要求要看一下测试覆盖率 翻了一波官方test命令覆盖率倒是有支持了,然而某个更新日志里面写着 ["Su ...
- Java基础学习总结(24)——Java单元测试之JUnit4详解
Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before ...
- ASP.NET Core搭建多层网站架构【3-xUnit单元测试之简单方法测试】
2020/01/28, ASP.NET Core 3.1, VS2019, xUnit 2.4.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[3-xUnit单元测试 ...
随机推荐
- 抽象数据类型与C++
类是一种新的数据类型,类似于数据结构,只是它拥有数据结构所没有的部分——“成员函数”,正是因为它所拥有的成员函数这一特性,使得它能隐藏“数据结构”(类)中的数据,不被用户所知道.通过类中的成员函数,使 ...
- Oracle RAC运维所遇问题记录一
Oracle11gR2,版本11.2.0.4集群测试环境运行正常 主机名:rac1,rac2 hosts文件: # Public172.17.188.12 rac1172.17.188.13 rac2 ...
- 多个module实体类集合打一个jar包并上传至远程库
本章内容主要分享多个module中的实体类集合生成到一个jar包中,并且发布到远程库:这里采用maven-assembly-plugin插件的功能来操作打包,内容不长却贴近实战切值得拥有,主要节点内容 ...
- Redis(二)--- Redis的底层数据结构
1.Redis的数据结构 Redis 的底层数据结构包含简单的动态字符串(SDS).链表.字典.压缩列表.整数集合等等:五大数据类型(数据对象)都是由一种或几种数结构构成. 在命令行中可以使用 OBJ ...
- SD卡操作
读写SD卡 Context类的openFileInput和openFileOutput方法都是针对应用程序的数据文件夹进行的文件操作,由于手机的ROM容量有限,因此这种操作有一定局限性. 手机的SD卡 ...
- JSP一二章笔试题
一. 什么是B/S架构,什么是C/S架构B/S(Browser/Server) 浏览器/服务器 C/S(Client/Server) 客户端/服务器 二. B/S架构的工作原理 浏览器请求服务器 通过 ...
- rabbitMQ_rpc(六)
远程过程调用(RPC) 在前面我们已经学习了如何使用工作队列在多个消费者之间分配耗时的任务. 但是如果我们需要在远程计算机上运行功能并等待结果怎么办?那将会是一个不同的故事.此模式通常称为远程过程调用 ...
- T-SQL 恢复数据库
USE master GO ALTER DATABASE DEMO SET SINGLE_USER GO ALTER DATABASE DEMO SET EMERGENCY GO DBCC CHECK ...
- Django安装 测试、导入项目以及运行开发服务器
安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py install 增加环境变量: C:\Python27\Scripts 测试djang ...
- linux下pip的安装
---恢复内容开始--- 1 输入apt-cache search wxpython 如果有返回信息 则输入 sudo apt-get install python-tools 2 否则 1.添加软件 ...