【发布时间】:2009-11-15 21:21:52
【问题描述】:
所以我开始编写一个包含我多年来编写和学习的有用方法的类库,我将从两个代码示例开始,然后提出我的具体问题:
我还想说明这不是其他一些问题的重复,“我从哪里开始单元测试问题。”
检查网络连接(不是互联网,只是网络)
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;
}
所以,我的问题是如何测试这些方法是否真正有效,显然我想开始对我的代码进行更多单元测试,这比这些快速示例更复杂。
如果可能,我想避免安装额外的工具/框架,但我愿意接受你的想法。
更新
应该这个新的单元测试代码(通过已经发布的链接)在哪里?在同一个程序集中?单独组装?
【问题讨论】:
标签: c# .net unit-testing