⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib0121.html

📁 j2ee架构师手册
💻 HTML
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Testing Best Practices</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0120.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0122.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch18"></a>
<div class="section">
<h2 class="first-section-title"><a name="594"></a><a name="ch18lev1sec4"></a>Testing Best Practices</h2><p class="first-para">
<b class="bold">Keep test cases and supporting classes in a separate package structure. </b>Although test cases are developed in conjunction with the application, they are not truly a part of it. Typically, you have no need to deploy them to anything but your testing environments.</p>
<p class="para">I usually organize the package structure of my test cases and supporting classes after the application package structure. For instance, for CementJ, the test classes for everything in package <span class="fixed">org.cementj.base</span> are in <span class="fixed">test.cementj.base</span>. Supporting classes for those test cases are in package <span class="fixed">test.cementj.base.support</span>. Keeping a consistent package structure will save developers time.</p>
<p class="para">
<b class="bold">Adopt a naming convention for test classes that makes them easy to find. </b>This is another suggestion that can save developers time. For instance, I name all of my test classes <span class="fixed">TestXxx</span>, where <span class="fixed">Xxx</span> is the name of the class being tested. For example, class <span class="fixed">TestValueObject</span> is the test class for <span class="fixed">ValueObject</span>. I prefer to combine all unit tests for the same class into one test class, but this is not a technical requirement or suggestion.</p>
<a name="595"></a><a name="IDX-250"></a>
<p class="para">
<b class="bold">Put a descriptive label on all test assertions.</b> When you code <span class="fixed">assert()</span> or <span class="fixed">assertTrue()</span> method calls in your test cases, you can optionally provide a label that will be displayed on failure; for example:</p>
<div class="informalexample">
<pre class="literallayout">
TestCase.assertTrue("Test 2: Select test", taskVO != null);
</pre>
</div>
<p class="para">This label becomes important when you're running the test case as part of a regression test suite. If the test fails, you don't want developers wasting time trying to figure out which test failed. Note that the error description doesn't need to identify the test class that failed; JUnit will do that for you.</p>
<p class="para">I've adopted the practice of prefixing all assertion descriptions with <span class="fixed">Testx:</span>, where <span class="fixed">x</span> is the test number within the test case. I make <span class="fixed">x</span> unique within the test class. This is boring and uncreative, but functional.</p>
<p class="last-para">
<b class="bold">Make each </b><b class="bold"><span class="fixed">TestCase</span></b> <b class="bold">class self-sufficient and independent.</b> Test cases should not rely on other test cases having to execute before it. If you write the test cases so that test case 1 has to be run before test case 2 will work, it's not obvious to other developers what the prerequisites are should they want to use a test case for unit testing and debugging. There may be some isolated cases where implementing this suggestion isn't practical.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle"  alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0120.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0122.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -