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.5 kB

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