Nunit单元测试工具

  • a1_601880
    了解作者
  • 3.3MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-10 03:45
    上传日期
Vs的单元测试工具 适合大多数VS版本,安装后,可以很好的使用
NUnit-2.6.4.zip
内容介绍
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8"> <meta name="generator" content="pdf2htmlEX"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <link rel="stylesheet" href="https://static.pudn.com/base/css/base.min.css"> <link rel="stylesheet" href="https://static.pudn.com/base/css/fancy.min.css"> <link rel="stylesheet" href="https://static.pudn.com/prod/directory_preview_static/62796f66517cd20ea4ced976/raw.css"> <script src="https://static.pudn.com/base/js/compatibility.min.js"></script> <script src="https://static.pudn.com/base/js/pdf2htmlEX.min.js"></script> <script> try{ pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({}); }catch(e){} </script> <title></title> </head> <body> <div id="sidebar" style="display: none"> <div id="outline"> </div> </div> <div id="pf1" class="pf w0 h0" data-page-no="1"><div class="pc pc1 w0 h0"><img class="bi x0 y0 w1 h1" alt="" src="https://static.pudn.com/prod/directory_preview_static/62796f66517cd20ea4ced976/bg1.jpg"><div class="c x0 y1 w2 h2"><div class="t m0 x1 h3 y2 ff1 fs0 fc0 sc0 ls0 ws0">Empecemos con un ejemplo simple. Supongamos que hemos escrito una aplicaci&#243;n bancaria y </div><div class="t m0 x1 h3 y3 ff1 fs0 fc0 sc0 ls0 ws0">que tenemos una clase de dominio b&#225;sica de Cuenta bancaria (Account). Account soporta </div><div class="t m0 x1 h3 y4 ff1 fs0 fc0 sc0 ls0 ws0">operaciones para dep&#243;sito (deposit), retiro (withdraw) y transferencia (transfer) de fondos<span class="_ _0"></span>. La </div><div class="t m0 x1 h3 y5 ff1 fs0 fc0 sc0 ls0 ws0">clase ser&#237;a algo similar a esto:</div><div class="t m0 x2 h4 y6 ff2 fs1 fc1 sc0 ls0 ws0">namespace bank</div><div class="t m0 x2 h4 y7 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x3 h4 y8 ff2 fs1 fc1 sc0 ls0 ws0">public class Account</div><div class="t m0 x3 h4 y9 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x4 h4 ya ff2 fs1 fc1 sc0 ls0 ws0">private float balance;</div><div class="t m0 x4 h4 yb ff2 fs1 fc1 sc0 ls0 ws0">public void Deposit(float amount)</div><div class="t m0 x4 h4 yc ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x5 h4 yd ff2 fs1 fc1 sc0 ls0 ws0">balance+=amount;</div><div class="t m0 x4 h4 ye ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x4 h4 yf ff2 fs1 fc1 sc0 ls0 ws0">public void Withdraw(float amount)</div><div class="t m0 x4 h4 y10 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x5 h4 y11 ff2 fs1 fc1 sc0 ls0 ws0">balance-=amount;</div><div class="t m0 x4 h4 y12 ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x4 h4 y13 ff2 fs1 fc1 sc0 ls0 ws0">public void TransferFunds(Account destination, float amount)</div><div class="t m0 x4 h4 y14 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x4 h4 y15 ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x4 h4 y16 ff2 fs1 fc1 sc0 ls0 ws0">public float Balance</div><div class="t m0 x4 h4 y17 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x5 h4 y18 ff2 fs1 fc1 sc0 ls0 ws0">get{ return balance;}</div><div class="t m0 x4 h4 y19 ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x3 h4 y1a ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x2 h4 y1b ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x1 h3 y1c ff1 fs0 fc0 sc0 ls0 ws0">Ahora vamos a escribir una prueba para esta clase - AccountTest. El primer m&#233;todo que </div><div class="t m0 x1 h3 y1d ff1 fs0 fc0 sc0 ls0 ws0">probaremos ser&#225; transferencia de fondos (TransferFunds).</div><div class="t m0 x2 h4 y1e ff2 fs1 fc1 sc0 ls0 ws0">namespace bank</div><div class="t m0 x2 h4 y1f ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x3 h4 y20 ff2 fs1 fc1 sc0 ls0 ws0">using NUnit.Framework;</div><div class="t m0 x3 h4 y21 ff2 fs1 fc1 sc0 ls0 ws0">[TestFixture]</div><div class="t m0 x3 h4 y22 ff2 fs1 fc1 sc0 ls0 ws0">public class AccountTest</div><div class="t m0 x3 h4 y23 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x4 h4 y24 ff2 fs1 fc1 sc0 ls0 ws0">[Test]</div><div class="t m0 x4 h4 y25 ff2 fs1 fc1 sc0 ls0 ws0">public void TransferFunds()</div><div class="t m0 x4 h4 y26 ff2 fs1 fc1 sc0 ls0 ws0">{</div><div class="t m0 x5 h4 y27 ff2 fs1 fc1 sc0 ls0 ws0">Account source = new Account();</div><div class="t m0 x5 h4 y28 ff2 fs1 fc1 sc0 ls0 ws0">source.Deposit(200.00F);</div><div class="t m0 x5 h4 y29 ff2 fs1 fc1 sc0 ls0 ws0">Account destination = new Account();</div><div class="t m0 x5 h4 y2a ff2 fs1 fc1 sc0 ls0 ws0">destination.Deposit(150.00F);</div><div class="t m0 x5 h4 y2b ff2 fs1 fc1 sc0 ls0 ws0">source.TransferFunds(destination, 100.00F);</div><div class="t m0 x5 h4 y2c ff2 fs1 fc1 sc0 ls0 ws0">Assertion.AssertEquals(250.00F, destination.Balance);</div><div class="t m0 x5 h4 y2d ff2 fs1 fc1 sc0 ls0 ws0">Assertion.AssertEquals(100.00F, source.Balance);</div><div class="t m0 x4 h4 y2e ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x3 h4 y2f ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x2 h4 y30 ff2 fs1 fc1 sc0 ls0 ws0">}</div><div class="t m0 x1 h3 y31 ff1 fs0 fc0 sc0 ls0 ws0">Lo primero que se nota en esta clase es que posee un atributo asociado a ella llamado </div><div class="t m0 x1 h3 y32 ff1 fs0 fc0 sc0 ls0 ws0">[TestFixture] &#8211; este es la forma de indicar que la clase contiene c&#243;digo de prueba (este atributo </div><div class="t m0 x1 h3 y33 ff1 fs0 fc0 sc0 ls0 ws0">puede ser heredado). Esta clase debe ser p&#250;blica (public) y no existen restricciones en su </div><div class="t m0 x1 h3 y34 ff1 fs0 fc0 sc0 ls0 ws0">superclase. La clase adem&#225;s debe tener un constructor.</div></div></div><div class="pi" data-data='{"ctm":[1.568627,0.000000,0.000000,1.568627,0.000000,0.000000]}'></div></div> </body> </html>
评论
    相关推荐