原文 VS2010(2012)中使用Unit Testing進行單元測試html
使用VS 2012自帶的Unit Testing工具進行單元測試是很是方便的。網上關於這方面的例子不少,這篇隨筆只起我的學習筆記之用,因此脈絡不會很清晰。web
一、簡單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() { } } }
測試代碼:post
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); } } }
二、測試準備和測試清理工做單元測試
若是我想在全部TestMethod執行前進行一些準備工做怎麼辦?答案是使用ClassInitialize。學習
若是我想在全部TestMethod執行完成後進行一些清理工做怎麼辦?答案是使用ClassCleanup。測試
若是我想在每一個TestMethod執行前進行一些準備工做怎麼辦?答案是使用TestInitialize。url
若是我想在每一個TestMethod執行完成後進行一些清理工做怎麼辦?答案是使用TestCleanup。spa
以下:
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."); } } }
三、[ExpectedException]
Unit Testing中的attribute除了最基本的TestClass、TestMethod之外,還有一些很是用可是可能有用的attribute。
[ExpectedException(exceptionType: Type]能夠用來代表某個測試方法預期拋出某個異常,而且只有真的拋出異常時才經過測試。好比下面:
[TestMethod] [ExpectedException(typeof(ArgumentException))] public void TestExpectedException() { throw new ArgumentException("參數錯誤"); }
四、斷言API
Assert類的靜態方法以下,其中經常使用的包括AreEqual、AreNotEqual、AreSame、IsNull、IsTrue、Inconclusive和Fail
針對集合類型的斷言方法:
針對字符串類型的斷言方法:
五、針對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
六、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
七、其餘資源(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