VS-unit testing

使用VS 2012自帶的Unit Testing工具進行單元測試是非常方便的。網(wǎng)上關于這方面的例子很多,這篇隨筆只起個人學習筆記之用,所以脈絡不會很清晰。

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);

? ? ? ? ? ? // 結果不明或者還未完成測試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]可以用來表明某個測試方法預期拋出某個異常,并且只有真的拋出異常時才通過測試。比如下面:

[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

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • 一、全局參數(shù)設置:去掉材質(zhì)最大深度的勾選,這時材質(zhì)反射的最大深度會采用默認的設置,一般為5,因為是最終渲染所以反射...
    壹米玖坤閱讀 5,261評論 0 4
  • 歡樂頌大結局了。 兩季歡樂頌我一集沒落,且不管我是多么無聊才會追那么長的電視劇打發(fā)時光,也不管我是多么不合格的媽媽...
    木子的一生閱讀 306評論 0 0
  • 第一章 “磚來!” 一聲吆喝穿透嘈雜,直擊滿頭大汗的副工的耳朵。副工“嗨”地提氣,抱起一塊土磚...
    耿志富閱讀 421評論 0 0
  • 南安一直覺得,對事,對人都要有自己的見解,別聽風就是雨。有時候起浪的因素不是風,而是那些在岸邊觀海的人。
    朱朱xxxx閱讀 317評論 0 0

友情鏈接更多精彩內(nèi)容