<?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; PEX</title>
	<atom:link href="http://magustest.com/blog/tag/pex/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>软件测试，自动化测试，白盒测试，Python</description>
	<lastBuildDate>Thu, 17 May 2012 14:19:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>在测试项目中应用Pex的经验分享</title>
		<link>http://magustest.com/blog/whiteboxtesting/using-pex/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-pex</link>
		<comments>http://magustest.com/blog/whiteboxtesting/using-pex/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 02:28:40 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[白盒测试]]></category>
		<category><![CDATA[PEX]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=547</guid>
		<description><![CDATA[Pex是微软研究院开发的一个自动化白盒测试工具，前面我也写过一些博客来对Pex进行介绍，上周决定正式把Pex应用到真正的项目中，不过效果没有想象中的好，不过还是决定记录下一些心得。 1. 关于测试类名称和测试方法名称 为了把Pex和其他的单元测试方法区分开来，本人选择了把所有Pex的测试都以PexTest作为方法名和类名的前缀。因为Pex会根据已经写好的PUT(Parameterized Unit Test)生成一般的单元测试代码，规则可以自定义，一般都是PUT的名字然后跟上01，02，03……如果我们以PexTest作为前缀，那么就比较容易区分PexTest和普通的单元测试。 2. 每个PexTest都会生成一些普通的单元测试代码，这些代码都是存放在一个叫{TestMethodName}.g.cs的文件中，对于这个文件里面代码，不建议手动修改。 3. 在组织测试代码的时候，应该吧PexTest和一般的单元测试分开，如图： 4. 如果需要测试Singleton实例中的方法，需要给这个实例创建一个带有[PexFactoryMethod]属性的方法，来告诉Pex如何获得Singleton实例。例如： 1 2 3 4 5 6 7 8 public static partial class CommentProviderFactory &#123; &#91;PexFactoryMethod&#40;typeof&#40;CommentProvider&#41;&#41;&#93; public static object Create&#40;&#41; &#123; return CommentProvider.Instance; &#125; &#125; 5. Pex有时候会尝试着对.NET自带的代码也进行测试，如果遇到这种情况，可以在Pex Exploration Results窗口中把相关的方法标记为不检测。 6. 如果修改了PUT，那么原来在{TestMethodName}.g.cs自动生成的测试方法有可能没法运行，可以把这个文件删掉，然后让Pex重新生成测试。 Related posts: 用XSD自动生成XML对应的.NET实体类 利用序列化和反序列化实现深拷贝 PEX-.NET自动化白盒测试工具的介绍(1)
Related posts:<ol>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/' rel='bookmark' title='利用序列化和反序列化实现深拷贝'>利用序列化和反序列化实现深拷贝</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/' rel='bookmark' title='PEX-.NET自动化白盒测试工具的介绍(1)'>PEX-.NET自动化白盒测试工具的介绍(1)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><a href="http://research.microsoft.com/en-us/projects/Pex/" target="_blank">Pex</a>是微软研究院开发的一个自动化白盒测试工具，前面我也写过一些博客来对Pex进行介绍，上周决定正式把Pex应用到真正的项目中，不过效果没有想象中的好，不过还是决定记录下一些心得。</p>
<p>1. 关于测试类名称和测试方法名称</p>
<p>为了把Pex和其他的单元测试方法区分开来，本人选择了把所有Pex的测试都以PexTest作为方法名和类名的前缀。因为Pex会根据已经写好的PUT(Parameterized Unit Test)生成一般的单元测试代码，规则可以自定义，一般都是PUT的名字然后跟上01，02，03……如果我们以PexTest作为前缀，那么就比较容易区分PexTest和普通的单元测试。</p>
<p>2. 每个PexTest都会生成一些普通的单元测试代码，这些代码都是存放在一个叫{TestMethodName}.g.cs的文件中，对于这个文件里面代码，不建议手动修改。</p>
<p>3. 在组织测试代码的时候，应该吧PexTest和一般的单元测试分开，如图：</p>
<p><img class="alignnone size-full wp-image-548" title="test" src="http://magustest.com/blog/wp-content/uploads/2009/06/test1.png" alt="test" width="270" height="192" /></p>
<p>4. 如果需要测试Singleton实例中的方法，需要给这个实例创建一个带有[PexFactoryMethod]属性的方法，来告诉Pex如何获得Singleton实例。例如：<br />
<span id="more-547"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> CommentProviderFactory
<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#91;</span>PexFactoryMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>CommentProvider<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">object</span> Create<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> CommentProvider<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>5. Pex有时候会尝试着对.NET自带的代码也进行测试，如果遇到这种情况，可以在Pex Exploration Results窗口中把相关的方法标记为不检测。</p>
<p>6. 如果修改了PUT，那么原来在{TestMethodName}.g.cs自动生成的测试方法有可能没法运行，可以把这个文件删掉，然后让Pex重新生成测试。</p>
<p>Related posts:<ol>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/net/serialize-deserialize-implement-deep-copy/' rel='bookmark' title='利用序列化和反序列化实现深拷贝'>利用序列化和反序列化实现深拷贝</a></li>
<li><a href='http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/' rel='bookmark' title='PEX-.NET自动化白盒测试工具的介绍(1)'>PEX-.NET自动化白盒测试工具的介绍(1)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/whiteboxtesting/using-pex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>自动化白盒测试工具PEX应用实例 &#8211; 发现ZUNE死机的BUG</title>
		<link>http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-pex-in-unit-test</link>
		<comments>http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 06:26:34 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[白盒测试]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[PEX]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=306</guid>
		<description><![CDATA[在12月的时候，我在博客里面介绍了一个自动化单元测试工具&#8211;PEX。在上一篇博客中，又讲述了微软ZUNE在闰年最后一天死机的原因。现在把这两篇文章串起来，跟大家分享一下如何用PEX帮助改善代码的质量。 首先来看一下用PEX测试ZUNE处理日期的方法的结果，如图： 大家可以复习一下被测代码： 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 while &#40;days &#62; 365&#41; &#123; if &#40;DateTime.IsLeapYear&#40;year&#41;&#41; &#123; if &#40;days &#62; 366&#41; &#123; days -= 366; year += 1; &#125; &#125; else &#123; days -= 365; year += 1; &#125; &#125; 从图中可以看到PEX总共运行了73次，产生了5个测试用例，其中2个测试用例是运行失败的，还有2个测试用例在运行过程中超出了设定的运行次数（其实这2个用例就找到了ZUNE的那个BUG）。我们分别来看看PEX生成的几个用例。第一行测试数据是days=0, year=1；这条测试数据是能够通过测试的。第二行和第三行测试数据是测试运行失败的，days是一个很大的整数，这两个测试虽然是失败的测试，但是不需要关心，因为实际中日期不可能到达那个数值，所以可以认为这是无效的测试数据。再来看最后的两条测试数据，都是超过了单个测试运行的次数，可以怀疑其中有死循环。 经过PEX初步的诊断，我们需要给PEX增加一些限制，使得生成的测试数据更加有代表性，更加有意义。 1 2 <a href='http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/' class='excerpt-more'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p>在12月的时候，我在博客里面介绍了一个<a href="http://magustest.com/blog/softwaretesting/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/" target="_blank">自动化单元测试工具&#8211;PEX</a>。在上一篇博客中，又讲述了<a href="http://magustest.com/blog/softwaretesting/whiteboxtesting/100-percent-statement-coverage-not-enough/" target="_blank">微软ZUNE在闰年最后一天死机的原因</a>。现在把这两篇文章串起来，跟大家分享一下如何用PEX帮助改善代码的质量。</p>
<p>首先来看一下用PEX测试ZUNE处理日期的方法的结果，如图：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/no-assumtion1.png"><img class="alignnone size-full wp-image-307" title="no-assumtion" src="http://magustest.com/blog/wp-content/uploads/2009/01/no-assumtion1.png" alt="" width="500" height="198" /></a><br />
<span id="more-306"></span><br />
大家可以复习一下被测代码：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>days <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">365</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">IsLeapYear</span><span style="color: #008000;">&#40;</span>year<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>days <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">366</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			days <span style="color: #008000;">-=</span> <span style="color: #FF0000;">366</span><span style="color: #008000;">;</span>
			year <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">else</span>
	<span style="color: #008000;">&#123;</span>
		days <span style="color: #008000;">-=</span> <span style="color: #FF0000;">365</span><span style="color: #008000;">;</span>
		year <span style="color: #008000;">+=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>从图中可以看到PEX总共运行了73次，产生了5个测试用例，其中2个测试用例是运行失败的，还有2个测试用例在运行过程中超出了设定的运行次数（其实这2个用例就找到了ZUNE的那个BUG）。我们分别来看看PEX生成的几个用例。第一行测试数据是days=0, year=1；这条测试数据是能够通过测试的。第二行和第三行测试数据是测试运行失败的，days是一个很大的整数，这两个测试虽然是失败的测试，但是不需要关心，因为实际中日期不可能到达那个数值，所以可以认为这是无效的测试数据。再来看最后的两条测试数据，都是超过了单个测试运行的次数，可以怀疑其中有死循环。</p>
<p>经过PEX初步的诊断，我们需要给PEX增加一些限制，使得生成的测试数据更加有代表性，更加有意义。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>PexMethod<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> abcd<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> days, <span style="color: #6666cc; font-weight: bold;">int</span> year<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	PexAssume<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>days <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">367</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	PexAssume<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>days <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	PexAssume<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>year <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">3001</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	PexAssume<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>year <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	Class1<span style="color: #008000;">.</span><span style="color: #0000FF;">AddDay</span><span style="color: #008000;">&#40;</span>days, year<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>在调用被测方法之前，是4条PexAssume语句，这4条语句可以告诉PEX，需要生成的测试数据中days是在1到369之间取值， year是在1到2999之间取值。有了这样的限制以后，PEX产生是结果会更加好，见下图：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/pex_result1.png"><img class="alignnone size-full wp-image-311" title="pex_result" src="http://magustest.com/blog/wp-content/uploads/2009/01/pex_result1.png" alt="" width="485" height="157" /></a></p>
<p>点击最后一条测试结果，查看Details：</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2009/01/test-case-for-endless-loop1.png"><img class="alignnone size-full wp-image-312" title="test-case-for-endless-loop" src="http://magustest.com/blog/wp-content/uploads/2009/01/test-case-for-endless-loop1.png" alt="" width="499" height="246" /></a></p>
<p>我们可以看到已经生成了普通的单元测试了。这个单元测试就可以留着以后做回归测试用了。</p>
<p>把他复制到相应的CS文件中，然后删除掉Ignore属性，Description属性可要可不要。最后测试如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>TestMethod<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> abcd24<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">abcd</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">366</span>, <span style="color: #FF0000;">400</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>PEX可以说是一个革命性的测试工具，如果有兴趣的朋友可以上<a href="http://social.msdn.microsoft.com/Forums/en-US/pex/threads/" target="_blank">MSDN论坛的PEX版块</a>学习。</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/whiteboxtesting/using-pex-in-unit-test/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PEX-.NET自动化白盒测试工具的介绍(1)</title>
		<link>http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=introduction-to-pex-automated-white-box-testing-for-dotnet</link>
		<comments>http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 08:20:22 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[白盒测试]]></category>
		<category><![CDATA[PEX]]></category>
		<category><![CDATA[单元测试]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=157</guid>
		<description><![CDATA[PEX的全称是(Program EXploration)，是一款在.NET下可应用的自动化白盒测试工具，来自于微软研究院。PEX通过分析代码来自动生成测试用例。对于程序里面的每一行代码，PEX都会尽可能地生成合适的输入值来达到提高覆盖率的目标。同时PEX还会分析代码中的分支，生成覆盖更多分支的测试代码（输入数据）；PEX在执行代码的同时会监控和分析代码的控制流和数据流，了解程序的行为。每运行完单一个测试以后，PEX会选择一条在前面的测试中没有覆盖到的路径，并且尝试执行它。这一切都是约束求解算法来实现的，官方文档中提到的是一个叫Z3的约束求解器。因为PEX了解被测代码内部结构和行为，所以它不是一个简单地输入随机参数的黑盒测试工具。PEX不会尝试枚举所有可能的输入，事实上也不可能枚举完所有的可能输入。 下面来看看如何利用PEX来帮助提高软件的质量吧。 1. 首先，安装PEX。PEX现在有2个Flavor，支持2个版本的Visual Studio，分别是VSTS2010和VSTS2008，这两个版本的PEX在功能上没有区别，只是在许可证上有区别。本文是以VSTS2008版本作为示例。 2. 建立一个C#的Class Library项目 3. 新建一个类，并且添加一个静态方法。这个方法是把输入的字符串格式化为首字母大写的格式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static string Capitalize&#40;string value&#41; &#123; var sb = new StringBuilder&#40;&#41;; bool word = false; // are we writing <a href='http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/' class='excerpt-more'>[...]</a>
No related posts.]]></description>
			<content:encoded><![CDATA[<p><a href="http://research.microsoft.com/Pex/" target="_blank">PEX</a>的全称是(Program EXploration)，是一款在.NET下可应用的自动化白盒测试工具，来自于微软研究院。PEX通过分析代码来自动生成测试用例。对于程序里面的每一行代码，PEX都会尽可能地生成合适的输入值来达到提高覆盖率的目标。同时PEX还会分析代码中的分支，生成覆盖更多分支的测试代码（输入数据）；PEX在执行代码的同时会监控和分析代码的控制流和数据流，了解程序的行为。每运行完单一个测试以后，PEX会选择一条在前面的测试中没有覆盖到的路径，并且尝试执行它。这一切都是约束求解算法来实现的，官方文档中提到的是一个叫Z3的约束求解器。因为PEX了解被测代码内部结构和行为，所以它不是一个简单地输入随机参数的黑盒测试工具。PEX不会尝试枚举所有可能的输入，事实上也不可能枚举完所有的可能输入。<br />
<span id="more-157"></span><br />
下面来看看如何利用PEX来帮助提高软件的质量吧。</p>
<p><strong><span style="color: #ff6600;">1. 首先，安装PEX。</span></strong>PEX现在有2个Flavor，支持2个版本的Visual Studio，分别是VSTS2010和VSTS2008，这两个版本的PEX在功能上没有区别，只是在许可证上有区别。本文是以VSTS2008版本作为示例。</p>
<p><span style="color: #ff6600;"><strong>2. 建立一个C#的Class Library项目</strong> </span></p>
<div id="attachment_158" class="wp-caption alignnone" style="width: 510px"><a href="http://magustest.com/blog/wp-content/uploads/2008/12/create-a-class-project1.png"><img class="size-full wp-image-158" title="create-a-class-project" src="http://magustest.com/blog/wp-content/uploads/2008/12/create-a-class-project1.png" alt="create a class project" width="500" height="360" /></a><p class="wp-caption-text">create a class project</p></div>
<p><span style="color: #ff6600;"><strong>3. 新建一个类，并且添加一个静态方法。</strong><span style="color: #000000;">这个方法是把输入的字符串格式化为首字母大写的格式</span></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> Capitalize<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// are we writing a word</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var c <span style="color: #0600FF; font-weight: bold;">in</span> value<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsLetter</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>word<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToUpper</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">else</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsPunctuation</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'_'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong><span style="color: #ff6600;">4. 这时候，可以创建一个传统的单元测试，但有了PEX，我们希望用PEX</span></strong><br />
<img class="alignnone size-full wp-image-159" title="trandition-unit-test" src="http://magustest.com/blog/wp-content/uploads/2008/12/trandition-unit-test1.png" alt="" width="214" height="198" /></p>
<p><span style="color: #ff6600;"><strong>5. 在该静态方法体内，点击右键，选择“Run Pex Explorations”，就可以运行PEX</strong></span><br />
<a href="http://magustest.com/blog/wp-content/uploads/2008/12/run-pex-explorations1.png"><img class="alignnone size-full wp-image-160" title="run-pex-explorations" src="http://magustest.com/blog/wp-content/uploads/2008/12/run-pex-explorations1.png" alt="" width="360" height="194" /></a></p>
<p><strong><span style="color: #ff6600;">6. PEX会编译这个类，并且运行这个方法</span></strong>，监控测试的运行，对于这个简单的方法，很快就会获得结果。在标题栏可以看到，运行了123次测试，其中有1个测试是失败的；失败的用例就是图中被圈出来的那一条。<br />
<a href="http://magustest.com/blog/wp-content/uploads/2008/12/pex-result1.png"><img class="alignnone size-full wp-image-161" title="pex-result" src="http://magustest.com/blog/wp-content/uploads/2008/12/pex-result1.png" alt="" width="500" height="289" /></a></p>
<p><strong><span style="color: #ff6600;">7. 点击选中测试结果中有异常的测试行</span></strong>，会看到以下菜单，可以做以下操作：</p>
<p>(1)保存该测试为普通的单元测试<br />
(2)自动添加代码来避免该错误的发生<br />
(3)允许此异常在程序中出现<br />
(4)把所有信息发送到文本或者剪切板，这里所有信息包括Detail和Stack trace，如果TFS里面配置了Work Item，还能直接发送到相应的Work Item中</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2008/12/menu1.png"><img class="alignnone size-full wp-image-162" title="menu" src="http://magustest.com/blog/wp-content/uploads/2008/12/menu1.png" alt="" width="500" height="266" /></a></p>
<p><span style="color: #ff6600;"><strong>8. 现在我们让PEX帮我们修正一个错。</strong></span>选择Add Precondition，会看到如下菜单。上半部分告诉用户，PEX会做那些操作，下半部分就是选中操作的具体内容，这里就是加上一段对输入是否为空的判断</p>
<p><a href="http://magustest.com/blog/wp-content/uploads/2008/12/add-code-as-precondition1.png"><img class="alignnone size-full wp-image-163" title="add-code-as-precondition" src="http://magustest.com/blog/wp-content/uploads/2008/12/add-code-as-precondition1.png" alt="" width="499" height="402" /></a></p>
<p><strong><span style="color: #ff6600;">9. 点击Apply就能把这段代码段加到我们的代码中</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> Capitalize<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">//</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>value <span style="color: #008000;">==</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;value&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">//</span>
	var sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #6666cc; font-weight: bold;">bool</span> word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// are we writing a word</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var c <span style="color: #0600FF; font-weight: bold;">in</span> value<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsLetter</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>word<span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToUpper</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
			<span style="color: #0600FF; font-weight: bold;">else</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0600FF; font-weight: bold;">else</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsPunctuation</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'_'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			word <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">return</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p> OK！PEX帮助我们找到一个Bug并且修复了它。</p>
<p>本文对PEX进行一个简单的介绍，当然PEX不仅仅是那么简单，还有很多丰富的内容，例如Parameterized Unit Test，Code Contracts等等，后面打算深入研究一下PEX，挖掘一下相关的功能。预告一下，下一篇会介绍如果利用PEX写参数化单元测试(Parameterized Unit Test)</p>
<p>更多的参考：</p>
<p><a href="http://research.microsoft.com/Pex/" target="_blank">Pex官方网站</a></p>
<p><a href="http://social.msdn.microsoft.com/Forums/en-US/pex/threads/" target="_blank">Pex在MSDN上的论坛</a></p>
<p>主要参考文档：<a href="http://research.microsoft.com/pex/articles/pexcodediggertutorial.pdf" target="_blank">Code Digger Tutorial</a></p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/whiteboxtesting/introduction-to-pex-automated-white-box-testing-for-dotnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

