<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>进化的测试 &#187; 代码覆盖</title>
	<atom:link href="http://magustest.com/blog/tag/%e4%bb%a3%e7%a0%81%e8%a6%86%e7%9b%96/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>关注软件测试，白盒测试，自动化测试，性能测试</description>
	<lastBuildDate>Wed, 02 Jun 2010 16:12:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>用VSPerfMon在测试ASP.NET程序的过程中收集代码覆盖率信息</title>
		<link>http://magustest.com/blog/softwaretesting/collecting-aspnet-code-coverage-date-using-vsperfmon/</link>
		<comments>http://magustest.com/blog/softwaretesting/collecting-aspnet-code-coverage-date-using-vsperfmon/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 15:27:20 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[软件测试]]></category>
		<category><![CDATA[Profiler]]></category>
		<category><![CDATA[VSTS]]></category>
		<category><![CDATA[代码覆盖]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=454</guid>
		<description><![CDATA[前两篇文章介绍了VSTS性能分析工具Profiler和如何用Profiler发现性能问题并且进行优化。今天想分享一下如何在测试ASP.NET程序的过程中获取代码覆盖率的信息。 设想如下场景，测试人员介入到项目的需求，分析，设计的阶段，得到了很多有用的文档和信息。接下来就到了编码的阶段，在这个时候测试工程师开始根据前面获得的信息来设计测试用例，做用例评审，力求测试用例覆盖能覆盖系统的每一个角落。不幸的是，系统实在是太大，没有人能确切地知道手上的测试用例对系统的覆盖（这里指定为代码的行覆盖）能达到什么样的程度。对于系统的关键部分，没有人希望在经过几轮测试以后，还有相当部分的代码是没有被执行过的。 解决这个问题的其中一个方法，就是对关键部分的代码进行代码覆盖率的统计。平时我做单元测试和接口测试的时候，得益于IDE的帮助，代码覆盖率信息很容易就能得到，但是对于部署在IIS上的ASP.NET代码，又如何去收集代码覆盖率信息呢？曾经找过好多.NET的代码覆盖率工具，没有合适的。不过就在最近，让我发现了VSPerfMon，VSTS自带的性能数据收集工具。真是众里寻他千百度，蓦然回首，那工具却在灯火阑珊处。 1. 首先要对需要进行代码覆盖率信息收集的二进制文件(DLL, EXE)用VSInstr进行处理，记得带上/COVERAGE参数 VSInstr &#8220;D:\Websites\somewhere\bin\MySpace.Web.UserHome.dll&#8221; /COVERAGE 2. 停掉IIS iisreset /stop 3. 用VSPerfClrEnv 设置相关的环境变量 VSPerfClrEnv /globaltraceon 4. 启动VSPerfMon；参数/COVERAGE代表本次监控收集的数据是代码覆盖率；/user:&#8221;NETWORK SERVICE&#8221;，指定用户名;/CS 启用跨域会话分析；/output:&#8221;d:\magus\0422.coverage&#8221;，指定输出文件的名字和路径，记得用.coverage作为扩展名。 VSPerfMon /COVERAGE /user:&#8221;NETWORK SERVICE&#8221; /CS /output:&#8221;d:\magus\0422.coverage&#8221; 在运行完上面这条命令以后，CMD窗口就提示： Started in Stand Alone Mode Filename: d:\magus\0422.coverage 这时候需要打开一个新的CMD进行余下的操作(原来的CMD就叫CMD_A，新开的就叫CMD_B吧) 5. 停止收集代码覆盖率信息数据，因为不想让代码覆盖率信息受到影响。 VSPerfCmd /globaloff 6. 启动IIS iisreset /start 7. 对ASP.NET程序进行预热 8. 重新开启代码覆盖率信息的收集 VSPerfCmd /globalon 9. 运行测试用例，手工OR自动都可以 10. 停止代码覆盖率信息的收集 [...]


Related posts:<ol><li><a href='http://magustest.com/blog/net/intruduction-to-vsts-profile/' rel='bookmark' title='Permanent Link: VSTS性能分析工具Profiler的介绍'>VSTS性能分析工具Profiler的介绍</a></li>
<li><a href='http://magustest.com/blog/net/using-vsts-profiler-find-bottleneck-and-optimize/' rel='bookmark' title='Permanent Link: 用VSTS Profiler发现性能问题并且进行优化实例'>用VSTS Profiler发现性能问题并且进行优化实例</a></li>
<li><a href='http://magustest.com/blog/net/tips-debugging-asp-net-on-iis/' rel='bookmark' title='Permanent Link: 调试部署在IIS上的ASP.NET程序实用小技巧'>调试部署在IIS上的ASP.NET程序实用小技巧</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>前两篇文章介绍了<a href="http://magustest.com/blog/net/intruduction-to-vsts-profile/" target="_blank">VSTS性能分析工具Profiler</a>和<a href="http://magustest.com/blog/net/using-vsts-profiler-find-bottleneck-and-optimize/" target="_blank">如何用Profiler发现性能问题并且进行优化</a>。今天想分享一下如何在测试ASP.NET程序的过程中获取代码覆盖率的信息。</p>
<p>设想如下场景，测试人员介入到项目的需求，分析，设计的阶段，得到了很多有用的文档和信息。接下来就到了编码的阶段，在这个时候测试工程师开始根据前面获得的信息来设计测试用例，做用例评审，力求测试用例覆盖能覆盖系统的每一个角落。不幸的是，系统实在是太大，没有人能确切地知道手上的测试用例对系统的覆盖（这里指定为代码的行覆盖）能达到什么样的程度。对于系统的关键部分，没有人希望在经过几轮测试以后，还有相当部分的代码是没有被执行过的。</p>
<p>解决这个问题的其中一个方法，就是对关键部分的代码进行代码覆盖率的统计。平时我做单元测试和接口测试的时候，得益于IDE的帮助，代码覆盖率信息很容易就能得到，但是对于部署在IIS上的ASP.NET代码，又如何去收集代码覆盖率信息呢？曾经找过好多.NET的代码覆盖率工具，没有合适的。不过就在最近，让我发现了VSPerfMon，VSTS自带的性能数据收集工具。真是众里寻他千百度，蓦然回首，那工具却在灯火阑珊处。</p>
<p><strong>1. 首先要对需要进行代码覆盖率信息收集的二进制文件(DLL, EXE)用VSInstr进行处理，记得带上/COVERAGE参数</strong></p>
<p><em><span style="text-decoration: underline;">VSInstr &#8220;D:\Websites\somewhere\bin\MySpace.Web.UserHome.dll&#8221; /COVERAGE</span></em></p>
<p><strong>2. 停掉IIS</strong></p>
<p><em><span style="text-decoration: underline;">iisreset /stop</span></em></p>
<p><strong>3. 用VSPerfClrEnv 设置相关的环境变量</strong></p>
<p><em><span style="text-decoration: underline;">VSPerfClrEnv /globaltraceon</span></em></p>
<p><strong>4. 启动VSPerfMon；参数/COVERAGE代表本次监控收集的数据是代码覆盖率；/user:&#8221;NETWORK SERVICE&#8221;，指定用户名;/CS 启用跨域会话分析；/output:&#8221;d:\magus\0422.coverage&#8221;，指定输出文件的名字和路径，记得用.coverage作为扩展名。</strong><br />
<span id="more-454"></span><br />
<em><span style="text-decoration: underline;">VSPerfMon /COVERAGE /user:&#8221;NETWORK SERVICE&#8221; /CS /output:&#8221;d:\magus\0422.coverage&#8221;</span></em></p>
<p>在运行完上面这条命令以后，CMD窗口就提示：</p>
<blockquote><p>Started in Stand Alone Mode<br />
Filename: d:\magus\0422.coverage</p></blockquote>
<p>这时候<span style="text-decoration: underline;"><strong>需要打开一个新的CMD进行余下的操作(原来的CMD就叫CMD_A，新开的就叫CMD_B吧)</strong></span></p>
<p><strong>5. 停止</strong><strong>收集</strong><strong>代码覆盖率信息数据，因为不想让代码覆盖率信息受到影响。</strong></p>
<p><em><span style="text-decoration: underline;">VSPerfCmd /globaloff</span></em></p>
<p><strong>6. 启动IIS</strong></p>
<p><em><span style="text-decoration: underline;">iisreset /start</span></em></p>
<p><strong>7. 对ASP.NET程序进行预热</strong></p>
<p><strong>8. 重新开启代码覆盖率信息的收集</strong></p>
<p><em><span style="text-decoration: underline;">VSPerfCmd /globalon</span></em></p>
<p><strong>9. 运行测试用例，手工OR自动都可以</strong></p>
<p><strong>10. 停止代码覆盖率信息的收集</strong></p>
<p><em><span style="text-decoration: underline;">VSPerfCmd /globaloff</span></em></p>
<p><strong>11. 停止IIS</strong></p>
<p><em><span style="text-decoration: underline;">iisreset /stop</span></em></p>
<p>这时候在CMD_A中会提示“UnRegistering process (7784)”，还是保持CMD_A不动，继续操作CMD_B</p>
<p><strong>12. 结束代码覆盖率的收集，并且生成.coverage文件</strong></p>
<p><em><span style="text-decoration: underline;">VSPerfCmd /shutdown</span></em></p>
<p>这时候在原来的CMD_A中就会出现如下信息：</p>
<blockquote><p>Shutting Down Named Pipe Server<br />
Shutting Down Buffer Writer<br />
Shutting Down Process Deamon</p></blockquote>
<p>如果没有看到错误提示信息，那么本次代码覆盖率收集就应该成功了。</p>
<p><strong>13. 在VSTS IDE中打开刚才生成的.coverage查看代码覆盖率。</strong></p>
<p>如图：</p>
<p><img class="alignnone size-full wp-image-455" title="coverage" src="http://magustest.com/blog/wp-content/uploads/2009/04/coverage1.png" alt="coverage" width="610" height="212" /></p>
<p>正如以前讨论过的，<a href="http://magustest.com/blog/softwaretesting/whiteboxtesting/100-percent-statement-coverage-not-enough/" target="_blank">100%的代码覆盖率是不够的</a>，那么我们在拿到代码覆盖率数据以后，应该看一下在运行完测试用例以后以后，系统的哪些代码还没有被执行过，然后回头设计一些新的测试用例来测试那些未被测试的代码。这就要求<a href="http://magustest.com/blog/softwaretesting/do-software-tester-needs-to-know-how-to-code/" target="_blank">测试工程师要能看懂代码</a>了。</p>
<p>本文思路得益于大半年前看到这篇博客“<a href="http://www.51testing.com/?uid-13997-action-viewspace-itemid-87396" target="_blank">如何引入代码覆盖率度量提高测试质量</a>”，一直惦记于心，终于找到了适用于.NET程序的解决方法了，呵呵。</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F&amp;t=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F&title=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F&title=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F&amp;title=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E7%94%A8VSPerfMon%E5%9C%A8%E6%B5%8B%E8%AF%95ASP.NET%E7%A8%8B%E5%BA%8F%E7%9A%84%E8%BF%87%E7%A8%8B%E4%B8%AD%E6%94%B6%E9%9B%86%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E7%8E%87%E4%BF%A1%E6%81%AF%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fsoftwaretesting%2Fcollecting-aspnet-code-coverage-date-using-vsperfmon%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/net/intruduction-to-vsts-profile/' rel='bookmark' title='Permanent Link: VSTS性能分析工具Profiler的介绍'>VSTS性能分析工具Profiler的介绍</a></li>
<li><a href='http://magustest.com/blog/net/using-vsts-profiler-find-bottleneck-and-optimize/' rel='bookmark' title='Permanent Link: 用VSTS Profiler发现性能问题并且进行优化实例'>用VSTS Profiler发现性能问题并且进行优化实例</a></li>
<li><a href='http://magustest.com/blog/net/tips-debugging-asp-net-on-iis/' rel='bookmark' title='Permanent Link: 调试部署在IIS上的ASP.NET程序实用小技巧'>调试部署在IIS上的ASP.NET程序实用小技巧</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/softwaretesting/collecting-aspnet-code-coverage-date-using-vsperfmon/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>微软ZUNE死机原因&#8211;单元测试百分百语句覆盖率是不够的</title>
		<link>http://magustest.com/blog/whiteboxtesting/100-percent-statement-coverage-not-enough/</link>
		<comments>http://magustest.com/blog/whiteboxtesting/100-percent-statement-coverage-not-enough/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 08:22:33 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[白盒测试]]></category>
		<category><![CDATA[代码覆盖]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=269</guid>
		<description><![CDATA[微软的30GB版本Zune在08年的最后一天出现了大规模死机的现象，原因其实就是一行代码原本应该写为大于等于，但是实际上写成了大于。下面来看看具体的代码，第5行就是导致死机的代码。 while (days > 365) { if (DateTime.IsLeapYear(year)) { if (days > 366) { days -= 366; year += 1; } } else { days -= 365; year += 1; } } 这是ZUNE处理时间的一段代码，每年的最后一天（days=365）向第二年的第一天（days要加1）过度的时候会做一个处理。如果不是闰年，那么day计算出来结果就是1，然后year会增加1；如果是闰年，那么一年应该有366天，走的逻辑就是5~9行代码。2008年31号ZUNE死机的原因就是，days=366，years=2008，会出现死循环。下面来写一个单元测试，来测试一下这一段代码块。 [TestMethod()] public void AddDayTest() { int[] arrdays = new int[] { -1, 0, 1, 66, 365, 367, 367 }; int[] arryear = [...]


Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/' rel='bookmark' title='Permanent Link: 自动化白盒测试工具PEX应用实例 &#8211; 发现ZUNE死机的BUG'>自动化白盒测试工具PEX应用实例 &#8211; 发现ZUNE死机的BUG</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/unit-test-pattern/' rel='bookmark' title='Permanent Link: 单元测试中的常用测试模式'>单元测试中的常用测试模式</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/refactoring-unit-test/' rel='bookmark' title='Permanent Link: 测试代码重构实例'>测试代码重构实例</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cnbeta.com/articles/73748.htm" target="_blank">微软的30GB版本Zune在08年的最后一天出现了大规模死机的现象</a>，原因其实就是一行代码原本应该写为大于等于，但是实际上写成了大于。下面来看看具体的代码，第5行就是导致死机的代码。</p>
<pre lang="csharp" colla="+">while (days > 365)
{
	if (DateTime.IsLeapYear(year))
	{
		if (days > 366)
		{
			days -= 366;
			year += 1;
		}
	}
	else
	{
		days -= 365;
		year += 1;
	}
}</pre>
<p><span id="more-269"></span><br />
这是ZUNE处理时间的一段代码，每年的最后一天（days=365）向第二年的第一天（days要加1）过度的时候会做一个处理。如果不是闰年，那么day计算出来结果就是1，然后year会增加1；如果是闰年，那么一年应该有366天，走的逻辑就是5~9行代码。2008年31号ZUNE死机的原因就是，days=366，years=2008，会出现死循环。下面来写一个单元测试，来测试一下这一段代码块。</p>
<pre lang="csharp"  colla="+">[TestMethod()]
public void AddDayTest()
{
    int[] arrdays = new int[] { -1, 0, 1, 66, 365, 367, 367 };
    int[] arryear = new int[] { -1, 0, 1, 66, 2008, 2008, 367 };
    for (int i = 0; i > arrdays.Length; i++)
    {
        int days = arrdays[i];
        int year = arryear[i];
        Class1.AddDay(days, year);
    }
}</pre>
<p>一段很简单的测试代码，里面的测试数据不是很讲究，大家将就着看吧，取这些测试数据就能达到VSTS里面的语句覆盖率达到100%。如图：AddDay方法语句覆盖率已经达到100%</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/100_code_coverage_21.png"><img class="alignnone size-full wp-image-273" title="100_code_coverage_2" src="http://magustest.com/blog/wp-content/uploads/2009/01/100_code_coverage_21.png" alt="" width="500" height="58" /></a></p>
<p>下图是具体代码，整个方法体的代码都是浅蓝色，代码都被覆盖到了</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/100_code_coverage1.png"><img class="alignnone size-full wp-image-274" title="100_code_coverage" src="http://magustest.com/blog/wp-content/uploads/2009/01/100_code_coverage1.png" alt="" width="448" height="410" /></a></p>
<p>回头看看测试数据，虽然达到了100%语句覆盖，但是依然遗漏了“闰年，366天”这样一条数据，只要我们用这条测试数据来测试，就能发现错误了。</p>
<p>下面来说说用VSTS做单元测试，通过一些设置来帮助我们发现类似这个死循环的BUG。在VSTS里面，可以设置每个单元测试的超时时间，默认是30分钟，这样对于发现一些出现死循环的BUG不太有利。很多人都说过，单元测试的一个特点就是&#8211;快！因为测试只是检查一小个单元是否能正常工作。如果一个“单元测试”需要几分钟，那么几乎可以很肯定地说，这不是一个单元测试，这个测试肯定跟外界有很多交互，例如读取一个外部文件？读取数据库？请求一个web service等等。在Test run config里面，把超时时间设置为10秒，如果一个测试会导致死循环，那么10秒后，就会提示我们该单元测试出现超时，如图：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/test_config_modify1.png"><img class="alignnone size-full wp-image-276" title="test_config_modify" src="http://magustest.com/blog/wp-content/uploads/2009/01/test_config_modify1.png" alt="" width="500" height="355" /></a></p>
<p>设置好了以后运行一下单元测试：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>TestMethod<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddDayTest1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> days <span style="color: #008000;">=</span> <span style="color: #FF0000;">366</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">int</span> year <span style="color: #008000;">=</span> <span style="color: #FF0000;">2008</span><span style="color: #008000;">;</span>
    Class1.<span style="color: #0000FF;">AddDay</span><span style="color: #000000;">&#40;</span>days, year<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>出现结果：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/test_timeout1.png"><img class="alignnone size-full wp-image-278" title="test_timeout" src="http://magustest.com/blog/wp-content/uploads/2009/01/test_timeout1.png" alt="" width="500" height="117" /></a></p>
<p>很好，我们可以在比较短的时间内发现问题，而不是观察CPU占用率长期达到100%。</p>
<p>总结：现在的代码覆盖率工具，大部分都只有语句覆盖的功能，而语句覆盖 又是众多代码覆盖标准里面最弱的一种。有时候做白盒的测试（单元，集成……），会很容易掉进一个陷阱，就是看着代码覆盖率工具给出的结果，来提高自己测试代码覆盖率。这样代码的覆盖率上去了，但是代码的质量却不见的能有提高。以后做测试的时候，也应该把精力放在关键部分的代码逻辑的验证上，不要老盯着覆盖率往上涨。</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://magustest.com/blog/feed/" title="RSS"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F&amp;t=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84" title="Facebook"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F&title=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84" title="豆瓣"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban.png" title="豆瓣" alt="豆瓣" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.douban.com/recommend/?url=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F&title=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84&n=1" title="豆瓣九点"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/douban9.png" title="豆瓣九点" alt="豆瓣九点" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.friendfeed.com/share?title=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84&amp;link=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F" title="FriendFeed"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F&amp;title=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84" title="Live"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" class="sociable-hovers" /></a>
	<img src="http://magustest.com/blog/wp-content/plugins/sociable/images/ping.png" title="Ping.fm" alt="Ping.fm" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://shuqian.qq.com/post?jumpback=1&title=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84&uri=http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F" title="QQ书签"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/qq.png" title="QQ书签" alt="QQ书签" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=%E5%BE%AE%E8%BD%AFZUNE%E6%AD%BB%E6%9C%BA%E5%8E%9F%E5%9B%A0--%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%E7%99%BE%E5%88%86%E7%99%BE%E8%AF%AD%E5%8F%A5%E8%A6%86%E7%9B%96%E7%8E%87%E6%98%AF%E4%B8%8D%E5%A4%9F%E7%9A%84%20-%20http%3A%2F%2Fmagustest.com%2Fblog%2Fwhiteboxtesting%2F100-percent-statement-coverage-not-enough%2F" title="Twitter"><img src="http://magustest.com/blog/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>

<p>Related posts:<ol><li><a href='http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/' rel='bookmark' title='Permanent Link: 自动化白盒测试工具PEX应用实例 &#8211; 发现ZUNE死机的BUG'>自动化白盒测试工具PEX应用实例 &#8211; 发现ZUNE死机的BUG</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/unit-test-pattern/' rel='bookmark' title='Permanent Link: 单元测试中的常用测试模式'>单元测试中的常用测试模式</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/refactoring-unit-test/' rel='bookmark' title='Permanent Link: 测试代码重构实例'>测试代码重构实例</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/whiteboxtesting/100-percent-statement-coverage-not-enough/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
