<?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>Marcio Althmann &#187; Testes Unitários</title>
	<atom:link href="http://www.marcioalthmann.net/tag/testes-unitarios/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcioalthmann.net</link>
	<description></description>
	<lastBuildDate>Tue, 24 Jan 2012 15:12:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Automatizando testes unitários com Pex</title>
		<link>http://www.marcioalthmann.net/2009/11/automatizando-testes-unitarios-com-pex/</link>
		<comments>http://www.marcioalthmann.net/2009/11/automatizando-testes-unitarios-com-pex/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 10:43:00 +0000</pubDate>
		<dc:creator>Márcio Fábio Althmann</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Dicas]]></category>
		<category><![CDATA[Testes Unitários]]></category>
		<category><![CDATA[Visual Studio .NET]]></category>

		<guid isPermaLink="false">/post/2009/11/30/Automatizando-testes-unitarios-com-Pex.aspx</guid>
		<description><![CDATA[Criar testes unit&#225;rios &#233; uma &#243;tima pr&#225;tica, e cada vez ganha mais espa&#231;o, existem v&#225;rios frameworks para cria&#231;&#227;o de testes unit&#225;rios, e com o Visual Studio n&#227;o &#233; diferente, agora dependendo da complexidade do m&#233;todo que vamos testar, a cria&#231;&#227;o dos testes unit&#225;rios para cobrir 100% desse m&#233;todo pode gerar um custo alto para o...<p><a href="http://www.marcioalthmann.net/2009/11/automatizando-testes-unitarios-com-pex/"> Leia mais →</a>]]></description>
			<content:encoded><![CDATA[<p>Criar testes unit&aacute;rios &eacute; uma &oacute;tima pr&aacute;tica, e cada vez ganha mais espa&ccedil;o, existem v&aacute;rios frameworks para cria&ccedil;&atilde;o de testes unit&aacute;rios, e com o Visual Studio n&atilde;o &eacute; diferente, agora dependendo da complexidade do m&eacute;todo que vamos testar, a cria&ccedil;&atilde;o dos testes unit&aacute;rios para cobrir 100% desse m&eacute;todo pode gerar um custo alto para o projeto, e &eacute; nesse ponto que o Pex ajuda, ele analisa o c&oacute;digo fonte, e gera os teste unit&aacute;rios informando valores de entrada, analisando os de sa&iacute;da, cobrindo todas as poss&iacute;veis varia&ccedil;&otilde;es no c&oacute;digo e procurando falhas.</p>
<p>Esse ser&aacute; s&oacute; o primeiro artigo sobre o Pex, mostrando a forma mais simples de utiliz&aacute;-lo, em breve mostrarei como criar testes parametrizados com o mesmo.</p>
<p>O Pex &eacute; uma ferramenta criada pelo <a href="http://research.microsoft.com" target="_blank">Microsoft Research</a> e o link direto para o projeto &eacute; <a href="http://research.microsoft.com/en-us/projects/pex" target="_blank">http://research.microsoft.com/en-us/projects/pex</a> , quando o Pex &eacute; instalado ele &eacute; integrado ao Visual Studio, testei aqui com o Visual Studio 2008 e com o 2010, no Visual Studio 2010 n&atilde;o consegui testar, deve ser por causa da vers&atilde;o Beta do 2010, assim que tiver novidades do funcionamento no Visual Studio 2010 posto aqui.</p>
<p>Agora vamos imaginar que temos no nosso projeto um m&eacute;todo que faz um c&aacute;lculo de sal&aacute;rio, o c&oacute;digo abaixo n&atilde;o &eacute; muito complexo mas tem v&aacute;rias op&ccedil;&otilde;es que ter&iacute;amos que levar em considera&ccedil;&atilde;o na cria&ccedil;&atilde;o dos testes unit&aacute;rios.</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum1" style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> Salario(<span style="color: #0000ff">int</span> quantidadeHoras, <span style="color: #0000ff">int</span> valorHora)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum2" style="color: #606060">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum3" style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> (quantidadeHoras &lt;= 0 || valorHora &lt;= 0)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum4" style="color: #606060">   4:</span>         <span style="color: #0000ff">return</span> 0;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum5" style="color: #606060">   5:</span>&nbsp; </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum6" style="color: #606060">   6:</span>     <span style="color: #0000ff">if</span>(quantidadeHoras &gt; 160)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum7" style="color: #606060">   7:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum8" style="color: #606060">   8:</span>         <span style="color: #0000ff">if</span>(valorHora &gt; 40)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum9" style="color: #606060">   9:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum10" style="color: #606060">  10:</span>             valorHora -= 3;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum11" style="color: #606060">  11:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum12" style="color: #606060">  12:</span>         <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum13" style="color: #606060">  13:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum14" style="color: #606060">  14:</span>             valorHora += 5;    </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum15" style="color: #606060">  15:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum16" style="color: #606060">  16:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum17" style="color: #606060">  17:</span>&nbsp; </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum18" style="color: #606060">  18:</span>     <span style="color: #0000ff">if</span>(valorHora &gt; 40)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum19" style="color: #606060">  19:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum20" style="color: #606060">  20:</span>         valorHora -= 4;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum21" style="color: #606060">  21:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum22" style="color: #606060">  22:</span>&nbsp; </pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum23" style="color: #606060">  23:</span>     <span style="color: #0000ff">return</span> valorHora*quantidadeHoras;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span id="lnum24" style="color: #606060">  24:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>&nbsp;</p>
<p>Com o Pex instalado ele faz a an&aacute;lise do c&oacute;digo do m&eacute;todo, e vai criar testes passando par&acirc;metros que validem toda a regra do meu m&eacute;todo, para isso &eacute; s&oacute; clicar com o bot&atilde;o direito no m&eacute;todo e escolher a op&ccedil;&atilde;o <em>Run Pex</em>.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/RunPex.jpg" rel="lightbox[40]" title="RunPex"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="RunPex" src="http://www.marcioalthmann.net/wp-content/uploads/RunPex.jpg" border="0" alt="RunPex" width="445" height="179" /></a></p>
<p>Nesse momento &eacute; que o <em>Pex</em> come&ccedil;a a analisar o c&oacute;digo, e ele mostra uma janela no Visual Studio com os resultados dos testes feitos.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/PexExplorationResults.jpg" rel="lightbox[40]" title="PexExplorationResults"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="PexExplorationResults" src="http://www.marcioalthmann.net/wp-content/uploads/PexExplorationResults.jpg" border="0" alt="PexExplorationResults" width="667" height="197" /></a></p>
<p>Podemos ver acima que ele gerou 6 testes unit&aacute;rios para cobrir todo o m&eacute;todo, nesse ponto ele ainda n&atilde;o gerou os testes no nosso projeto, e clicando em cada um desses testes podemos ver o c&oacute;digo utilizado em cada teste.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/PexExplorationResultsCode.jpg" rel="lightbox[40]" title="PexExplorationResultsCode"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="PexExplorationResultsCode" src="http://www.marcioalthmann.net/wp-content/uploads/PexExplorationResultsCode.jpg" border="0" alt="PexExplorationResultsCode" width="740" height="229" /></a></p>
<p>Para criar os testes unit&aacute;rios no projeto selecione o teste desejado e clique no bot&atilde;o <em>Save</em> na imagem abaixo todos os testes est&atilde;o selecionados, clicando no bot&atilde;o <em>Save</em> uma janela ser&aacute; aberta mostrando o nome do projeto de testes que ser&aacute; criado e tamb&eacute;m o diret&oacute;rio onde ser&aacute; criado., podemos modificar o nome e diret&oacute;rio sem problemas.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/PexSave.jpg" rel="lightbox[40]" title="PexSave"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="PexSave" src="http://www.marcioalthmann.net/wp-content/uploads/PexSave.jpg" border="0" alt="PexSave" width="773" height="436" /></a></p>
<p>Agora podemos ver no <em>Solution Explorer</em> o projeto de testes criado.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/SolutionExplorer.jpg" rel="lightbox[40]" title="SolutionExplorer"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SolutionExplorer" src="http://www.marcioalthmann.net/wp-content/uploads/SolutionExplorer.jpg" border="0" alt="SolutionExplorer" width="276" height="250" /></a></p>
<p>Com os testes criado podemos utilizar todos os recursos de testes unit&aacute;rios que temos no <em>Visual Studio</em> como por exemplo ver a cobertura de c&oacute;digo.</p>
<p><a href="http://www.marcioalthmann.net/wp-content/uploads/CoberturaCodigo.jpg" rel="lightbox[40]" title="CoberturaCodigo"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CoberturaCodigo" src="http://www.marcioalthmann.net/wp-content/uploads/CoberturaCodigo.jpg" border="0" alt="CoberturaCodigo" width="660" height="385" /></a></p>
<p>O Pex com certeza ajuda muito na cria&ccedil;&atilde;o dos nossos testes unit&aacute;rios, esse foi um exemplo simples de como utiliz&aacute;-lo, em breve exemplos mais complexos com testes parametrizados.</p>
<p>Abra&ccedil;os.</p>
<div id="fb-like" style=""><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.marcioalthmann.net/2009/11/automatizando-testes-unitarios-com-pex/&amp;layout=button_count&amp;show_faces=false&amp;width=300&amp;action=like&amp;font=&amp;colorscheme=light&amp;locale=en_US" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:300px; height:20px"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.marcioalthmann.net/2009/11/automatizando-testes-unitarios-com-pex/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
