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. SSM 框架集成

    1.SSM是什么? SSM是指目前最主流的项目架构的三大框架: SpringMVC : spring的 Web层框架,是spring的一个模块 Spring :容器框架 MyBatis :持久层框架 ...

  2. Java NIO学习系列五:I/O模型

    前面总结了很多IO.NIO相关的基础知识点,还总结了IO和NIO之间的区别及各自适用场景,本文会从另一个视角来学习一下IO,即IO模型.什么是IO模型?对于不同人.在不同场景下给出的答案是不同的,所以 ...

  3. Kotlin之var和val区别

    var 和 val 是Kotlin的两个声明变量的关键字, var声明的变量是一个可变的变量,而val声明的变量是一个只读的变量(类似于java中的final变量)

  4. C语言入门5-键盘的输入和屏幕输出

    C程序中的键盘输入和屏幕输出都是通过  调用输入/输出函数  实现的. 一.数据的格式化    屏幕输出 函数printf()的一般格式  (有两种) (1)第一种: printf(格式控制字符串): ...

  5. [剑指offer] 6. 旋转数组的最小数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋 ...

  6. [HDOJ] 2026.Max Sum

    2026.Max Sum (c++) Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1 ...

  7. IIS短文件名漏洞复现

    IIS短文件名漏洞复现 一.漏洞描述 此漏洞实际是由HTTP请求中旧DOS 8.3名称约定(SFN)的代字符(~)波浪号引起的.它允许远程攻击者在Web根目录下公开文件和文件夹名称(不应该可被访问). ...

  8. 没事别想不开做Halcon视觉工程师 halcon机器视觉如何学习?

    今天我们来听听看来自一个机器视觉工程师的唠叨和吐槽,在这之后,你还想学人工智能,还想学机器视觉?恭喜你,你对人工智能机器视觉是真爱了! 既然自己选择了这条路,那么无论前进路上有多坎坷,跪着也要走完. ...

  9. 从零开发一款自己的小程序UI组件库(一)

    写在前面:有开发过小程序的朋友肯定知道组件化开发的特性,高内聚与低耦合.使用已有的UI组件库,诸如:vantUI-weapp.minUI-weapp等UI组件库的诞生使我们的开发速度大大的加快,丰富的 ...

  10. 在Linux - Centos上安装Python3(上)

    必看内容 在Linux上安装Python常用的2种方法 1.Python源码编译安装,有点复杂,适合老司机 2.从EPEL/IUS仓库安装,新手建议使用些方法,比较简单,目前2019-07-31提供最 ...