VS2010(2012)中使用Unit Testing进行单元测试
来源:互联网 发布:淘宝网卖什么东西好 编辑:程序博客网 时间:2024/04/28 07:03
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进行单元测试
- 理解单元测试(Unit Testing)
- 单元测试(Unit Testing)基础
- 【Android】unit testing 单元测试
- 单元测试Unit Testing
- 软件单元测试(Software Unit Testing)
- About Unit Testing单元测试摘要
- 使用Google C++ Testing Framework进行单元测试
- VS2010中如何进行单元测试
- 使用Symbian OS Unit单元测试框架进行单元测试
- VS2010中使用CLR单元测试框架进行C++测试
- Unit-Testing in a Project --- 单元测试
- android应用开发之:单元测试(unit testing)
- flask入门的教程-(单元测试) Unit Testing
- iOS单元测试入门(Unit Testing)
- 使用Android Studio进行本地单元测试(Local Unit Tests)
- 单元测试(Unit Testing) – 对已有代码添加单元测试
- ASP.NET MVC + MVC Contrib + Unit Testing MVC 单元测试
- jQuery自定义插件简单介绍
- Java多线程编程总结
- hdoj 1398 Square Coins【母函数】
- Linux终端使用技巧
- 搜狗五笔拼音混合输入法2016 v2.10
- VS2010(2012)中使用Unit Testing进行单元测试
- Asp 操作Access数据库时出现死锁.ldb的解决方法
- docker 学习笔记1
- 汉邦一点通视频监控客户端 v1.4.0 官方版
- SVN问题:Server sent unexpected return value (403 Forbidden) in response to OPTIONS
- 晦涩的oracle X$
- 机器学习中的算法(1)-决策树模型组合之随机森林与GBDT
- ndk-stack 调试NDK程序
- nc 56处理异常