VS2010(2012)中使用Unit Testing进行单元测试
原文 VS2010(2012)中使用Unit Testing进行单元测试
使用VS 2012自带的Unit Testing工具进行单元测试是非常方便的。网上关于这方面的例子很多,这篇随笔只起个人学习笔记之用,所以脉络不会很清晰。
1、简单Demo:
待测试类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace NUnitLab
{
public class MaxValue
{
// 将要测试的方法
public static int Max(int[] list)
{
if (list == null)
return -1; int len = list.Length;
if (len == 0)
return list[0]; int i, max = int.MinValue;
for (i = 0; i < len; i++)
{
if (list[i] > max)
max = list[i];
} return max;
} public static int Min(int[] list)
{
return 0;
} public static void Main()
{ }
}
}

测试代码:

using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnitLab; namespace UnitTestProject
{
[TestClass()]
public class TestMaxValue
{
[TestMethod]
public void TestMax()
{
Assert.AreEqual(MaxValue.Max(new int[] {9, 9, 1}), 9);
Assert.AreEqual(MaxValue.Max(new int[] { -1, 2, 1 }), 2);
}
}
}

2、测试准备和测试清理工作
如果我想在所有TestMethod执行前进行一些准备工作怎么办?答案是使用ClassInitialize。
如果我想在所有TestMethod执行完成后进行一些清理工作怎么办?答案是使用ClassCleanup。
如果我想在每个TestMethod执行前进行一些准备工作怎么办?答案是使用TestInitialize。
如果我想在每个TestMethod执行完成后进行一些清理工作怎么办?答案是使用TestCleanup。
如下:

using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnitLab; namespace UnitTestProject
{
[TestClass()]
public class TestMaxValue
{
public TestContext TestContext { get; set; } [ClassInitialize()]
public static void Init(TestContext context)
{
Console.WriteLine("Use ClassInitialize to run code before you run the first test in the class.");
} [TestInitialize]
public void BeforeTest()
{
Console.WriteLine("Use TestInitialize to run code before you run each test.");
} [TestMethod]
public void TestMax()
{
Assert.AreEqual(MaxValue.Max(new int[] {9, 9, 1}), 9);
Assert.AreEqual(MaxValue.Max(new int[] { -1, 2, 1 }), 2);
// 结果不明或者还未完成测试
Assert.Inconclusive(string.Format("还未完成{0}方法的单元测试", MethodBase.GetCurrentMethod().Name));
} [TestCleanup]
public void AfterTest()
{
Console.WriteLine("Use TestCleanup to run code after you run each test.");
} [ClassCleanup()]
public static void Cleanup()
{
Console.WriteLine("Use ClassCleanup to run code after all tests in a class have run.");
}
}
}

