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.

junitlauncher.html 17 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  18. <title>JUnitLauncher Task</title>
  19. </head>
  20. <body>
  21. <h2 id="junitlauncher">JUnitLauncher</h2>
  22. <h3>Description</h3>
  23. <p>
  24. This task allows tests to be launched and run using the JUnit 5 framework.
  25. </p>
  26. <p>
  27. JUnit 5 introduced a newer set of APIs to write and launch tests. It also introduced the concept
  28. of test engines. Test engines decide which classes are considered as testcases and how they are
  29. executed. JUnit 5 supports running tests that have been written using JUnit 4 constructs as well
  30. as tests that have been written using JUnit 5 constructs. For more details about JUnit 5
  31. itself, please refer to the JUnit 5 project's documentation
  32. at <a href="https://junit.org/junit5/">https://junit.org/junit5/</a>.
  33. </p>
  34. <p>
  35. The goal of this <code>junitlauncher</code> task is to allow launching the JUnit 5 test launcher
  36. and building the test requests so that the selected tests can then be parsed and executed by the
  37. test engine(s) supported by JUnit 5. This task in itself does <i>not</i> understand what a test
  38. case is nor does it execute the tests itself.
  39. </p>
  40. <p>
  41. <strong>Note</strong>: This task depends on external libraries not included in the Apache Ant
  42. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for
  43. more information.
  44. </p>
  45. <p>
  46. <strong>Note</strong>: You must have the necessary JUnit 5 libraries in the classpath of the
  47. tests. At the time of writing this documentation, the list of JUnit 5 platform libraries that
  48. are necessary to run the tests are:
  49. </p>
  50. <ul>
  51. <li>
  52. <samp>junit-platform-commons.jar</samp>
  53. </li>
  54. <li>
  55. <samp>junit-platform-engine.jar</samp>
  56. </li>
  57. <li>
  58. <samp>junit-platform-launcher.jar</samp>
  59. </li>
  60. </ul>
  61. <p>
  62. Depending on the test engine(s) that you want to use in your tests, you will further need the
  63. following libraries in the classpath
  64. </p>
  65. <p>
  66. For <q>junit-vintage</q> engine:
  67. </p>
  68. <ul>
  69. <li>
  70. <samp>junit-vintage-engine.jar</samp>
  71. </li>
  72. <li>
  73. <samp>junit.jar</samp> (JUnit 4.x version)
  74. </li>
  75. </ul>
  76. <p>
  77. For <q>junit-jupiter</q> engine:
  78. </p>
  79. <ul>
  80. <li>
  81. <samp>junit-jupiter-api.jar</samp>
  82. </li>
  83. <li>
  84. <samp>junit-jupiter-engine.jar</samp>
  85. </li>
  86. <li>
  87. <samp>opentest4j.jar</samp>
  88. </li>
  89. </ul>
  90. <p>
  91. To have these in the test classpath, you can follow <em>either</em> of the following approaches:
  92. </p>
  93. <ul>
  94. <li>Put all these relevant jars along with the <samp>ant-junitlauncher.jar</samp>
  95. in <samp>ANT_HOME/lib</samp> directory</li>
  96. <li>OR Leave <samp>ant-junitlauncher.jar</samp> in the <samp>ANT_HOME/lib</samp> directory and
  97. include all other relevant jars in the classpath by passing them as a <kbd>-lib</kbd>
  98. option, while invoking Ant</li>
  99. </ul>
  100. <p>
  101. Tests are defined by nested elements like <code>test</code>, <code>testclasses</code> tags
  102. (see <a href="#nested">nested elements</a>).
  103. </p>
  104. <h3>Parameters</h3>
  105. <table class="attr">
  106. <tr>
  107. <th>Attribute</th>
  108. <th>Description</th>
  109. <th>Required</th>
  110. </tr>
  111. <tr>
  112. <td>haltOnFailure</td>
  113. <td>A value of <q>true</q> implies that build has to stop if any failure occurs in any of
  114. the tests. JUnit 4+ classifies failures as both assertion failures as well as exceptions
  115. that get thrown during test execution. As such, this task too considers both these cases
  116. as failures and doesn't distinguish one from another.
  117. </td>
  118. <td>No; default is <q>false</q></td>
  119. </tr>
  120. <tr>
  121. <td>failureProperty</td>
  122. <td>The name of a property to set in the event of a failure
  123. (exceptions in tests are considered failures as well).
  124. </td>
  125. <td>No</td>
  126. </tr>
  127. </table>
  128. <h3 id="nested">Nested Elements</h3>
  129. <h4>classpath</h4>
  130. <p>
  131. The nested <code>&lt;classpath&gt;</code> element that represents
  132. a <a href="../using.html#path">PATH like structure</a> can be used to configure the task to use
  133. this classpath for finding and running the tests. This classpath will be used for:
  134. </p>
  135. <ul>
  136. <li>Finding the test classes to execute</li>
  137. </ul>
  138. <p>
  139. If the <code>classpath</code> element isn't configured for the task, then the classpath of Ant
  140. itself will be used for finding the test classes.
  141. </p>
  142. <h4>listener</h4>
  143. <p>
  144. The <code>junitlauncher</code> task can be configured with <code>listener</code>(s) to listen to
  145. test execution events (such as a test execution starting, completing etc...). The listener is
  146. expected to be a class which implements
  147. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>.
  148. This <code class="code">TestExecutionListener</code> interface is an API exposed by the JUnit 5
  149. platform APIs and isn't specific to Ant. As such, you can use any existing implementation
  150. of <code class="code">TestExecutionListener</code> in this task.
  151. </p>
  152. <h5>Test result formatter</h5>
  153. <p>
  154. <code>junitlauncher</code> provides a way where the test execution results can be formatted and
  155. presented in a way that's customizable. The task allows for configuring test result formatters,
  156. through the use of <code>listener</code> element. As noted previously, the <code>listener</code>
  157. element expects the listener to implement
  158. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>
  159. interface. Typically, result formatters need a bit more configuration details to be fed to them,
  160. during the test execution&mdash;details like where to write out the formatted result. Any such
  161. listener can optionally implement
  162. the <code class="code">org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  163. interface. This interface is specific to Ant <code>junitlauncher</code> task and it extends
  164. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code> interface
  165. </p>
  166. <p>
  167. The <code>junitlauncher</code> task comes with the following pre-defined test result formatter
  168. types:
  169. </p>
  170. <ul>
  171. <li>
  172. <q>legacy-plain</q> : This formatter prints a short statistics line for all test
  173. cases.
  174. </li>
  175. <li>
  176. <q>legacy-brief</q> : This formatter prints information for tests that failed or were
  177. skipped.
  178. </li>
  179. <li>
  180. <q>legacy-xml</q> : This formatter prints statistics for the tests in XML format.
  181. </li>
  182. </ul>
  183. <p>
  184. <strong>Note</strong>: Each of these formatters named <q>legacy</q> try to format the results
  185. similar to what the <code>junit</code> task's formatters used to do. Furthermore,
  186. the <q>legacy-xml</q> formatter generates the XML to comply with the same schema that
  187. the <code>junit</code> task's XML formatter used to follow. As a result, the XML generated by
  188. this formatter, can be used as-is by the <code>junitreport</code> task.
  189. </p>
  190. <p>
  191. The <code>listener</code> element supports the following attributes:
  192. </p>
  193. <table class="attr">
  194. <tr>
  195. <th>Attribute</th>
  196. <th>Description</th>
  197. <th>Required</th>
  198. </tr>
  199. <tr>
  200. <td>type</td>
  201. <td>Use a predefined formatter (either <q>legacy-xml</q>, <q>legacy-plain</q>
  202. or <q>legacy-brief</q>).</td>
  203. <td rowspan="2">Exactly one of these</td>
  204. </tr>
  205. <tr>
  206. <td>classname</td>
  207. <td class="left">Name of a listener class which
  208. implements <code>org.junit.platform.launcher.TestExecutionListener</code> or
  209. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  210. interface
  211. </td>
  212. </tr>
  213. <tr>
  214. <td>resultFile</td>
  215. <td>
  216. The file name to which the formatted result needs to be written to. This attribute is
  217. only relevant when the listener class implements
  218. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  219. interface.
  220. <p>
  221. If no value is specified for this attribute and the listener implements
  222. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  223. then the file name will be defaulted to and will be of the
  224. form <code>TEST-<i>testname</i>.<i>formatter-specific-extension</i></code>
  225. (ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type
  226. formatter)
  227. </p>
  228. </td>
  229. <td>No</td>
  230. </tr>
  231. <tr>
  232. <td>sendSysOut</td>
  233. <td>If set to <q>true</q> then the listener will be passed the <code>stdout</code> content
  234. generated by the test(s). This attribute is relevant only if the listener class
  235. implements
  236. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  237. interface.</td>
  238. <td>No; defaults to <q>false</q></td>
  239. </tr>
  240. <tr>
  241. <td>sendSysErr</td>
  242. <td>If set to <q>true</q> then the listener will be passed the <code>stderr</code> content
  243. generated by the test(s). This attribute is relevant only if the listener class
  244. implements
  245. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  246. interface.</td>
  247. <td>No; defaults to <q>false</q></td>
  248. </tr>
  249. <tr>
  250. <td>if</td>
  251. <td>Only use this listener <a href="../properties.html#if+unless">if the named property is
  252. set</a>.</td>
  253. <td>No</td>
  254. </tr>
  255. <tr>
  256. <td>unless</td>
  257. <td>Only use this listener <a href="../properties.html#if+unless">if the named property
  258. is <strong>not</strong> set</a>.</td>
  259. <td>No</td>
  260. </tr>
  261. </table>
  262. <h4>test</h4>
  263. <p>Defines a single test class.</p>
  264. <table class="attr">
  265. <tr>
  266. <th>Attribute</th>
  267. <th>Description</th>
  268. <th>Required</th>
  269. </tr>
  270. <tr>
  271. <td>name</td>
  272. <td>Fully qualified name of the test class.</td>
  273. <td>Yes</td>
  274. </tr>
  275. <tr>
  276. <td>methods</td>
  277. <td>Comma-separated list of names of test case methods to execute. If this is specified,
  278. then only these test methods from the test class will be executed.</td>
  279. <td>No</td>
  280. </tr>
  281. <tr>
  282. <td>haltOnFailure</td>
  283. <td>Stop the build process if a failure occurs during the test run (exceptions are
  284. considered as failures too). Overrides value set on <code>junitlauncher</code>
  285. element.</td>
  286. <td>No</td>
  287. </tr>
  288. <tr>
  289. <td>failureProperty</td>
  290. <td>The name of a property to set in the event of a failure (exceptions are considered
  291. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  292. <td>No</td>
  293. </tr>
  294. <tr>
  295. <td>outputDir</td>
  296. <td>Directory to write the reports to.</td>
  297. <td>No; default is the base directory of the project.</td>
  298. </tr>
  299. <tr>
  300. <td>if</td>
  301. <td>Only run this test <a href="../properties.html#if+unless">if the named property is
  302. set</a>.</td>
  303. <td>No</td>
  304. </tr>
  305. <tr>
  306. <td>unless</td>
  307. <td>Only run this test <a href="../properties.html#if+unless">if the named property
  308. is <strong>not</strong> set</a>.</td>
  309. <td>No</td>
  310. </tr>
  311. </table>
  312. <p>
  313. Tests can define their own listeners via nested <code>listener</code> elements.
  314. </p>
  315. <h4>testclasses</h4>
  316. <p>Define a number of tests based on pattern matching.</p>
  317. <p>
  318. <code>testclasses</code> collects the included <a href="../Types/resources.html">resources</a>
  319. from any number of nested <a href="../Types/resources.html#collection">Resource
  320. Collection</a>s. It then selects each resource whose name ends in <code>.class</code>. These
  321. classes are then passed on to the JUnit 5 platform for it to decide and run them as tests.
  322. </p>
  323. <table class="attr">
  324. <tr>
  325. <th>Attribute</th>
  326. <th>Description</th>
  327. <th>Required</th>
  328. </tr>
  329. <tr>
  330. <td>haltOnFailure</td>
  331. <td>Stop the build process if a failure occurs during the test run (exceptions are
  332. considered as failures too). Overrides value set on <code>junitlauncher</code>
  333. element.</td>
  334. <td>No</td>
  335. </tr>
  336. <tr>
  337. <td>failureProperty</td>
  338. <td>The name of a property to set in the event of a failure (exceptions are considered
  339. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  340. <td>No</td>
  341. </tr>
  342. <tr>
  343. <td>outputDir</td>
  344. <td>Directory to write the reports to.</td>
  345. <td>No; default is the base directory of the project.</td>
  346. </tr>
  347. <tr>
  348. <td>if</td>
  349. <td>Only run the tests <a href="../properties.html#if+unless">if the named property is
  350. set</a>.</td>
  351. <td>No</td>
  352. </tr>
  353. <tr>
  354. <td>unless</td>
  355. <td>Only run the tests <a href="../properties.html#if+unless">if the named property
  356. is <strong>not</strong> set</a>.</td>
  357. <td>No</td>
  358. </tr>
  359. </table>
  360. <p>
  361. <code>testclasses</code> can define their own listeners via nested <code>listener</code>
  362. elements.
  363. </p>
  364. <h3>Examples</h3>
  365. <pre>
  366. &lt;path id="test.classpath"&gt;
  367. ...
  368. &lt;/path&gt;
  369. &lt;junitlauncher&gt;
  370. &lt;classpath refid="test.classpath"/&gt;
  371. &lt;test name="org.myapp.SimpleTest"/&gt;
  372. &lt;/junitlauncher&gt;</pre>
  373. <p>
  374. Launches the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> test
  375. </p>
  376. <pre>
  377. &lt;junitlauncher&gt;
  378. &lt;classpath refid="test.classpath"/&gt;
  379. &lt;test name="org.myapp.SimpleTest" haltOnFailure="true"/&gt;
  380. &lt;test name="org.myapp.AnotherTest"/&gt;
  381. &lt;/junitlauncher&gt;
  382. </pre>
  383. <p>
  384. Launches the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> and
  385. the <samp>org.myapp.AnotherTest</samp> tests. The build process will be stopped if any test, in
  386. the <samp>org.myapp.SimpleTest</samp>, fails.
  387. </p>
  388. <pre>
  389. &lt;junitlauncher&gt;
  390. &lt;classpath refid="test.classpath"/&gt;
  391. &lt;test name="org.myapp.SimpleTest" methods="testFoo, testBar"/&gt;
  392. &lt;/junitlauncher&gt;</pre>
  393. <p>
  394. Launches the JUnit 5 platform to run only the <samp>testFoo</samp> and <samp>testBar</samp>
  395. methods of the <samp>org.myapp.SimpleTest</samp> test class.
  396. </p>
  397. <pre>
  398. &lt;junitlauncher&gt;
  399. &lt;classpath refid="test.classpath"/&gt;
  400. &lt;testclasses outputdir="${output.dir}"&gt;
  401. &lt;fileset dir="${build.classes.dir}"&gt;
  402. &lt;include name="org/example/**/tests/**/"/&gt;
  403. &lt;/fileset&gt;
  404. &lt;/testclasses&gt;
  405. &lt;/junitlauncher&gt;</pre>
  406. <p>
  407. Selects any <samp>.class</samp> files that match
  408. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  409. the <samp>${build.classes.dir}</samp> and passes those classes to the JUnit 5 platform for
  410. execution as tests.
  411. </p>
  412. <pre>
  413. &lt;junitlauncher&gt;
  414. &lt;classpath refid="test.classpath"/&gt;
  415. &lt;testclasses outputdir="${output.dir}"&gt;
  416. &lt;fileset dir="${build.classes.dir}"&gt;
  417. &lt;include name="org/example/**/tests/**/"/&gt;
  418. &lt;/fileset&gt;
  419. &lt;listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/&gt;
  420. &lt;listener type="legacy-plain" sendSysOut="true" /&gt;
  421. &lt;/testclasses&gt;
  422. &lt;/junitlauncher&gt;</pre>
  423. <p>
  424. Selects any <samp>.class</samp> files that match
  425. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  426. the <samp>${build.classes.dir}</samp> and passes those classes to the JUnit 5 platform for
  427. execution as tests. Test results will be written out to the <samp>${output.dir}</samp> by
  428. the <q>legacy-xml</q> and <q>legacy-plain</q> formatters, in separate files. Furthermore, both
  429. the <q>legacy-xml</q> and the <q>legacy-plain</q> listeners, above, are configured to receive
  430. the standard output content generated by the tests. The <q>legacy-xml</q> listener is
  431. configured to receive standard error content as well.
  432. </p>
  433. </body>
  434. </html>