[英]Unit testing a simple class in c# with vs2012
我正在嘗試對c#VS 2012中的一個簡單函數(編譯為庫)進行unit test
。
public static class Configuration
{
public static T DeSerialize<T>(string filePath)
{
if (!System.IO.File.Exists(filePath))
{
throw new System.IO.FileNotFoundException(filePath);
}
using (Stream reader = new FileStream(filePath, FileMode.Open))
{
var serializer = new System.Xml.Serialization.XmlSerializer(T);
return (T)serializer.Deserialize(reader);
}
}
}
[TestClass]
public class Test
{
[TestMethod]
public void deserializationTest()
{
var something = Configuration.DeSerialize<Item>(@"d:\CoffeeShop.Items.config");
Console.WriteLine(something);
}
}
但是,我堅持what to do next?
。 我namespaces should I import
什么namespaces should I import
。 非常感謝任何指針。
您應該使用Microsoft.VisualStudio.TestTools.UnitTesting
命名空間,該命名空間包含MSTest框架類(如TestClass
和TestMethod
屬性所暗示的那樣使用)。 然后,您應該使用Assert
類的方法來驗證您的測試結果。 您的代碼可能看起來像這樣:
[TestMethod]
public void deserializationTest()
{
var something = Configuration.DeSerialize<Item>(@"d:\CoffeeShop.Items.config");
Assert.AreEqual("expected item name", something.Name);
}
此測試驗證DeSerialize
調用返回的Item
Name
屬性等於"expected item name"
。 當然,這只是您的Item
類的外觀的一個假設,但是您可能會明白要點。
如果您開始在Visual Studio中進行單元測試,則本教程可能會派上用場: http : //msdn.microsoft.com/zh-cn/library/ms182532.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.