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

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