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

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