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

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