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

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