一款简单易用的.Net 断言测试框架 : Shouldly
GitHub地址:https://github.com/shouldly/shouldly
Shouldly的官方文档:http://docs.shouldly-lib.net/
Nuget安装:

在测试类中引用:
using Shouldly;
用法 :
//验证
UsingDbContext(context =>
{
var bill = context.Bills.FirstOrDefault();
bill.ShouldNotBeNull();
});
我们看一下,ShouldNotBeNull是如何定义的, 其实就是在在原类型的基础上加的扩展方法。
namespace Shouldly
{
[DebuggerStepThrough]
[ShouldlyMethods]
public static class ShouldBeNullExtensions
{
public static void ShouldBeNull<T>(this T actual);
public static void ShouldBeNull<T>(this T actual, string customMessage);
public static void ShouldBeNull<T>(this T actual, [InstantHandle] Func<string> customMessage);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual, string customMessage);
[ContractAnnotation("actual:null => halt")]
public static void ShouldNotBeNull<T>(this T actual, [InstantHandle] Func<string> customMessage);
}
}
[DebuggerStepThrough]
[ShouldlyMethods]
public static class ShouldBeTestExtensions
{
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this DateTime actual, DateTime expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, string customMessage);
public static void ShouldBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this T actual, T expected);
public static void ShouldBe<T>(this T actual, T expected, string customMessage);
public static void ShouldBe<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder = false);
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance, string customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder, [InstantHandle] Func<string> customMessage);
public static void ShouldBe<T>(this IEnumerable<T> actual, IEnumerable<T> expected, bool ignoreOrder, string customMessage);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance, string customMessage);
public static void ShouldBe(this decimal actual, decimal expected, decimal tolerance);
public static void ShouldBe(this double actual, double expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this double actual, double expected, double tolerance, string customMessage);
public static void ShouldBe(this double actual, double expected, double tolerance);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance, string customMessage);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance);
public static void ShouldBe(this IEnumerable<float> actual, IEnumerable<float> expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance, string customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance);
public static void ShouldBe(this float actual, float expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this float actual, float expected, double tolerance, string customMessage);
public static void ShouldBe(this float actual, float expected, double tolerance);
public static void ShouldBe(this IEnumerable<decimal> actual, IEnumerable<decimal> expected, decimal tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBe(this IEnumerable<double> actual, IEnumerable<double> expected, double tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected, string customMessage);
public static void ShouldBeAssignableTo(this object actual, Type expected);
public static T ShouldBeAssignableTo<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static T ShouldBeAssignableTo<T>(this object actual);
public static T ShouldBeAssignableTo<T>(this object actual, string customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeGreaterThan<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThan<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeGreaterThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeInRange<T>(this T actual, T from, T to, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeInRange<T>(this T actual, T from, T to, string customMessage) where T : IComparable<T>;
public static void ShouldBeInRange<T>(this T actual, T from, T to) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeLessThan<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeLessThan<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeLessThan<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, Func<string> customMessage);
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer);
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, string customMessage) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected) where T : IComparable<T>;
public static void ShouldBeLessThanOrEqualTo<T>(this T actual, T expected, IComparer<T> comparer, string customMessage);
public static void ShouldBeNegative(this decimal actual);
public static void ShouldBeNegative(this decimal actual, string customMessage);
public static void ShouldBeNegative(this decimal actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this double actual, string customMessage);
public static void ShouldBeNegative(this double actual);
public static void ShouldBeNegative(this double actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual);
public static void ShouldBeNegative(this long actual, string customMessage);
public static void ShouldBeNegative(this short actual, string customMessage);
public static void ShouldBeNegative(this short actual);
public static void ShouldBeNegative(this long actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this long actual);
public static void ShouldBeNegative(this int actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this int actual, string customMessage);
public static void ShouldBeNegative(this int actual);
public static void ShouldBeNegative(this short actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeNegative(this float actual, string customMessage);
public static T ShouldBeOfType<T>(this object actual);
public static T ShouldBeOfType<T>(this object actual, string customMessage);
public static void ShouldBeOfType(this object actual, Type expected, string customMessage);
public static void ShouldBeOfType(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static T ShouldBeOfType<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBeOfType(this object actual, Type expected);
public static void ShouldBeOneOf<T>(this T actual, T[] expected, string customMessage);
public static void ShouldBeOneOf<T>(this T actual, params T[] expected);
public static void ShouldBeOneOf<T>(this T actual, T[] expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this float actual, string customMessage);
public static void ShouldBePositive(this decimal actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this short actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this short actual, string customMessage);
public static void ShouldBePositive(this short actual);
public static void ShouldBePositive(this decimal actual);
public static void ShouldBePositive(this decimal actual, string customMessage);
public static void ShouldBePositive(this float actual);
public static void ShouldBePositive(this double actual);
public static void ShouldBePositive(this double actual, string customMessage);
public static void ShouldBePositive(this double actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this float actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this long actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this long actual, string customMessage);
public static void ShouldBePositive(this long actual);
public static void ShouldBePositive(this int actual, [InstantHandle] Func<string> customMessage);
public static void ShouldBePositive(this int actual, string customMessage);
public static void ShouldBePositive(this int actual);
public static void ShouldBeSameAs(this object actual, object expected, string customMessage);
public static void ShouldBeSameAs(this object actual, object expected, [InstantHandle] Func<string> customMessage);
public static void ShouldBeSameAs(this object actual, object expected);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected, [InstantHandle] Func<string> customMessage);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected, string customMessage);
[ContractAnnotation("actual:null,expected:null => halt")]
public static void ShouldNotBe<T>(this T actual, T expected);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBe(this DateTimeOffset actual, DateTimeOffset expected, TimeSpan tolerance);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance, string customMessage);
public static void ShouldNotBe(this DateTime actual, DateTime expected, TimeSpan tolerance);
public static void ShouldNotBe(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected);
public static void ShouldNotBeAssignableTo<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo<T>(this object actual, string customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeAssignableTo(this object actual, Type expected, string customMessage);
public static void ShouldNotBeAssignableTo<T>(this object actual);
public static void ShouldNotBeInRange<T>(this T actual, T from, T to) where T : IComparable<T>;
public static void ShouldNotBeInRange<T>(this T actual, T from, T to, [InstantHandle] Func<string> customMessage) where T : IComparable<T>;
public static void ShouldNotBeInRange<T>(this T actual, T from, T to, string customMessage) where T : IComparable<T>;
public static void ShouldNotBeOfType(this object actual, Type expected, string customMessage);
public static void ShouldNotBeOfType<T>(this object actual, string customMessage);
public static void ShouldNotBeOfType(this object actual, Type expected);
public static void ShouldNotBeOfType(this object actual, Type expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOfType<T>(this object actual);
public static void ShouldNotBeOfType<T>(this object actual, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, T[] expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, T[] expected, string customMessage);
public static void ShouldNotBeOneOf<T>(this T actual, params T[] expected);
public static void ShouldNotBeSameAs(this object actual, object expected, [InstantHandle] Func<string> customMessage);
public static void ShouldNotBeSameAs(this object actual, object expected, string customMessage);
public static void ShouldNotBeSameAs(this object actual, object expected);
}
当测试失败时,

异常测试:
var bill = context.Bills.FirstOrDefault();
Should.Throw<DivideByZeroException>(() =>
{
bill.SetBmiNoPay();
});
测试结果:

一款简单易用的.Net 断言测试框架 : Shouldly的更多相关文章
- 推荐一款简单易用线上引流测试工具:GoReplay
一. 引流测试产生背景 日常大部分的测试工作都是在测试环境下,通过模拟用户的行为来对系统进行验证,包括功能以及性能.在这个过程中,你可能会遇到以下问题: 用户访问行为比较复杂,模拟很难和用户行为一致, ...
- 简单易用的leetcode开发测试工具(npm)
描述 最近在用es6解leetcode,当问题比较复杂时,有可能修正了新的错误,却影响了前面的流程.要用通用的测试工具,却又有杀鸡用牛刀的感觉,所以就写了个简单易用的leetcode开发测试工具,分享 ...
- 设计与开发一款简单易用的Web报表工具(支持常用关系数据及hadoop、hbase等)
EasyReport是一个简单易用的Web报表工具(支持Hadoop,HBase及各种关系型数据库),它的主要功能是把SQL语句查询出的行列结构转换成HTML表格(Table),并支持表格的跨行(Ro ...
- Criterion - 一个简单可扩展的 C 语言测试框架
A dead-simple, yet extensible, C test framework. Philosophy Most test frameworks for C require a lot ...
- 如何以最简单的方式安装 KALI 渗透测试框架系统
0x01 第一步下载 KALI 百度搜索 KALI 官网,找到下载区,我选的是 64 位标准版,但是推荐下载 32 位(功能貌似更全) 这个为下载后的 iso 镜像文件 0x02 第二步打开虚拟机,配 ...
- 简单易用的堡垒机系统—Teleport
简单易用的堡垒机系统-Teleport 官方文档:http://teleport.eomsoft.net/doc#!1 一.Teleport介绍 Teleport是触维软件推出的一款简单易用的堡垒机 ...
- 消灭Bug!十款免费移动应用测试框架推荐
对于移动应用开发者而言,Bug往往是最让人头疼的一大问题.不同于时时刻刻可以修补的Web App,移动App中的Bug往往隐藏得很深,甚至有时候等到用户使用才显现出来,这么一来开发者搞不好就会赔了 ...
- hapi lab测试框架简单使用
1. 依赖安装 yarn init yarn add lab code 2. 基本模式 const Lab = require('lab'); const Code = require('code') ...
- [.net 面向对象程序设计进阶] (22) 团队开发利器(一)简单易用的代码管理工具VSS
[.net 面向对象程序设计进阶] (22) 团队开发利器(一)简单易用的代码管理工具VSS 本篇要点:在进阶篇快要结束的时候说说源代码管理器,我们的开发,不是一个人可以完成的事,团队协作很重要,而且 ...
随机推荐
- 多进程使用matplotlib.pyplot绘heatmap(多线程不可以)
数据格式如下: 8_15/l_eye/2732.png -20.5773 -5.17769 -3.34583 21.5859 9_13_1/l_eye/1211.png -10.1145 34.992 ...
- jquery prop attr
checked比较特殊,只要设置了属性checked,不管何值都是checked的.例如:<input type="checkbox" checked><inpu ...
- 过程记录:搭建wordpress站点
过程记录:搭建wordpress站点 前提:现在aws中搭建好LNAMP环境和网络mysql数据库,即为下载的wdcp和aws的rds 1.获取WordPress安装包(中文版) https://cn ...
- Mysql数据库常用操作语句大全
零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=PAS ...
- 001-linux scp文件拷贝
scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的.可能会稍微影响一下速度.当你服务器 ...
- 关于sails 初学者常见问题汇总
http://sailsdoc.swift.ren/ 这里有 sails中文文档 一.安装时: 先装nodejs,成功标志 node -v 安装sails 全局安装 node install sail ...
- 从原型链看DOM--Text类型
文本节点由Text类型表示,包含的是可以按照字面解释的纯文本内容,纯文本中可以包含转义后的HTML字符但不能包含HTML代码.原型链继承关系为:textNode.__proto__->Text. ...
- php实现异步的程序调用
浏览器和服务器之间的通信是基于HTTP协议进行链接通讯的,它是一种请求和相应的协议.浏览器通过URL向服务器发送请求,服务器接收到请求并执行请求,然后服务器将执行完成的数据返回到客户端. 这就存在一个 ...
- 已经安装好了的lamp或者lnmp环境,编译其他的模块进来?
问题: 如何为已经编译好了的环境再次编译其他的模块? 方法: 一般分为两种情况: 1. php的源码安装包中本来就有这个 .so 的扩展,我们只需要进入到php的安装源码包中的ext文件夹下,然后找到 ...
- UVA10026:Shoemaker's Problem(贪心)
题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/K 题目需求:鞋匠有n个任务,第i个任务要花费ti ...