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三的更多相关文章

  1. 单元测试之NUnit二

    NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档.初次写博客,望大家指点. 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 本文介绍常用的NUni ...

  2. 单元测试之NUnit一

    NUnit 分三篇文章介绍,入门者可阅读文章,有基础者直接参考官方文档.初次写博客,望大家指点. 导航: 单元测试之NUnit一 单元测试之NUnit二 单元测试之NUnit三 NUnit是什么? N ...

  3. spring boot单元测试之RestTemplate(三)——api详解

    本篇内容来自翟永超的<Springcloud微服务实战>,转载请注明. 一.GET请求 在RestTemplate中,对GET请求可以通过如下两个方法进行调用实现. 第一种:getForE ...

  4. [转载]单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而又忐忑的心情点击界面上的 ...

  5. 单元测试之道(使用NUnit)

    首先来看下面几个场景你是否熟悉 1.你正在开发一个系统,你不断地编码-编译-调试-编码-编译-调试……终于,你负责的功能模块从上到下全部完成且编译通过!你长出一口气,怀着激动而 又忐忑的心情点击界面上 ...

  6. 补习系列(8)-springboot 单元测试之道

    目录 目标 一.About 单元测试 二.About Junit 三.SpringBoot-单元测试 项目依赖 测试样例 四.Mock测试 五.最后 目标 了解 单元测试的背景 了解如何 利用 spr ...

  7. .NET Core单元测试之搞死开发的覆盖率统计(coverlet + ReportGenerator )

    .NET Core单元测试之搞死开发的覆盖率统计 这两天在给项目补单元测试,dalao们要求要看一下测试覆盖率 翻了一波官方test命令覆盖率倒是有支持了,然而某个更新日志里面写着 ["Su ...

  8. Java基础学习总结(24)——Java单元测试之JUnit4详解

    Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before  ...

  9. 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单元测试 ...

随机推荐

  1. 题解 P3811 【【模板】乘法逆元】

    P3811 [模板]乘法逆元 一个刚学数论的萌新,总结了一下这题的大部分做法 //一.费马小定理+快速幂 O(nlogn) 64分 #include<cstdio> using names ...

  2. 一些学习js的算法题目

    1.排序 问题描述 编写一个程序,输入3个整数,然后程序将对这三个整数按照从大到小进行排列. 输入格式:输入只有一行,即三个整数,中间用空格隔开. 输出格式:输出只有一行,即排序后的结果. 输入输出样 ...

  3. docker实战(一)之Tomcat的安装

           docker号称分分钟就可以将环境构建完成,这话一点也不假,因为docker在使用软件时只需要从官方 仓库中拉取对应的镜像就行了.docker的使用前需要了解两个名词--镜像和容器.这两 ...

  4. ASP.NET登录验证码解决方案

    目录 #验证码效果图 #代码 0.html代码 1.Handler中调用验证码生成类 2.验证码图片绘制生成类 3.高斯模糊算法类 #注意 #参考 在web项目中,为了防止登录被暴力破解,需要在登录的 ...

  5. mysql协议分析1---报文的格式和基本类型

    navicat 和 mysql 是一对好基友,每天都有非常频繁的交流,主人在navicat上写下每条sql语句,轻轻的点了下执行按钮,navicat就飞快的把主人的指令传送到mysql那里,mysql ...

  6. UPC Contest RankList – 2019年第二阶段我要变强个人训练赛第十六场

    E: 飞碟解除器 •题目描述 wjyyy在玩跑跑卡丁车的时候,获得了一个飞碟解除器,这样他就可以免受飞碟的减速干扰了.飞碟解除器每秒末都会攻击一次飞碟,但每次只有p/q的概率成功攻击飞碟.当飞碟被成功 ...

  7. activeMQ_helloworld(一)

    一.activeMQ下载,直接在Linux上wget http://mirror.bit.edu.cn/apache//activemq/5.14.5/apache-activemq-5.14.5-b ...

  8. 谈谈用Boox Max 2 阅读A4纸文献的体验

    首先说说选择Boox的几个原因: 护眼.这个不用多说,之所以除了电脑,还要电子阅读器,主要是为了护眼. 减少纸质书籍购买.纸质书籍拿在手上是有质感,读起来也更舒服,可一则一些外文书买纸质的是很贵的,相 ...

  9. Java 字符串分隔 split

    Java中的我们可以利用 split 方法(Java.lang.string.split)把字符串按照指定的分割符进行分割,然后返回字符串数组,下面是string.split的用法实例及注意事项. s ...

  10. hdoj 4706 Children's Day

    题目意思就是用a-z组成一个N,然后到z后又跳回a,输出宽从3到10的N. #include <stdio.h> #include <string.h> char s[14][ ...