您的位置:首页 > 职场人生

不喜欢写测试的朋友看过来,与你分享写测试的经验 做一个爱写测试的程序员

2013-06-28 20:10 477 查看
自从掌握了单元测试的要领之后,经常写测试,做测试,也非常喜欢做单元测试。我的文章《数据采集:完美下载淘宝Ip数据库简单的程序节省60元人民币而不必购买数据库》中的代码,也是个测试方法,源代码在QQ群中公布过。
现在把它公开给大家,分享这个Ip地址信息下载功能的测试代码:
[TestMethod]
publicvoidIPLibraryTest()
{
stringwhole="211.64.0-255.0-255";
IPRangeipRange=newIPRange(whole);
List<IPAddress>addresses=ipRange.GetAllIP()asList<IPAddress>;
DataTabletable=newDataTable("Ip");
JavaScriptSerializerserializer=newJavaScriptSerializer();
stringurl=string.Empty,json=string.Empty;
Parallel.ForEach(addresses,(address)=>
{
url=string.Format("http://ip.taobao.com/service/getIpInfo.php?ip={0}",address.ToString());
json=string.Empty;
try
{
json=Convert.ToString(NetworkHelper.PostRequest(url));
}
catch(Exception)
{

}
Objectobj2=serializer.DeserializeObject(json);
if(string.IsNullOrWhiteSpace(json)||obj2==null)
return;

Dictionary<String,Object>dictionary=(Dictionary<String,Object>)obj2;

foreach(KeyValuePair<string,object>valuePairindictionary)
{
if(valuePair.Value.GetType()==typeof(Dictionary<string,object>))
{
Dictionary<String,Object>subdictionary=valuePair.ValueasDictionary<String,Object>;
lock(table)
{
if(table.Columns.Count==0)
{
foreach(KeyValuePair<string,object>pairinsubdictionary)
{
table.Columns.Add(pair.Key,typeof(string));
}
}

DataRowrow=table.NewRow();
foreach(KeyValuePair<string,object>pairinsubdictionary)
{
row[pair.Key]=pair.Value;
}
table.Rows.Add(row);
}
}
}
});

stringdatabaseFile=@"H:\Development\Source\Test\Database\64IpLibrary.accdb";
OleDbHelperaccessHelper=newOleDbHelper(databaseFile);
accessHelper.AppendData(table);
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}程序中的不合理的地方,欢迎批评指正。
三个工具的组合VisualStudio+Resharper+VisualSourceSafe,经过几年的积累,累积了大量的测试代码。
VisualStudio内置的MSTest本身好用,NUnit也可以,这是测试的基础工具,再配合Reshaprer的测试管理器,在方法前面点一个小按钮,即可以调试或是运行的方式开始测试,相当方便。最后,累积的测试代码,直接提交到源代码管理工具中,备以后查验。
我们开发中经常遇到这样的情景,有一个小方法不知道是否正确,需要测试一下。不需要复杂的用户输入的功能,可以开一个控制台程序,执行测试,如用户输入复杂,则需要开一个WindowsForms程序。测试完成后,把可用的功能代码拷贝走,剩下的测试程序通常会扔掉。在没有学会MSTest之前,我经常这么做。等到以后发现有问题时,再回来来找当初的测试情景和代码,传递的参数数据,已经无从得知。
经过这种痛苦之后,我开始尝试单元测试,把要测试的代码和数据都保存一个Test项目中,每个解决方案都跟一个测试项目,用于执行测试,保存测试参数:



经过若干年的积累,这里面积累了很多实用的代码。
比如在做许可验证时,生成公匙和私匙,就下面二行代码,于是写成一个测试方法,保存在我的源代码服务器中
[TestMethod]
publicvoidSolutionValidationTest()
{
stringpublickey=RSACryptionHelper.GeneratePublickKey(false);
stringprivateKey=RSACryptionHelper.GeneratePublickKey(true);
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}这种代码库的积累,如果回忆起来,比查MSDN还快。
再比如,我的许可证机制是以SignatureXml为基础的,下面的方法可以测试许可证文件,验证它的正确性。
[TestMethod]
publicvoidSolutionLicenseValidationTest()
{
SolutionLicenselicense=SolutionLicense.ReadLicense();
stringlicenseFile=@"H:\Development\Build\License.lic";

StreamReaderreader=newStreamReader(licenseFile,Encoding.UTF8,true);

XmlTextReaderxmlReader=newXmlTextReader(licenseFile);
XmlDocumentdoc=newXmlDocument();
doc.Load(xmlReader);
doc.Load(reader);

license=SolutionLicense.ReadLicense();
license.VerifyLicense();
}
有时候真的很偷懒,不想写个GUI程序,就用这种方法来进行。调试过程中,如果有异常,MSTest会中断运行,显示异常的原因。用NUnit写的测试也一样的效果。有了Resharper的帮助,做代码的单元测试时,你完全像是在做功能测试一样,告诉VS我想运行一个方法,在方法名称前点击Run或是Debug即可。



行号左边的那个小按钮,对测试的运行和调试起到关键性的作用,极大的简化了测试代码的运行和检测。
有时候,我并不想调用Assert去判断值的真或假,比如上面的生成许可证文件,我去硬盘里面看看,检查一下内容也可以判断,自动化的步骤是加一个Assert(true,File.Exist(fileName)),但是由决定权在你手里,能把代码测试好,功能调试好就可以收工回家。

通俗的理解,单元测试的二个主要作用:

1保存API接口或是功能的调用方式和参数值,以留作改善或是问题查找的源头

2记录对新技术的学习,掌握情况,供快速参考。

Lambda表达式代表一个匿名方法,如果调用这个方法,对它求值呢,参考下面的代码例子
[TestMethod]
publicvoidTraceTestMethod()
{
Expression<Func<DateTime>>expr=()=>DateTime.Now.AddDays(1);
Func<DateTime>tomorrow=expr.Compile();
Console.WriteLine(tomorrow());

}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}看了这个例子后,肯清楚的解释了步骤,照葫芦化瓢,一下子就可以应用到工作中。
再来看几个扩展方法的例子,看下面的代码,生成字节数
[TestMethod]
publicvoidByteTest()
{
varkb=1.KB();
varmb=1.MB();
vargb=1.GB();
vartb=1.TB();
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}追综到源代码中,原来是这样的几个扩展方法:
///<summary>
///Kilobytes
///</summary>
///<paramname="value"></param>
///<returns></returns>
publicstaticintKB(thisintvalue)
{
returnvalue*1024;
}

///<summary>
///Megabytes
///</summary>
///<paramname="value"></param>
///<returns></returns>
publicstaticintMB(thisintvalue)
{
returnvalue.KB()*1024;
}

///<summary>
///Gigabytes
///</summary>
///<paramname="value"></param>
///<returns></returns>
publicstaticintGB(thisintvalue)
{
returnvalue.MB()*1024;
}

///<summary>
///Terabytes
///</summary>
///<paramname="value"></param>
///<returns></returns>
publicstaticlongTB(thisintvalue)
{
return(long)value.GB()*(long)1024;
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}测试代码是为自己而写,为自己掌握这项技术,熟悉这下技术,记的笔记。
VisualStudio本身就是个很好的代码收藏工具,有最好的编辑器,调试器,再以一个源代码管理工具作为辅助,这就组合成一个很好的代码收藏工具。看了我这篇文章之后,你可能再也不想用代码收藏工具了。我鼓励你把它扔掉,把收藏的代码片段直接保存在一个测试项目中,坚持积累,积跬步而行千里路,庖丁解牛,游刃有余。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