3、[ExpectedException]
Unit Testing中的attribute除了最基本的TestClass、TestMethod以外,还有一些非常用但是可能有用的attribute。
[ExpectedException(exceptionType: Type]可以用来表明某个测试方法预期抛出某个异常,并且只有真的抛出异常时才通过测试。比如下面:
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestExpectedException()
{
throw new ArgumentException("参数错误");
}
4、断言API
Assert类的静态方法如下,其中常用的包括AreEqual、AreNotEqual、AreSame、IsNull、IsTrue、Inconclusive和Fail

针对集合类型的断言方法:

针对字符串类型的断言方法:

5、针对ASP.NET的单元测试
这里推荐网上的一个系列博客,
ASP.NET单元测试系列1(新手上路):http://blog.miniasp.com/post/2010/09/14/ASPNET-MVC-Unit-Testing-Part-01-Kick-off.aspx
ASP.NET单元测试系列2(可测试性):http://blog.miniasp.com/post/2010/09/15/ASPNET-MVC-Unit-Testing-Part-02-Testability.aspx
ASP.NET单元测试系列3(使用Mock):http://blog.miniasp.com/post/2010/09/16/ASPNET-MVC-Unit-Testing-Part-03-Using-Mock-moq.aspx
ASP.NET单元测试系列4(单元测试的目的与价值):http://blog.miniasp.com/post/2010/09/17/ASPNET-MVC-Unit-Testing-Part-04-The-Purpose-and-Value.aspx
ASP.NET单元测试系列5(了解Stub):http://blog.miniasp.com/post/2010/09/18/ASPNET-MVC-Unit-Testing-Part-05-Using-Stub-Object.aspx
ASP.NET单元测试系列6(测试路由规则):http://blog.miniasp.com/post/2010/09/23/ASPNET-MVC-Unit-Testing-Part-06-Routing.aspx
6、Visual Studio 2012 Fakes框架
http://www.cnblogs.com/liuliuyingxia/archive/2012/08/26/2657515.html
http://www.cnblogs.com/liuliuyingxia/archive/2012/08/25/2655856.html
7、其他资源(MSDN)
Real World Developer Testing with Visual Studio 2012: http://channel9.msdn.com/Events/TechEd/Europe/2012/AAP401
Verifying Unit Testing by Using Unit Tests: http://msdn.microsoft.com/en-us/library/dd264975(v=vs.110).aspx
VS2010(2012)中使用Unit Testing进行单元测试的更多相关文章
- Javascript单元测试Unit Testing之QUnit
body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } QUnit是一个基于JQuery的单元测试Uni ...
- 读书笔记-实用单元测试(英文版) Pragmatic Unit Testing in C# with NUnit
读书笔记-实用单元测试(英文版) Pragmatic Unit Testing in C# with NUnit Author: Andrew Hunt ,David Thomas with Matt ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(15)|Unit Testing单元测试]
[易学易懂系列|rustlang语言|零基础|快速入门|(15)] 实用知识 Unit Testing单元测试 我们知道,在现代软件开发的过程中,单元测试对软件的质量极及重要. 今天我们来看看Rust ...
- MVC Unit Testing学习笔记
MVC Unit Testing 参考文档: 1.http://www.asp.net/mvc/overview/testing 2.http://www.asp.net/mvc/tutorials/ ...
- 体验VS2017的Live Unit Testing
相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是可以在编写代码的时候进行实时的bac ...
- Unit Testing of Spring MVC Controllers: “Normal” Controllers
Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Unit Testing of Spring MVC Controllers: Configuration
Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...
- Live Unit Testing
Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是 ...
- AY写给国人的教程- VS2017 Live Unit Testing[1/2]-C#人爱学不学-aaronyang技术分享
原文:AY写给国人的教程- VS2017 Live Unit Testing[1/2]-C#人爱学不学-aaronyang技术分享 谢谢大家观看-AY的 VS2017推广系列 Live Unit Te ...
随机推荐
- <转> 30 个有关 Python 的小技巧
目录[+] 1.1 拆箱 1.2 拆箱变量交换 1.3 扩展拆箱(只兼容python3) 1.4 负数索引 1.5 切割列表 1.6 负数索引切割列表 1.7指定步长切割列表 1.8 负数步长切割列表 ...
- Ubuntu14.04 Y460闪屏问题解决方案
我的笔记本是联想Y460,安装了Ubuntu之后发现屏幕闪烁移位,而且在使用IDE的时候出现无法输入中文等问题,其实是显卡驱动的问题,N卡官网给的驱动不好用,尝试使用大黄蜂 参考:https://wi ...
- 【BZOJ1132】【POI2008】Tro 计算几何 叉积求面积
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- 解决 RichTextBox 文件格式不对问题
RichTextBox文件格式不对: 原因:富文本框的LoadFile方法只支持RTF格式的文件或者标准的ASCII文本本档,,我们一般的文本文档是ANSI或者UTF-8的格式,所以,报这个错. 解决 ...
- Arduino 入门程序示例之一片 LED(2015-06-11)
概述 从点到线,从线到面.现在开始要来一片的 LED 了,一大波的 LED 正在到来! 示例程序 因为手头没有现成的模块,手头只有 595,所以这里每一个示例程序都是使用 74HC595 扩展 IO ...
- [转]Centos6.5使用yum安装mysql—配置MySQL允许远程登录
一.mysql安装 第1步.yum安装mysql[root@stonex ~]# yum -y install mysql-server安装结果:Installed: mysql-server ...
- C语言中Const与指针(转载)
一.说明指针常量.指向常量的指针和指向常量的常量指针的含义.区别和共同点 首先,以上三种概念的共同点:都指的是指针 指针也是一种变量,它存储指定类型的变量的内存地址,如char* 来声明一个字符型指针 ...
- ie6下常见的bug 调整页面兼容性
ie6下常见的bug 我们布局页面,首先符合标准,如何写一个页面的标准性? 但是ie6等浏览器本身就比较特殊,bug比较多,兵法云,知己知彼百战百胜.我们需要了解ie6的一些常见bug,这样,更好的调 ...
- ffmpeg h265
最新版本号的ffmpeg 支持 libh265,可是还是0基础測试阶段 在linux 上安装ffmpeg 支持h265编码器依照下面步骤: Anyhow here are the simple ste ...
- BZOJ 2750: [HAOI2012]Road( 最短路 )
对于每个点都跑最短路, 然后我们得到了个DAG, 在这DAG上更新每条边的答案. 考虑e(u, v)∈DAG对答案的贡献: 假设从S到u得路径数为A[u], 从v出发到达任意点的路径数为B[v], ...