使用VS 2012自帶的Unit Testing工具進行單元測試是非常方便的冈钦。網(wǎng)上關(guān)于這方面的例子很多虱肄,這篇隨筆只起個人學習筆記之用谜叹,所以脈絡(luò)不會很清晰。
1括堤、簡單Demo:
? ? 待測試類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace NUnitLab
{
? ? publicclass MaxValue
? ? {
? ? ? ? // 將要測試的方法publicstaticintMax(int[] list)
? ? ? ? {
? ? ? ? ? ? if(list ==null)
? ? ? ? ? ? ? ? return-1;
? ? ? ? ? ? intlen = list.Length;
? ? ? ? ? ? if(len ==0)
? ? ? ? ? ? ? ? returnlist[0];
? ? ? ? ? ? inti, max =int.MinValue;
? ? ? ? ? ? for(i =0; i < len; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(list[i] > max)
? ? ? ? ? ? ? ? ? ? max = list[i];
? ? ? ? ? ? }
? ? ? ? ? ? return max;
? ? ? ? }
? ? ? ? publicstaticintMin(int[] list)
? ? ? ? {
? ? ? ? ? ? return0;
? ? ? ? }
? ? ? ? publicstaticvoid Main()
? ? ? ? {
? ? ? ? }
? ? }
}
? ?測試代碼:
using System;using System.Reflection;using Microsoft.VisualStudio.TestTools.UnitTesting;using NUnitLab;namespace UnitTestProject
{
? ? [TestClass()]
? ? publicclass TestMaxValue
? ? {? ? ? ? [TestMethod]
? ? ? ? publicvoid TestMax()
? ? ? ? {
? ? ? ? ? ? Assert.AreEqual(MaxValue.Max(newint[] {9,9,1}),9);
? ? ? ? ? ? Assert.AreEqual(MaxValue.Max(newint[] { -1,2,1}),2);? ? ? ? }? ? }
}
2碌秸、測試準備和測試清理工作
? ? 如果我想在所有TestMethod執(zhí)行前進行一些準備工作怎么辦绍移?答案是使用ClassInitialize。
? ? 如果我想在所有TestMethod執(zhí)行完成后進行一些清理工作怎么辦讥电?答案是使用ClassCleanup蹂窖。
? ? 如果我想在每個TestMethod執(zhí)行前進行一些準備工作怎么辦?答案是使用TestInitialize恩敌。
? ? 如果我想在每個TestMethod執(zhí)行完成后進行一些清理工作怎么辦瞬测?答案是使用TestCleanup。
? ? 如下:
using System;using System.Reflection;using Microsoft.VisualStudio.TestTools.UnitTesting;using NUnitLab;namespace UnitTestProject
{
? ? [TestClass()]
? ? publicclass TestMaxValue
? ? {
? ? ? ? publicTestContext TestContext {get;set; }
? ? ? ? [ClassInitialize()]
? ? ? ? publicstaticvoid Init(TestContext context)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("Use ClassInitialize to run code before you run the first test in the class.");
? ? ? ? }
? ? ? ? [TestInitialize]
? ? ? ? publicvoid BeforeTest()
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("Use TestInitialize to run code before you run each test.");
? ? ? ? }
? ? ? ? [TestMethod]
? ? ? ? publicvoid TestMax()
? ? ? ? {
? ? ? ? ? ? Assert.AreEqual(MaxValue.Max(newint[] {9,9,1}),9);
? ? ? ? ? ? Assert.AreEqual(MaxValue.Max(newint[] { -1,2,1}),2);
? ? ? ? ? ? // 結(jié)果不明或者還未完成測試Assert.Inconclusive(string.Format("還未完成{0}方法的單元測試", MethodBase.GetCurrentMethod().Name));? ? ? ? }
? ? ? ? [TestCleanup]
? ? ? ? publicvoid AfterTest()
? ? ? ? {
? ? ? ? ? ? Console.WriteLine("Use TestCleanup to run code after you run each test.");
? ? ? ? }
? ? ? ? [ClassCleanup()]
? ? ? ? publicstaticvoid 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]可以用來表明某個測試方法預(yù)期拋出某個異常狮斗,并且只有真的拋出異常時才通過測試。比如下面:
[TestMethod]
[ExpectedException(typeof(ArgumentException))]publicvoid TestExpectedException()
{
? thrownewArgumentException("參數(shù)錯誤");
}
4弧蝇、斷言API
? ? Assert類的靜態(tài)方法如下碳褒,其中常用的包括AreEqual、AreNotEqual看疗、AreSame沙峻、IsNull、IsTrue两芳、Inconclusive和Fail
? ?針對集合類型的斷言方法:
? ?針對字符串類型的斷言方法:
5摔寨、針對ASP.NET的單元測試
? ? 這里推薦網(wǎng)上的一個系列博客,
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(測試路由規(guī)則):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
轉(zhuǎn)自:http://www.cnblogs.com/feichexia/archive/2012/11/21/DonetUnitTesting.html