You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.xml 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?xml version="1.0"?>
  2. <!--
  3. Copyright 2006 The Apache Software Foundation
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. -->
  14. <document>
  15. <properties>
  16. <index value="1"/>
  17. <title>AntUnit</title>
  18. </properties>
  19. <body>
  20. <section name="Idea">
  21. <p>Initially all tests for Ant tasks were written as individual
  22. <a href="http://www.junit.org/">JUnit</a> test cases. Pretty
  23. soon it was clear that most tests needed to perform common tasks
  24. like reading a build file, initializing a project instance with
  25. it and executing a target. At this point <a
  26. href="http://svn.apache.org/viewcvs.cgi/ant/core/trunk/src/testcases/org/apache/tools/ant/BuildFileTest.java">BuildFileTest</a>
  27. was invented, a base class for almost all task test cases.</p>
  28. <p>BuildFileTest works fine and in fact has been picked up by <a
  29. href="http://ant-contrib.sf.net/">the Ant-Contrib Project</a>
  30. and others as well.</p>
  31. <p>Over time a new pattern evolved, more and more tests only
  32. executed a target and didn't check any effects. Instead that
  33. target contained the assertions as a <code>&lt;fail&gt;</code>
  34. task. This is an example taken from the build file for the
  35. ANTLR task (using Ant 1.7 features):</p>
  36. <source><![CDATA[
  37. <target name="test3" depends="setup">
  38. <antlr target="antlr.g" outputdirectory="${tmp.dir}"/>
  39. <fail>
  40. <condition>
  41. <!-- to prove each of these files exists;
  42. ANTLR >= 2.7.6 leaves behind new (.smap) files as well. -->
  43. <resourcecount when="ne" count="5">
  44. <fileset dir="${tmp.dir}">
  45. <include name="CalcParserTokenTypes.txt" />
  46. <include name="CalcParserTokenTypes.java" />
  47. <include name="CalcLexer.java" />
  48. <include name="CalcParser.java" />
  49. <include name="CalcTreeWalker.java" />
  50. </fileset>
  51. </resourcecount>
  52. </condition>
  53. </fail>
  54. </target>
  55. ]]></source>
  56. <p>where the corresponding JUnit testcase has been reduced
  57. to</p>
  58. <source><![CDATA[
  59. ...
  60. public class ANTLRTest extends BuildFileTest {
  61. private final static String TASKDEFS_DIR = "src/etc/testcases/taskdefs/optional/antlr/";
  62. public ANTLRTest(String name) {
  63. super(name);
  64. }
  65. public void setUp() {
  66. configureProject(TASKDEFS_DIR + "antlr.xml");
  67. }
  68. public void tearDown() {
  69. executeTarget("cleanup");
  70. }
  71. public void test3() {
  72. executeTarget("test3");
  73. }
  74. ...
  75. }
  76. ]]></source>
  77. <p>This approach has a couple of advantages, one of them is that
  78. it is very easy to translate an example build file from a bug
  79. report into a test case. If you ask a user for a testcase for a
  80. given bug in Ant, he now doesn't need to understand JUnit or how
  81. to fit a test into Ant's existing tests any more.</p>
  82. <p>AntUnit takes this approach to testing even further, it
  83. removes JUnit completely and it comes with a set of predefined
  84. <code>&lt;assert&gt;</code> tasks in order to reuse common kind
  85. of checks.</p>
  86. <p>It turns out that AntUnit lends itself as a solution to other
  87. problems as well. The assertions are an easy way to validate a
  88. setup before even starting the build process, for example.
  89. AntUnit could also be used for functional and integration tests
  90. outside of the scope of Ant tasks (assert contents of databases
  91. after running an application, assert contents of HTTP responses
  92. ...). This is an area that will need more research.</p>
  93. </section>
  94. <section name="Concepts">
  95. <subsection name="antunit Task">
  96. <p>The &lt;antunit&gt; task drives the tests much like
  97. &lt;junit&gt; does for JUnit tests.</p>
  98. <p>When called on a build file, the task will start a new Ant
  99. project for that build file and scan for targets with names
  100. that start with "test". For each such target it then will</p>
  101. <ol>
  102. <li>Execute the target named setUp, if there is one.</li>
  103. <li>Execute the target itself - if this target depends on
  104. other targets the normal Ant rules apply and the dependent
  105. targets are executed first.</li>
  106. <li>Execute the target names tearDown, if there is one.</li>
  107. </ol>
  108. </subsection>
  109. <subsection name="Assertions">
  110. <p>The base task is <code>&lt;assertTrue&gt;</code>. It
  111. accepts a single nested condition and throws a subclass of
  112. BuildException named AssertionFailedException if that
  113. condition evaluates to false.</p>
  114. <p>This task could have been implemented using
  115. <code>&lt;macrodef&gt;</code> and <code>&lt;fail&gt;</code>,
  116. but in fact it is a "real" task so that it is possible to
  117. throw a subclass of BuildException. The
  118. <code>&lt;antunit&gt;</code> task catches this exception and
  119. marks the target as failed, any other type of Exception
  120. (including other BuildException) are test errors.</p>
  121. <p>Together with <code>&lt;assertTrue&gt;</code> there are
  122. many predefined assertions for common conditions, most of
  123. these are only macros.</p>
  124. </subsection>
  125. <subsection name="Other Tasks">
  126. <p>The <code>&lt;logcapturer&gt;</code> captures all messages
  127. that pass Ant's logging system and provides them via a
  128. reference inside of the project. If you want to assert
  129. certain log messages, you need to start this task (prior to
  130. your target under test) and use the
  131. <code>&lt;assertLogContains&gt;</code> assertion.</p>
  132. <p><code>&lt;expectFailure&gt;</code> is a task container that
  133. catches any BuildException thrown by tasks nested into it. If
  134. no exception has been thrown it will cause a test failure (by
  135. throwing an AssertionFailedException).</p>
  136. </subsection>
  137. <subsection name="AntUnitListener">
  138. <p>Part of the library is the <code>AntUnitListener</code>
  139. interface that can be used to record test results. The
  140. &lt;antunit&gt; task accepts arbitrary many listeners and
  141. relays test results to them.</p>
  142. <p>Currently only a single implementation
  143. <code>&lt;plainlistener&gt;</code> modelled after the "plain"
  144. JUnit listener is bundled with the library.</p>
  145. </subsection>
  146. </section>
  147. <section name="Examples">
  148. <p>This is a way to test that <code>&lt;touch&gt;</code>
  149. actually creates a file if it doesn't exist:</p>
  150. <source><![CDATA[
  151. <project xmlns:au="antlib:org.apache.ant.antunit">
  152. <!-- is called prior to the test -->
  153. <target name="setUp">
  154. <property name="foo" value="foo"/>
  155. </target>
  156. <!-- is called after the test, if if that causes an error -->
  157. <target name="tearDown">
  158. <delete file="${foo}" quiet="true"/>
  159. </target>
  160. <!-- the actual test case -->
  161. <target name="testTouchCreatesFile">
  162. <au:assertFileDoesntExist file="${foo}"/>
  163. <touch file="${foo}"/>
  164. <au:assertFileExists file="${foo}"/>
  165. </target>
  166. </project>
  167. ]]></source>
  168. <p>When running a task like</p>
  169. <source><![CDATA[
  170. <au:antunit>
  171. <fileset dir="." includes="touch.xml"/>
  172. <au:plainlistener/>
  173. </au:antunit>
  174. ]]></source>
  175. <p>from a buildfile of its own you'll get a result that looks like</p>
  176. <source><![CDATA[
  177. [au:antunit] Build File: /tmp/touch.xml
  178. [au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.249 sec
  179. [au:antunit] Target: testTouchCreatesFile took 0.183 sec
  180. BUILD SUCCESSFUL
  181. Total time: 1 second
  182. ]]></source>
  183. </section>
  184. </body>
  185. </document>