惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
博客园 - 【当耐特】
博客园_首页
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
V
Visual Studio Blog
F
Fortinet All Blogs
Martin Fowler
Martin Fowler

博客园 - 踏歌长行

前端知识质量内容网址 NuGet 质量博客链接 Entity Framework 质量博客链接 LINK1123:failure during conversion to COFF:file invalid or corrupt 未授权用户在此计算机上的请求登录类型 System.ServiceModel.AddressAccessDeniedException DataGrid 导出 Excel 中文乱码 80070005 Access is denied in Windows 2008 How to install ASP.NET 1.1 with IIS7 on Vista and Windows 2008 Unable to find script library '/aspnet_client/system-web/1-1-4322/webvalidation.js' JQuery file upload Access is denied in IE 7, 8, 9 ReportViewer 自适应高度 单元测试之模拟Mock ASP.NET MVC ajax 提交列表到 Action VS 2010 制作 Windows Service 安装包之用户界面 VS 2010 制作 Windows Service 安装包 Postback 之后保持浏览器滚动条的位置 VS 2010 开发 ActiveX 自动升级外篇 VS 2010 开发 ActiveX 制作 cab 包
单元测试之 Xunit
踏歌长行 · 2014-01-15 · via 博客园 - 踏歌长行



单元测试项目中引入 xunit.dll
1. 有类 Common,中有方法 Divide(int a, int b),代码如下

public class Common
{
        public int Divide(int a, int b)
        {
            if (b == 0)
                return 0;

            return a / b;
        }
}

2. 在单元测试项目中为方法 Divide 写单元测试代码如下

[Fact]
public void TestDivideInCommon()
{
            var common = new Common();

            // 第一种情况
            // 调用方法得到的值
            var actual1 = common.Divide(1, 0);
            // 期望的值
            const int expect1 = 0;

            // 对比两个值,如果相等则通过验证
            Assert.True(actual1 == expect1, "When b = 0");

            // 第二种情况
            var actual2 = common.Divide(6, 2);
            const int expect2 = 3;

            // 一个方法,不同的条件可能结果有很大差异,多写几个断言
            Assert.True(actual2 == expect2, "When a = 6, b = 2");
}

3. Test Explorer 中点击 Run All 运行单元测试方法