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 9.8 kB

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