所以我開始寫的我寫有用的方法類庫,拿起多年來,我會用兩個代碼例子入手,然後問我具體的問題:如何開始使用單元測試?總共n00b的問題,想法?
我還喜歡說這是而不是其他一些的副本,「我在哪裏開始單元測試問題。」
檢查網絡連接(不上網,只是netwok)
public static Boolean IsNetworkConnected()
{
Boolean ret = false;
try
{
String HostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry thisHost = System.Net.Dns.GetHostEntry(HostName);
String thisIpAddr = thisHost.AddressList[0].ToString();
ret = thisIpAddr != System.Net.IPAddress.Parse("127.0.0.1").ToString();
}
catch (Exception)
{
return false;
}
return ret;
}
而且我IsValiEmail方法(注意,我沒有寫的正則表達式)
public const String MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
public static bool IsValidEmail(string email)
{
if (email != null && email != string.Empty)
return Regex.IsMatch(email, MatchEmailPattern);
else
return false;
}
所以,我的問題是怎麼做的我測試這些方法實際上工作,顯然我想開始更多我的代碼比這些快速示例更復雜的單元測試。
如果可能,我想避免安裝其他工具/框架,但我願意接受您的想法。
更新
凡應該這個新的單元測試代碼(通過鏈接已經發布)住?在同一個大會?單獨的組裝?
+1。這是一本很好的起始書。 – TrueWill 2009-11-05 03:53:42
+1同意,也是一本很棒的書,一旦你掌握了基礎知識,但想獲得可維護的測試套件。 – Mathias 2009-11-05 04:00:08
幾乎任何羅伊奧謝羅夫用單元測試做的事情都是金子! – 2009-11-05 04:23:47