<?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>Guideline | 酷 壳 - CoolShell</title>
	<atom:link href="https://coolshell.cn/tag/guideline/feed" rel="self" type="application/rss+xml" />
	<link>https://coolshell.cn</link>
	<description>享受编程和技术所带来的快乐 - Coding Your Ambition</description>
	<lastBuildDate>Mon, 27 Jul 2009 08:24:57 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2</generator>
	<item>
		<title>一些单元测试的Guideline</title>
		<link>https://coolshell.cn/articles/1192.html</link>
					<comments>https://coolshell.cn/articles/1192.html#respond</comments>
		
		<dc:creator><![CDATA[陈皓]]></dc:creator>
		<pubDate>Mon, 27 Jul 2009 08:24:57 +0000</pubDate>
				<category><![CDATA[技术读物]]></category>
		<category><![CDATA[流程方法]]></category>
		<category><![CDATA[CppUnit]]></category>
		<category><![CDATA[Guideline]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Unit Test]]></category>
		<category><![CDATA[UT]]></category>
		<category><![CDATA[xUnit]]></category>
		<guid isPermaLink="false">http://coolshell.cn/?p=1192</guid>

					<description><![CDATA[<p>Jimmy Bogard 曾经写过一篇文章： 《从单元测试中获益》，这这篇文章中给出了下面三条规则： “测试名应该从用户的角度描述是什么和为什么” – 这样一来...</p>
<p class="read-more"><a class="btn btn-default" href="https://coolshell.cn/articles/1192.html"> Read More<span class="screen-reader-text">  Read More</span></a></p>
The post <a href="https://coolshell.cn/articles/1192.html">一些单元测试的Guideline</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></description>
										<content:encoded><![CDATA[<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3415450859608158"
     crossorigin="anonymous"></script>Jimmy Bogard 曾经写过一篇文章： 《<a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/12/18/getting-value-out-of-your-unit-tests.aspx">从单元测试中获益</a>》，这这篇文章中给出了下面三条规则：</p>
<ol>
<li>“<strong>测试名应该从用户的角度描述是什么和为什么</strong>” – 这样一来，程序员可以从名字就可以知道用户需要什么样的软件行为。</li>
<li>“<strong>测试也是代码，同样也需要我们更多的爱</strong>” – 真实运行在生产环境下的代码不仅仅只是我们需要去关心和花心思的代码。对于单元测试中的代码同样也需要易读易维护，以及可重用的特性。“<em>我非常痛恨那些又长又复杂的测试代码，如果一个测试需要30行的单元测试代码，请把其放在一个方法中。一个长的测试步骤只会激怒程序员。如果你在正式的代码中都没有这么长的代码，那么为什么我们需要在测试代码中容忍这样的情形呢？</em>”</li>
<li>“<strong>不要只用一种固定的模式或组织风格</strong>”<em> – </em>有些时候，对于一些特殊的测试案例，标准的类设计模式，或一个固有的测试装置可能并不能有效的工作。</li>
</ol>
<p><span id="more-1192"></span></p>
<p><a href="http://tech.groups.yahoo.com/group/testdrivendevelopment/message/31412">Lior Friedman</a> 加上： “第0条 &#8211; 测试应该只测试单元其外部的行为，而不是内部的结构”。或者说，只测试对一个单元的期望，而不是这个单元的构成。</p>
<p><a href="http://groups.google.com/group/nunit-discuss/msg/56c9d75647731502?hl=en">Ravichandran Jv</a> 也加上了他的条例：</p>
<ol>
<li>一个测试一个断言（如果可能）。 </li>
<li>如果在测试中有“if else” 的语句，请把if和else两个分支拆分成两个测试案例。 </li>
<li>如果一个测试案例中也有if else 分枝，那么这个测试案例也需要被重构。</li>
<li>测试案例的命名代表了这种测试的类型。例如：TestMakeReservation() 和TestMakeNoReservation()是不一样的类型。</li>
</ol>
<p><a href="http://groups.google.com/group/nunit-discuss/msg/fb335c19a8a44821?hl=en">Charlie Poole</a>，NUnit的作者，重述了“一个测试一个断言”成“一个逻辑断言Logical Assert” – 他说， “有时候，因为我们测试API的表现不足，你需要写多个物理的Assert才能达到一个完整的结果。许多使用NUnit框架API进行单元测试的开发，很不可能只使用一个Assert就完成了一个测试”。</p>
<p><a href="http://www.bryancook.net/2008/06/test-naming-conventions-guidelines.html">Bryan Cook</a> 也提供了一个不错的可供考虑的列表：</p>
<ol>
<li>做到：对Fixture一致地命名</li>
<li>做到：使用namespace</li>
<li>做到：测试方法的命名和Setup/TearDown 一致</li>
<li>考虑：分离你的测试和开发代码</li>
<li>做到：测试的命令和被测试的功能一致</li>
<li>考虑：使用&#8221;Cannot&#8221; 前缀命名期望的异常</li>
</ol>
<p>Bryan 有超过 <a href="http://www.bryancook.net/2008/06/test-naming-conventions-guidelines.html">一打的建议</a>。</p>
<p>最后，有些人建议大家读一下 Gerard Meszaros的书： “<a href="http://www.amazon.com/xUnit-Test-Patterns-Refactoring-Addison-Wesley/dp/0131495054/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1248380993&amp;sr=8-1">xUnit Test Patterns: Refactoring Test Code</a>”</p>
<p>文章：<a href="http://www.infoq.com/news/2009/07/Better-Unit-Tests" target="_blank">链接</a><!--



<p align="center"><a href= target=_blank><img decoding="async" src=""></a></p>





<p align="center"><img decoding="async" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.weixin.jpg"> <img decoding="async" loading="lazy" src="https://coolshell.cn/wp-content/uploads/2020/03/coolshell.mini_.jpg" width="300" height="300"> <br />关注CoolShell微信公众账号和微信小程序</p>

 

--></p>
<div style="margin-top: 15px; font-size: 16px;color: #cc0000;">
<p align="center"><strong>（转载本站文章请注明作者和出处 <a href="https://coolshell.cn/">酷 壳 &#8211; CoolShell</a> ，请勿用于任何商业用途）</strong></p>
</div>

<div class="wp_rp_wrap  wp_rp_vertical_m" id="wp_rp_first"><div class="wp_rp_content"><h3 class="related_post_title">相关文章</h3><ul class="related_post wp_rp"><li ><a href="https://coolshell.cn/articles/8593.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/28.jpg" alt="如何测试洗牌程序" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8593.html" class="wp_rp_title">如何测试洗牌程序</a></li><li ><a href="https://coolshell.cn/articles/8209.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2012/09/fight-150x150.jpg" alt="“单元测试要做多细？”" width="150" height="150" /></a><a href="https://coolshell.cn/articles/8209.html" class="wp_rp_title">“单元测试要做多细？”</a></li><li ><a href="https://coolshell.cn/articles/67.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/21.jpg" alt="几个你可能从来没有用过的HTML标识" width="150" height="150" /></a><a href="https://coolshell.cn/articles/67.html" class="wp_rp_title">几个你可能从来没有用过的HTML标识</a></li><li ><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/24.jpg" alt="到处都是Unix的胎记" width="150" height="150" /></a><a href="https://coolshell.cn/articles/1532.html" class="wp_rp_title">到处都是Unix的胎记</a></li><li ><a href="https://coolshell.cn/articles/506.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/plugins/wordpress-23-related-posts-plugin/static/thumbs/1.jpg" alt="Windows下和程序员相关小工具" width="150" height="150" /></a><a href="https://coolshell.cn/articles/506.html" class="wp_rp_title">Windows下和程序员相关小工具</a></li><li ><a href="https://coolshell.cn/articles/2834.html" class="wp_rp_thumbnail"><img src="https://coolshell.cn/wp-content/uploads/2010/08/super_mario-150x150.jpg" alt="史上最烂的超级玛丽" width="150" height="150" /></a><a href="https://coolshell.cn/articles/2834.html" class="wp_rp_title">史上最烂的超级玛丽</a></li></ul></div></div>The post <a href="https://coolshell.cn/articles/1192.html">一些单元测试的Guideline</a> first appeared on <a href="https://coolshell.cn">酷 壳 - CoolShell</a>.]]></content:encoded>
					
					<wfw:commentRss>https://coolshell.cn/articles/1192.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
