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 28 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <!DOCTYPE html>
  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. https://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. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>JUnitLauncher Task</title>
  20. </head>
  21. <body>
  22. <h2 id="junitlauncher">JUnitLauncher</h2>
  23. <h3>Description</h3>
  24. <p>
  25. This task allows tests to be launched and run using the JUnit 5 framework.
  26. </p>
  27. <p>
  28. JUnit 5 introduced a newer set of APIs to write and launch tests. It also introduced the concept
  29. of test engines. Test engines decide which classes are considered as testcases and how they are
  30. executed. JUnit 5 supports running tests that have been written using JUnit 4 constructs as well
  31. as tests that have been written using JUnit 5 constructs. For more details about JUnit 5
  32. itself, please refer to the JUnit 5 project's documentation
  33. at <a href="https://junit.org/junit5/">https://junit.org/junit5/</a>.
  34. </p>
  35. <p>
  36. The goal of this <code>junitlauncher</code> task is to allow launching the JUnit 5 test launcher
  37. and building the test requests so that the selected tests can then be parsed and executed by the
  38. test engine(s) supported by JUnit 5. This task in itself does <i>not</i> understand what a test
  39. case is nor does it execute the tests itself.
  40. </p>
  41. <p>
  42. <strong>Note</strong>: This task depends on external libraries not included in the Apache Ant
  43. distribution. See <a href="../install.html#librarydependencies">Library Dependencies</a> for
  44. more information.
  45. </p>
  46. <p>
  47. <strong>Note</strong>: You must have the necessary JUnit 5 libraries in the classpath of the
  48. tests. At the time of writing this documentation, the list of JUnit 5 platform libraries that
  49. are necessary to run the tests are:
  50. </p>
  51. <ul id="junit-platform-libraries">
  52. <li>
  53. <samp>junit-platform-commons.jar</samp>
  54. </li>
  55. <li>
  56. <samp>junit-platform-engine.jar</samp>
  57. </li>
  58. <li>
  59. <samp>junit-platform-launcher.jar</samp>
  60. </li>
  61. <li>
  62. <samp>opentest4j.jar</samp>
  63. </li>
  64. </ul>
  65. <p>
  66. Depending on the test engine(s) that you want to use in your tests, you will further need the
  67. following libraries in the classpath
  68. </p>
  69. <p id="junit-vintage-engine-libraries">
  70. For <q>junit-vintage</q> engine:
  71. </p>
  72. <ul>
  73. <li>
  74. <samp>junit-vintage-engine.jar</samp>
  75. </li>
  76. <li>
  77. <samp>junit.jar</samp> (JUnit 4.x version)
  78. </li>
  79. </ul>
  80. <p id="junit-jupiter-engine-libraries">
  81. For <q>junit-jupiter</q> engine:
  82. </p>
  83. <ul>
  84. <li>
  85. <samp>junit-jupiter-api.jar</samp>
  86. </li>
  87. <li>
  88. <samp>junit-jupiter-engine.jar</samp>
  89. </li>
  90. </ul>
  91. <p>
  92. To have these in the test classpath, you can follow <em>either</em> of the following approaches:
  93. </p>
  94. <ul id="setup">
  95. <li id="setup-recommended"><b>Recommended approach since Ant 1.10.6</b>: Place the <samp>ant-junitlauncher.jar</samp> in <samp>ANT_HOME/lib</samp> directory
  96. and use the nested <code>&lt;classpath&gt;</code> element to specify the location of the
  97. the rest of the JUnit specific jars (noted above). Please read the
  98. <a href=#nested-classpath>using classpath element</a> section for more details.</li>
  99. <li>OR Put all these relevant jars along with the <samp>ant-junitlauncher.jar</samp>
  100. in <samp>ANT_HOME/lib</samp> directory</li>
  101. <li>OR Put <samp>ant-junitlauncher.jar</samp> in the <samp>ANT_HOME/lib</samp> directory and
  102. include all other relevant jars in the classpath by passing them as a <kbd>-lib</kbd>
  103. option, while invoking Ant</li>
  104. </ul>
  105. <p>
  106. Tests are defined by nested elements like <code>test</code>, <code>testclasses</code> tags
  107. (see <a href="#nested">nested elements</a>).
  108. </p>
  109. <h3>Parameters</h3>
  110. <table class="attr">
  111. <tr>
  112. <th scope="col">Attribute</th>
  113. <th scope="col">Description</th>
  114. <th scope="col">Required</th>
  115. </tr>
  116. <tr>
  117. <td>includeTags</td>
  118. <td>A comma separated list of <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations">JUnit 5 tags</a>, describing the tests to include.
  119. <p><em>Since Ant 1.10.7</em></p>
  120. </td>
  121. <td>No</td>
  122. </tr>
  123. <tr>
  124. <td>excludeTags</td>
  125. <td>A comma separated list of <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations">JUnit 5 tags</a>, describing the tests to exclude.
  126. <p><em>Since Ant 1.10.7</em></p>
  127. </td>
  128. <td>No</td>
  129. </tr>
  130. <tr>
  131. <td>haltOnFailure</td>
  132. <td>A value of <q>true</q> implies that build has to stop if any failure occurs in any of
  133. the tests. JUnit 4+ classifies failures as both assertion failures as well as exceptions
  134. that get thrown during test execution. As such, this task too considers both these cases
  135. as failures and doesn't distinguish one from another.
  136. </td>
  137. <td>No; default is <q>false</q></td>
  138. </tr>
  139. <tr>
  140. <td>failureProperty</td>
  141. <td>The name of a property to set in the event of a failure
  142. (exceptions in tests are considered failures as well).
  143. </td>
  144. <td>No</td>
  145. </tr>
  146. <tr>
  147. <td>printSummary</td>
  148. <td>If the value is set to <code>true</code> then this task, upon completion of the test execution,
  149. prints the summary of the execution to <code>System.out</code>. The summary itself is generated
  150. by the JUnit 5 platform and not by this task.
  151. </td>
  152. <td>No; defaults to <code>false</code></td>
  153. </tr>
  154. </table>
  155. <h3 id="nested">Nested Elements</h3>
  156. <h4 id="nested-classpath">classpath</h4>
  157. <p>
  158. The nested <code>&lt;classpath&gt;</code> element that represents
  159. a <a href="../using.html#path">PATH like structure</a> can be used to configure the task to use
  160. this classpath for finding and running the tests. This classpath will be used for:
  161. </p>
  162. <ul>
  163. <li>Finding the test classes to execute</li>
  164. <li>Finding test engines that run the tests</li>
  165. <li>If <a href="#setup-recommended">configured to do so</a>, finding all necessary JUnit libraries</li>
  166. </ul>
  167. <p>
  168. If the <code>classpath</code> element isn't configured for the task, then the classpath of Ant
  169. itself will be used for finding the test classes and the JUnit libraries.
  170. </p>
  171. <p>
  172. Below is an example of setting up the classpath to include the Jupiter test engine and
  173. the JUnit platform libraries during the execution of the tests.
  174. </p>
  175. <pre>
  176. &lt;project&gt;
  177. &lt;property name="output.dir" value="${basedir}/build"/&gt;
  178. &lt;property name="src.test.dir" value="${basedir}/src/test"/&gt;
  179. &lt;property name="build.classes.dir" value="${output.dir}/classes"/&gt;
  180. &lt;target name="init"&gt;
  181. &lt;mkdir dir="${output.dir}"/&gt;
  182. &lt;/target&gt;
  183. &lt;path id="junit.platform.libs.classpath"&gt;
  184. &lt;fileset dir="${basedir}/src/lib/junit-platform/"/&gt;
  185. &lt;/path&gt;
  186. &lt;path id="junit.engine.jupiter.classpath"&gt;
  187. &lt;fileset dir="${basedir}/src/lib/jupiter/"/&gt;
  188. &lt;/path&gt;
  189. &lt;target name="compile-test" depends="init"&gt;
  190. &lt;mkdir dir="${build.classes.dir}"/&gt;
  191. &lt;javac srcdir="${src.test.dir}"
  192. destdir="${build.classes.dir}"&gt;
  193. &lt;!-- our tests only need JUnit Jupiter engine
  194. libraries in our compile classpath for the tests --&gt;
  195. &lt;classpath refid="junit.engine.jupiter.classpath"/&gt;
  196. &lt;/javac&gt;
  197. &lt;/target&gt;
  198. &lt;target name="test" depends="compile-test"&gt;
  199. &lt;junitlauncher&gt;
  200. &lt;!-- include the JUnit platform related libraries
  201. required to run the tests --&gt;
  202. &lt;classpath refid="junit.platform.libs.classpath"/&gt;
  203. &lt;!-- include the JUnit Jupiter engine libraries --&gt;
  204. &lt;classpath refid="junit.engine.jupiter.classpath"/&gt;
  205. &lt;classpath&gt;
  206. &lt;!-- the test classes themselves --&gt;
  207. &lt;pathelement location="${build.classes.dir}"/&gt;
  208. &lt;/classpath&gt;
  209. &lt;testclasses outputdir="${output.dir}"&gt;
  210. &lt;fileset dir="${build.classes.dir}"/&gt;
  211. &lt;listener type="legacy-brief" sendSysOut="true"/&gt;
  212. &lt;listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/&gt;
  213. &lt;/testclasses&gt;
  214. &lt;/junitlauncher&gt;
  215. &lt;/target&gt;
  216. &lt;/project&gt;
  217. </pre>
  218. <p>
  219. In the example above,
  220. </p>
  221. <ul>
  222. <li>The <code>src/lib/jupiter</code> directory is expected to contain
  223. the Jupiter test engine related jars (which have been
  224. <a href="#junit-jupiter-engine-libraries">listed in an earlier section of this
  225. document</a>).</li>
  226. <li>The <code>src/lib/junit-platform</code> directory is expected to
  227. contain the JUnit platform jars (which have been
  228. <a href="#junit-platform-libraries">listed in an earlier section of this
  229. document</a>)</li>
  230. </ul>
  231. <p>
  232. In the <code>test</code> target we use the <code>classpath</code> nested element
  233. to point to the <code>junit.engine.jupiter.classpath</code> and <code>junit.platform.libs.classpath</code>
  234. containing those jars.
  235. In this <code>test</code> target we also use another <code>classpath</code> element to point to
  236. the location containing our test classes. If required, all these classpaths can be combined
  237. into one.
  238. </p>
  239. <h4>listener</h4>
  240. <p>
  241. The <code>junitlauncher</code> task can be configured with <code>listener</code>(s) to listen to
  242. test execution events (such as a test execution starting, completing etc...). The listener is
  243. expected to be a class which implements
  244. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>.
  245. This <code class="code">TestExecutionListener</code> interface is an API exposed by the JUnit 5
  246. platform APIs and isn't specific to Ant. As such, you can use any existing implementation
  247. of <code class="code">TestExecutionListener</code> in this task.
  248. </p>
  249. <h5>Test result formatter</h5>
  250. <p>
  251. <code>junitlauncher</code> provides a way where the test execution results can be formatted and
  252. presented in a way that's customizable. The task allows for configuring test result formatters,
  253. through the use of <code>listener</code> element. As noted previously, the <code>listener</code>
  254. element expects the listener to implement
  255. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code>
  256. interface. Typically, result formatters need a bit more configuration details to be fed to them,
  257. during the test execution&mdash;details like where to write out the formatted result. Any such
  258. listener can optionally implement
  259. the <code class="code">org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  260. interface. This interface is specific to Ant <code>junitlauncher</code> task and it extends
  261. the <code class="code">org.junit.platform.launcher.TestExecutionListener</code> interface
  262. </p>
  263. <p>
  264. The <code>junitlauncher</code> task comes with the following pre-defined test result formatter
  265. types:
  266. </p>
  267. <ul>
  268. <li>
  269. <q>legacy-plain</q> : This formatter prints a short statistics line for all test
  270. cases.
  271. </li>
  272. <li>
  273. <q>legacy-brief</q> : This formatter prints information for tests that failed or were
  274. skipped.
  275. </li>
  276. <li>
  277. <q>legacy-xml</q> : This formatter prints statistics for the tests in XML format.
  278. </li>
  279. </ul>
  280. <p>
  281. <strong>Note</strong>: Each of these formatters named <q>legacy</q> try to format the results
  282. similar to what the <code>junit</code> task's formatters used to do. Furthermore,
  283. the <q>legacy-xml</q> formatter generates the XML to comply with the same schema that
  284. the <code>junit</code> task's XML formatter used to follow. As a result, the XML generated by
  285. this formatter, can be used as-is by the <code>junitreport</code> task.
  286. </p>
  287. <p>
  288. The <code>listener</code> element supports the following attributes:
  289. </p>
  290. <table class="attr">
  291. <tr>
  292. <th scope="col">Attribute</th>
  293. <th scope="col">Description</th>
  294. <th scope="col">Required</th>
  295. </tr>
  296. <tr>
  297. <td>type</td>
  298. <td>Use a predefined formatter (either <q>legacy-xml</q>, <q>legacy-plain</q>
  299. or <q>legacy-brief</q>).</td>
  300. <td rowspan="2">Exactly one of these</td>
  301. </tr>
  302. <tr>
  303. <td>classname</td>
  304. <td class="left">Name of a listener class which
  305. implements <code>org.junit.platform.launcher.TestExecutionListener</code> or
  306. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  307. interface
  308. </td>
  309. </tr>
  310. <tr>
  311. <td>resultFile</td>
  312. <td>
  313. The file name to which the formatted result needs to be written to. This attribute is
  314. only relevant when the listener class implements
  315. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  316. interface.
  317. <p>
  318. If no value is specified for this attribute and the listener implements
  319. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  320. then the file name will be defaulted to and will be of the
  321. form <code>TEST-<i>testname</i>.<i>formatter-specific-extension</i></code>
  322. (ex: <samp>TEST-org.myapp.SomeTest.xml</samp> for the <q>legacy-xml</q> type
  323. formatter)
  324. </p>
  325. <p>
  326. This file is considered relative to the <code>outputDir</code> configured on the listener.
  327. If no <code>outputDir</code> is set on the listener, then the file is considered relative to the
  328. <code>outputDir</code> of the test in context of which this listener is being run.
  329. </p>
  330. </td>
  331. <td>No</td>
  332. </tr>
  333. <tr>
  334. <td>outputDir</td>
  335. <td>Directory into which to create the output of the listener.
  336. <p><em>Since Ant 1.10.6</em></p>
  337. </td>
  338. <td>No</td>
  339. </tr>
  340. <tr>
  341. <td>sendSysOut</td>
  342. <td>If set to <q>true</q> then the listener will be passed the <code>stdout</code> content
  343. generated by the test(s). This attribute is relevant only if the listener class
  344. implements
  345. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  346. interface.</td>
  347. <td>No; defaults to <q>false</q></td>
  348. </tr>
  349. <tr>
  350. <td>sendSysErr</td>
  351. <td>If set to <q>true</q> then the listener will be passed the <code>stderr</code> content
  352. generated by the test(s). This attribute is relevant only if the listener class
  353. implements
  354. the <code>org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter</code>
  355. interface.</td>
  356. <td>No; defaults to <q>false</q></td>
  357. </tr>
  358. <tr>
  359. <td>if</td>
  360. <td>Only use this listener <a href="../properties.html#if+unless">if the named property is
  361. set</a>.</td>
  362. <td>No</td>
  363. </tr>
  364. <tr>
  365. <td>unless</td>
  366. <td>Only use this listener <a href="../properties.html#if+unless">if the named property
  367. is <strong>not</strong> set</a>.</td>
  368. <td>No</td>
  369. </tr>
  370. </table>
  371. <h4>test</h4>
  372. <p>Defines a single test class.</p>
  373. <table class="attr">
  374. <tr>
  375. <th scope="col">Attribute</th>
  376. <th scope="col">Description</th>
  377. <th scope="col">Required</th>
  378. </tr>
  379. <tr>
  380. <td>name</td>
  381. <td>Fully qualified name of the test class.</td>
  382. <td>Yes</td>
  383. </tr>
  384. <tr>
  385. <td>methods</td>
  386. <td>Comma-separated list of names of test case methods to execute. If this is specified,
  387. then only these test methods from the test class will be executed.</td>
  388. <td>No</td>
  389. </tr>
  390. <tr>
  391. <td>haltOnFailure</td>
  392. <td>Stop the build process if a failure occurs during the test run (exceptions are
  393. considered as failures too). Overrides value set on <code>junitlauncher</code>
  394. element.</td>
  395. <td>No</td>
  396. </tr>
  397. <tr>
  398. <td>failureProperty</td>
  399. <td>The name of a property to set in the event of a failure (exceptions are considered
  400. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  401. <td>No</td>
  402. </tr>
  403. <tr>
  404. <td>outputDir</td>
  405. <td>Directory to write the reports to.</td>
  406. <td>No; default is the base directory of the project.</td>
  407. </tr>
  408. <tr>
  409. <td>if</td>
  410. <td>Only run this test <a href="../properties.html#if+unless">if the named property is
  411. set</a>.</td>
  412. <td>No</td>
  413. </tr>
  414. <tr>
  415. <td>unless</td>
  416. <td>Only run this test <a href="../properties.html#if+unless">if the named property
  417. is <strong>not</strong> set</a>.</td>
  418. <td>No</td>
  419. </tr>
  420. <tr>
  421. <td>includeEngines</td>
  422. <td>A comma separated set of test engine ids. If specified, only these test engine(s)
  423. will be used for running the tests.
  424. <br/>
  425. For example: <code>includeEngines="junit-jupiter"</code> will only use the Jupiter
  426. test engine for execution of the tests and will ignore any other engines that might
  427. have been found in the classpath.
  428. </td>
  429. <td>No</td>
  430. </tr>
  431. <tr>
  432. <td>excludeEngines</td>
  433. <td>A comma separated set of test engine ids. If specified, these test engine(s)
  434. will be excluded when running the tests.
  435. <br/>
  436. For example: <code>excludeEngines="junit-vintage"</code> will exclude the vintage
  437. test engine during execution of the tests and will use any other engines that might
  438. have been found in the classpath.
  439. </td>
  440. <td>No</td>
  441. </tr>
  442. </table>
  443. <p>
  444. Tests can define their own listeners via nested <code>listener</code> elements.
  445. </p>
  446. <p>
  447. The <a href="#fork">fork</a> nested element can be used to run the test in a newly forked
  448. JVM.
  449. </p>
  450. <h4>testclasses</h4>
  451. <p>Define a number of tests based on pattern matching.</p>
  452. <p>
  453. <code>testclasses</code> collects the included <a href="../Types/resources.html">resources</a>
  454. from any number of nested <a href="../Types/resources.html#collection">Resource
  455. Collection</a>s. It then selects each resource whose name ends in <code>.class</code>. These
  456. classes are then passed on to the JUnit 5 platform for it to decide and run them as tests.
  457. </p>
  458. <table class="attr">
  459. <tr>
  460. <th scope="col">Attribute</th>
  461. <th scope="col">Description</th>
  462. <th scope="col">Required</th>
  463. </tr>
  464. <tr>
  465. <td>haltOnFailure</td>
  466. <td>Stop the build process if a failure occurs during the test run (exceptions are
  467. considered as failures too). Overrides value set on <code>junitlauncher</code>
  468. element.</td>
  469. <td>No</td>
  470. </tr>
  471. <tr>
  472. <td>failureProperty</td>
  473. <td>The name of a property to set in the event of a failure (exceptions are considered
  474. failures as well). Overrides value set on <code>junitlauncher</code> element.</td>
  475. <td>No</td>
  476. </tr>
  477. <tr>
  478. <td>outputDir</td>
  479. <td>Directory to write the reports to.</td>
  480. <td>No; default is the base directory of the project.</td>
  481. </tr>
  482. <tr>
  483. <td>if</td>
  484. <td>Only run the tests <a href="../properties.html#if+unless">if the named property is
  485. set</a>.</td>
  486. <td>No</td>
  487. </tr>
  488. <tr>
  489. <td>unless</td>
  490. <td>Only run the tests <a href="../properties.html#if+unless">if the named property
  491. is <strong>not</strong> set</a>.</td>
  492. <td>No</td>
  493. </tr>
  494. <tr>
  495. <td>includeEngines</td>
  496. <td>A comma separated set of test engine ids. If specified, only these test engine(s)
  497. will be used for running the tests.
  498. <br/>
  499. For example: <code>includeEngines="junit-jupiter"</code> will only use the Jupiter
  500. test engine for execution of the tests and will ignore any other engines that might
  501. have been found in the classpath.
  502. </td>
  503. <td>No</td>
  504. </tr>
  505. <tr>
  506. <td>excludeEngines</td>
  507. <td>A comma separated set of test engine ids. If specified, these test engine(s)
  508. will be excluded when running the tests.
  509. <br/>
  510. For example: <code>excludeEngines="junit-vintage"</code> will exclude the vintage
  511. test engine during execution of the tests and will use any other engines that might
  512. have been found in the classpath.
  513. </td>
  514. <td>No</td>
  515. </tr>
  516. </table>
  517. <p>
  518. <code>testclasses</code> can define their own listeners via nested <code>listener</code>
  519. elements.
  520. </p>
  521. <p>
  522. The <a href="#fork">fork</a> nested element can be used to run the tests in a newly forked
  523. JVM. All tests that are part of this <code>testclasses</code> element will run in one single
  524. instance of the newly forked JVM.
  525. </p>
  526. <h4 id="fork">fork</h4>
  527. <p><em>Since Ant 1.10.6</em></p>
  528. Tests launched using the <code>junitlauncher</code> task, by default, run in the same JVM that
  529. initiates the task. This behaviour can be changed using the <code>fork</code> element.
  530. The <code>fork</code> element and its attributes define the characteristics of
  531. the new JVM instance that will be created to launch the tests.
  532. <table class="attr">
  533. <tr>
  534. <th scope="col">Attribute</th>
  535. <th scope="col">Description</th>
  536. <th scope="col">Required</th>
  537. </tr>
  538. <tr>
  539. <td>dir</td>
  540. <td>The user working directory that will be used for the forked JVM</td>
  541. <td>No</td>
  542. </tr>
  543. <tr>
  544. <td>timeout</td>
  545. <td>A value in milliseconds, specifying a maximum duration, the test
  546. running in this forked JVM is allowed to run. If the test runs longer
  547. than this configured value, then the JVM is killed</td>
  548. <td>No</td>
  549. </tr>
  550. <tr>
  551. <td>includeJUnitPlatformLibraries</td>
  552. <td>If set to <code>true</code>, then the jar files that make up the
  553. JUnit platform, will be included in the runtime classpath of the forked
  554. JVM. If set to <code>false</code>, then the <a href="#nested-classpath">configured classpath</a>
  555. of this task, which will be made available to the runtime classpath of the forked
  556. JVM, is expected to contain the JUnit platform library jars</td>
  557. <td>No. Value defaults to <code>true</code>.</td>
  558. </tr>
  559. <tr>
  560. <td>includeAntRuntimeLibraries</td>
  561. <td>If set to <code>true</code>, then the jar files that make up the
  562. Ant runtime, will be included in the runtime classpath of the forked
  563. JVM. If set to <code>false</code>, then the <a href="#nested-classpath">configured classpath</a>
  564. of this task, which will be made available to the runtime classpath of the forked
  565. JVM, is expected to contain the Ant runtime jars</td>
  566. <td>No. Value defaults to <code>true</code>.</td>
  567. </tr>
  568. </table>
  569. The <code>fork</code> element allows the following nested elements:
  570. <h5>jvmarg</h5>
  571. <p>
  572. Additional JVM arguments may be passed to the forked JVM via the <code>jvmarg</code> elements.
  573. For example:
  574. </p>
  575. <pre>
  576. &lt;fork ...&gt;
  577. &lt;jvmarg value="-Djava.compiler=NONE"/&gt;
  578. ...
  579. &lt;/fork&gt;
  580. </pre>
  581. <p>
  582. <code>jvmarg</code> allows all attributes described in <a href="../using.html#arg">Command-line Arguments</a>
  583. </p>
  584. <h5>sysproperty</h5>
  585. <p>
  586. The <code>sysproperty</code> elements allow passing Java system properties to the forked JVM:
  587. </p>
  588. <pre>
  589. &lt;fork&gt;
  590. &lt;sysproperty key="greeting" value="hello world"/&gt;
  591. ...
  592. &lt;/fork&gt;
  593. </pre>
  594. <p>
  595. The attributes for this element are the same as for <a href="../Tasks/exec.html#env">environment variables</a>
  596. </p>
  597. <h5>syspropertyset</h5>
  598. <p>
  599. You can specify a set of properties to be used as system properties with
  600. <a href="../Types/propertyset.html">syspropertyset</a>(s)
  601. </p>
  602. <h5>env</h5>
  603. <p>
  604. It is possible to specify environment variables to pass to the forked JVM via
  605. nested <code>env</code> elements. For a description of the <code>env</code>
  606. element's attributes, see the description in the <a href="../Tasks/exec.html#env">exec</a> task.
  607. </p>
  608. <h5>modulepath</h5>
  609. <p>
  610. The location of Java modules can be specified using the <code>modulepath</code> element,
  611. which is a <a href="../using.html#path">path-like structure</a>.
  612. </p>
  613. For example:
  614. <pre>
  615. &lt;fork&gt;
  616. &lt;modulepath&gt;
  617. &lt;pathelement location="lib"/&gt;
  618. &lt;pathelement location="dist/test.jar"/&gt;
  619. &lt;/modulepath&gt;
  620. ...
  621. &lt;/fork&gt;
  622. </pre>
  623. <h5>upgrademodulepath</h5>
  624. <p>
  625. The location of Java modules, that replace upgradeable modules in the runtime, can be specified
  626. using the <code>upgrademodulepath</code> element, which is a <a href="../using.html#path">path-like
  627. structure</a>.
  628. </p>
  629. <h3>Examples</h3>
  630. <p>
  631. Launch the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> test
  632. </p>
  633. <pre>
  634. &lt;path id="test.classpath"&gt;
  635. ...
  636. &lt;/path&gt;
  637. &lt;junitlauncher&gt;
  638. &lt;classpath refid="test.classpath"/&gt;
  639. &lt;test name="org.myapp.SimpleTest"/&gt;
  640. &lt;/junitlauncher&gt;</pre>
  641. <p>
  642. Launch the JUnit 5 platform to run the <samp>org.myapp.SimpleTest</samp> and
  643. the <samp>org.myapp.AnotherTest</samp> tests. The build process will be stopped if any test, in
  644. the <samp>org.myapp.SimpleTest</samp>, fails.
  645. </p>
  646. <pre>
  647. &lt;junitlauncher&gt;
  648. &lt;classpath refid="test.classpath"/&gt;
  649. &lt;test name="org.myapp.SimpleTest" haltOnFailure="true"/&gt;
  650. &lt;test name="org.myapp.AnotherTest"/&gt;
  651. &lt;/junitlauncher&gt;
  652. </pre>
  653. <p>
  654. Launch the JUnit 5 platform to run only the <samp>testFoo</samp> and <samp>testBar</samp>
  655. methods of the <samp>org.myapp.SimpleTest</samp> test class.
  656. </p>
  657. <pre>
  658. &lt;junitlauncher&gt;
  659. &lt;classpath refid="test.classpath"/&gt;
  660. &lt;test name="org.myapp.SimpleTest" methods="testFoo, testBar"/&gt;
  661. &lt;/junitlauncher&gt;</pre>
  662. <p>
  663. Select any <samp>.class</samp> files that match
  664. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  665. the <samp>${build.classes.dir}</samp> and passes those classes to the JUnit 5 platform for
  666. execution as tests.
  667. </p>
  668. <pre>
  669. &lt;junitlauncher&gt;
  670. &lt;classpath refid="test.classpath"/&gt;
  671. &lt;testclasses outputdir="${output.dir}"&gt;
  672. &lt;fileset dir="${build.classes.dir}"&gt;
  673. &lt;include name="org/example/**/tests/**/"/&gt;
  674. &lt;/fileset&gt;
  675. &lt;/testclasses&gt;
  676. &lt;/junitlauncher&gt;</pre>
  677. <p>
  678. Select any <samp>.class</samp> files that match
  679. the <samp>org/example/**/tests/**/</samp> <code>fileset</code> filter, under
  680. the <samp>${build.classes.dir}</samp> and pass those classes to the JUnit 5 platform for
  681. execution as tests. Test results will be written out to the <samp>${output.dir}</samp> by
  682. the <q>legacy-xml</q> and <q>legacy-plain</q> formatters, in separate files. Furthermore, both
  683. the <q>legacy-xml</q> and the <q>legacy-plain</q> listeners, above, are configured to receive
  684. the standard output content generated by the tests. The <q>legacy-xml</q> listener is configured
  685. to receive standard error content as well.
  686. </p>
  687. <pre>
  688. &lt;junitlauncher&gt;
  689. &lt;classpath refid="test.classpath"/&gt;
  690. &lt;testclasses outputdir="${output.dir}"&gt;
  691. &lt;fileset dir="${build.classes.dir}"&gt;
  692. &lt;include name="org/example/**/tests/**/"/&gt;
  693. &lt;/fileset&gt;
  694. &lt;listener type="legacy-xml" sendSysOut="true" sendSysErr="true"/&gt;
  695. &lt;listener type="legacy-plain" sendSysOut="true" /&gt;
  696. &lt;/testclasses&gt;
  697. &lt;/junitlauncher&gt;</pre>
  698. </body>
  699. </html>