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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. <li>Finding the JUnit 5 framework libraries (which include the API jars and test engine
  138. jars). The complete set of jars that are relevant in JUnit 5 framework are listed in
  139. the <a href="#junit5deps">dependencies</a> section</li>
  140. </ul>
  141. <p>
  142. If the <code>classpath</code> element isn't configured for the task, then the classpath of Ant
  143. itself will be used for finding the test classes and JUnit 5 libraries.
  144. </p>
  145. <h4>listener</h4>
  146. <p>
  147. The <code>junitlauncher</code> task can be configured with <code>listener</code>(s) to listen to
  148. test execution events (such as a test execution starting, completing etc...). The listener is
  149. expected to be a class which implements
  150. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>.
  151. This <code class="code">TestExecutionListener</code> interface is an API exposed by the JUnit 5
  152. platform APIs and isn't specific to Ant. As such, you can use any existing implementation
  153. of <code class="code">TestExecutionListener</code> in this task.
  154. </p>
  155. <h5>Test result formatter</h5>
  156. <p>
  157. <code>junitlauncher</code> provides a way where the test execution results can be formatted and
  158. presented in a way that's customizable. The task allows for configuring test result formatters,
  159. through the use of <code>listener</code> element. As noted previously, the <code>listener</code>
  160. element expects the listener to implement
  161. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>
  162. interface. Typically, result formatters need a bit more configuration details to be fed to them,
  163. during the test execution&mdash;details like where to write out the formatted result. Any such
  164. listener can optionally implement
  165. the <code class="code">org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  166. interface. This interface is specific to Ant <code>junitlauncher</code> task and it extends
  167. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code> interface
  168. </p>
  169. <p>
  170. The <code>junitlauncher</code> task comes with the following pre-defined test result formatter
  171. types:
  172. </p>
  173. <ul>
  174. <li>
  175. <q>legacy-plain</q> : This formatter prints a short statistics line for all test
  176. cases.
  177. </li>
  178. <li>
  179. <q>legacy-brief</q> : This formatter prints information for tests that failed or were
  180. skipped.
  181. </li>
  182. <li>
  183. <q>legacy-xml</q> : This formatter prints statistics for the tests in XML format.
  184. </li>
  185. </ul>
  186. <p>
  187. <strong>Note</strong>: Each of these formatters named <q>legacy</q> try to format the results
  188. similar to what the <code>junit</code> task's formatters used to do. Furthermore,
  189. the <q>legacy-xml</q> formatter generates the XML to comply with the same schema that
  190. the <code>junit</code> task's XML formatter used to follow. As a result, the XML generated by
  191. this formatter, can be used as-is by the <code>junitreport</code> task.
  192. </p>
  193. <p>
  194. The <code>listener</code> element supports the following attributes:
  195. </p>
  196. <table class="attr">
  197. <tr>
  198. <th>Attribute</th>
  199. <th>Description</th>
  200. <th>Required</th>
  201. </tr>
  202. <tr>
  203. <td>type</td>
  204. <td>Use a predefined formatter (either <q>legacy-xml</q>, <q>legacy-plain</q>
  205. or <q>legacy-brief</q>).</td>
  206. <td rowspan="2">Exactly one of these</td>
  207. </tr>
  208. <tr>
  209. <td>classname</td>
  210. <td class="left">Name of a listener class which
  211. implements <code>org.junit.platform.launcher.TestExecutionListener</code> or
  212. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  213. interface
  214. </td>
  215. </tr>
  216. <tr>
  217. <td>resultFile</td>
  218. <td>
  219. The file name to which the formatted result needs to be written to. This attribute is
  220. only relevant when the listener class implements
  221. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  222. interface.
  223. <p>
  224. If no value is specified for this attribute and the listener implements
  225. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  226. then the file name will be defaulted to and will be of the
  227. form <code>TEST-<i>testname</i>.<i>formatter-specific-extension</i></code>
  228. (ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type
  229. formatter)
  230. </p>
  231. </td>
  232. <td>No</td>
  233. </tr>
  234. <tr>
  235. <td>sendSysOut</td>
  236. <td>If set to <q>true</q> then the listener will be passed the <code>stdout</code> content
  237. generated by the test(s). This attribute is relevant only if the listener class
  238. implements
  239. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  240. interface.</td>
  241. <td>No; defaults to <q>false</q></td>
  242. </tr>
  243. <tr>
  244. <td>sendSysErr</td>
  245. <td>If set to <q>true</q> then the listener will be passed the <code>stderr</code> content
  246. generated by the test(s). This attribute is relevant only if the listener class
  247. implements
  248. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  249. interface.</td>
  250. <td>No; defaults to <q>false</q></td>
  251. </tr>
  252. <tr>
  253. <td>if</td>
  254. <td>Only use this listener <a href="../properties.html#if+unless">if the named property is
  255. set</a>.</td>
  256. <td>No</td>
  257. </tr>
  258. <tr>
  259. <td>unless</td>
  260. <td>Only use this listener <a href="../properties.html#if+unless">if the named property
  261. is <strong>not</strong> set</a>.</td>
  262. <td>No</td>
  263. </tr>
  264. </table>
  265. <h4>test</h4>
  266. <p>Defines a single test class.</p>
  267. <table class="attr">
  268. <tr>
  269. <th>Attribute</th>
  270. <th>Description</th>
  271. <th>Required</th>
  272. </tr>
  273. <tr>
  274. <td>name</td>
  275. <td>Fully qualified name of the test class.</td>
  276. <td>Yes</td>
  277. </tr>
  278. <tr>
  279. <td>methods</td>
  280. <td>Comma-separated list of names of test case methods to execute. If this is specified,
  281. then only these test methods from the test class will be executed.</td>
  282. <td>No</td>
  283. </tr>
  284. <tr>
  285. <td>haltOnFailure</td>
  286. <td>Stop the build process if a failure occurs during the test run (exceptions are
  287. considered as failures too). Overrides value set on <code>junitlauncher</code>
  288. element.</td>
  289. <td>No</td>
  290. </tr>
  291. <tr>
  292. <td>failureProperty</td>
  293. <td>The name of a property to set in the event of a failure (exceptions are considered
  294. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  295. <td>No</td>
  296. </tr>
  297. <tr>
  298. <td>outputDir</td>
  299. <td>Directory to write the reports to.</td>
  300. <td>No; default is the base directory of the project.</td>
  301. </tr>
  302. <tr>
  303. <td>if</td>
  304. <td>Only run this test <a href="../properties.html#if+unless">if the named property is
  305. set</a>.</td>
  306. <td>No</td>
  307. </tr>
  308. <tr>
  309. <td>unless</td>
  310. <td>Only run this test <a href="../properties.html#if+unless">if the named property
  311. is <strong>not</strong> set</a>.</td>
  312. <td>No</td>
  313. </tr>
  314. </table>
  315. <p>
  316. Tests can define their own listeners via nested <code>listener</code> elements.
  317. </p>
  318. <h4>testclasses</h4>
  319. <p>Define a number of tests based on pattern matching.</p>
  320. <p>
  321. <code>testclasses</code> collects the included <a href="../Types/resources.html">resources</a>
  322. from any number of nested <a href="../Types/resources.html#collection">Resource
  323. Collection</a>s. It then selects each resource whose name ends in <code>.class</code>. These
  324. classes are then passed on to the JUnit 5 platform for it to decide and run them as tests.
  325. </p>
  326. <table class="attr">
  327. <tr>
  328. <th>Attribute</th>
  329. <th>Description</th>
  330. <th>Required</th>
  331. </tr>
  332. <tr>
  333. <td>haltOnFailure</td>
  334. <td>Stop the build process if a failure occurs during the test run (exceptions are
  335. considered as failures too). Overrides value set on <code>junitlauncher</code>
  336. element.</td>
  337. <td>No</td>
  338. </tr>
  339. <tr>
  340. <td>failureProperty</td>
  341. <td>The name of a property to set in the event of a failure (exceptions are considered
  342. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  343. <td>No</td>
  344. </tr>
  345. <tr>
  346. <td>outputDir</td>
  347. <td>Directory to write the reports to.</td>
  348. <td>No; default is the base directory of the project.</td>
  349. </tr>
  350. <tr>
  351. <td>if</td>
  352. <td>Only run the tests <a href="../properties.html#if+unless">if the named property is
  353. set</a>.</td>
  354. <td>No</td>
  355. </tr>
  356. <tr>
  357. <td>unless</td>
  358. <td>Only run the tests <a href="../properties.html#if+unless">if the named property
  359. is <strong>not</strong> set</a>.</td>
  360. <td>No</td>
  361. </tr>
  362. </table>
  363. <p>
  364. <code>testclasses</code> can define their own listeners via nested <code>listener</code>
  365. 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;</pre>
  376. <p>
  377. Launches the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> test
  378. </p>
  379. <pre>
  380. &lt;junitlauncher&gt;
  381. &lt;classpath refid="test.classpath"/&gt;
  382. &lt;test name="org.myapp.SimpleTest" haltOnFailure="true"/&gt;
  383. &lt;test name="org.myapp.AnotherTest"/&gt;
  384. &lt;/junitlauncher&gt;
  385. </pre>
  386. <p>
  387. Launches the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> and
  388. the <samp>org.myapp.AnotherTest</samp> tests. The build process will be stopped if any test, in
  389. the <samp>org.myapp.SimpleTest</samp>, fails.
  390. </p>
  391. <pre>
  392. &lt;junitlauncher&gt;
  393. &lt;classpath refid="test.classpath"/&gt;
  394. &lt;test name="org.myapp.SimpleTest" methods="testFoo, testBar"/&gt;
  395. &lt;/junitlauncher&gt;</pre>
  396. <p>
  397. Launches the JUnit 5 platform to run only the <samp>testFoo</samp> and <samp>testBar</samp>
  398. methods of the <samp>org.myapp.SimpleTest</samp> test class.
  399. </p>
  400. <pre>
  401. &lt;junitlauncher&gt;
  402. &lt;classpath refid="test.classpath"/&gt;
  403. &lt;testclasses outputdir="${output.dir}"&gt;
  404. &lt;fileset dir="${build.classes.dir}"&gt;
  405. &lt;include name="org/example/**/tests/**/"/&gt;
  406. &lt;/fileset&gt;
  407. &lt;/testclasses&gt;
  408. &lt;/junitlauncher&gt;</pre>
  409. <p>
  410. Selects any <samp>.class</samp> files that match
  411. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  412. the <samp>${build.classes.dir}</samp> and passes those classes to the JUnit 5 platform for
  413. execution as tests.
  414. </p>
  415. <pre>
  416. &lt;junitlauncher&gt;
  417. &lt;classpath refid="test.classpath"/&gt;
  418. &lt;testclasses outputdir="${output.dir}"&gt;
  419. &lt;fileset dir="${build.classes.dir}"&gt;
  420. &lt;include name="org/example/**/tests/**/"/&gt;
  421. &lt;/fileset&gt;
  422. &lt;listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/&gt;
  423. &lt;listener type="legacy-plain" sendSysOut="true" /&gt;
  424. &lt;/testclasses&gt;
  425. &lt;/junitlauncher&gt;</pre>
  426. <p>
  427. Selects any <samp>.class</samp> files that match
  428. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  429. the <samp>${build.classes.dir}</samp> and passes those classes to the JUnit 5 platform for
  430. execution as tests. Test results will be written out to the <samp>${output.dir}</samp> by
  431. the <q>legacy-xml</q> and <q>legacy-plain</q> formatters, in separate files. Furthermore, both
  432. the <q>legacy-xml</q> and the <q>legacy-plain</q> listeners, above, are configured to receive
  433. the standard output content generated by the tests. The <q>legacy-xml</q> listener is
  434. configured to receive standard error content as well.
  435. </p>
  436. </body>
  437. </html>