Skip to content

Write a JUnit XML report of each run - #449

Merged
samoht merged 4 commits into
mirage:mainfrom
samoht:junit-xml
Nov 9, 2026
Merged

samoht merged 4 commits into
mirage:mainfrom
samoht:junit-xml

Conversation

@samoht

@samoht samoht commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

With --junit or ALCOTEST_JUNIT=1, each run writes a JUnit XML report. This is experimental: the location and contents of the report may change. The reports of all runs are in one directory:

_build/_tests/junit/<name>-<time>-<id>.xml

<name> is the name given to run. CI systems collect the reports from _build/_tests/junit; the README shows the setup for GitLab, GitHub Actions, CircleCI and Jenkins.

For this test executable:

let () =
  run "my_tests"
    [
      suite "parser"
        [
          test "empty" (fun () -> check int "length" 0 0);
          test "unicode" (fun () -> check int "length" 1 2);
        ];
    ]

the report is (backtrace cut):

<?xml version="1.0" encoding="UTF-8"?>
<testsuites time="0.000"><testsuite name="my_tests" file="parser_test.ml" tests="2" failures="1" errors="0" time="0.000" timestamp="2026-11-06T17:19:17Z"><testcase name="empty" classname="my_tests.parser" time="0.000" file="parser_test.ml" line="8"/>
<testcase name="unicode" classname="my_tests.parser" time="0.000" file="parser_test.ml" line="9"><failure message="FAIL length" type="Check_error">File "parser_test.ml", line 9, character 10:
FAIL length

   Expected: `1'
   Received: `2'
...
</failure>
</testcase>
</testsuite>
</testsuites>

The <testsuite> has the file that calls run, read from the call stack like the location of each test, so reports of executables with the same suite name are told apart. A test's classname is <name>.<suite>, followed by its groups. As in other JUnit reports, a test that fails a check or calls fail is a <failure>, and a test that raises any other exception, such as failwith or assert false, is an <error>; both make the run fail, and CI systems count them separately. A skipped test is <skipped/>, and the output of a failing test goes to <system-out>, keeping the last 64 KiB. file and line are where the test is defined, or where it failed. The report is written at the end of the run or from at_exit, which marks the running test as an <error>. Nothing is written if the process is killed.

Reports validate against the Jenkins xunit schema, apart from file and line, which it doesn't declare but which several consumers read:

The XML printer aims to be simple but robust: it escapes markup, replaces characters that XML forbids, and gives repeated test names a #<index> suffix, since GitLab keeps one test per name. test/e2e/alcotest/junit/test_junit.ml checks this with names such as </testsuite><x a="&bogus;">, ]]><![CDATA[, control characters and invalid UTF-8; test_crash.ml checks a process that exits during a test, and test_long.ml the 64 KiB cap. GitLab's own parser reads all the reports of this repository's tests without an error.

With ALCOTEST_JUNIT=1 exported, the reports accumulate in _build/_tests/junit (#471, #472), so a local JUnit tool can show the history of dune test. Five runs of a suite with a flaky test and a regression, in junit2html (junit2html --report-matrix history.html _build/_tests/junit/*.xml):

History of five runs in junit2html

Known limitation: under js_of_ocaml, a test has no file and line unless its check is given ~pos:__POS__, see #475.

Fixes #130

@samoht
samoht marked this pull request as draft November 2, 2026 15:51
@samoht
samoht force-pushed the junit-xml branch 4 times, most recently from f13bb4c to b6eec23 Compare November 2, 2026 16:26
@samoht
samoht force-pushed the junit-xml branch 4 times, most recently from 1e29b98 to 1d57b21 Compare November 5, 2026 16:34
@samoht samoht changed the title Add JUnit XML reports Write a JUnit XML report next to the logs Nov 5, 2026
@samoht
samoht force-pushed the junit-xml branch 17 times, most recently from 23e33f2 to 1ca0233 Compare November 6, 2026 14:17
@samoht
samoht marked this pull request as ready for review November 6, 2026 14:36
samoht added a commit to samoht/alcotest that referenced this pull request Nov 6, 2026
@samoht
samoht force-pushed the junit-xml branch 3 times, most recently from 895cacd to f93fe7a Compare November 6, 2026 19:41
samoht added a commit to samoht/alcotest that referenced this pull request Nov 6, 2026
@samoht samoht changed the title Write a JUnit XML report next to the logs Write a JUnit XML report of each run Nov 6, 2026
@samoht
samoht force-pushed the junit-xml branch 2 times, most recently from 5c71f1f to 2b58edd Compare November 6, 2026 20:07
@samoht

samoht commented Nov 6, 2026 •

Copy link
Copy Markdown
Member Author

A few years later, @pbiggar here you are, in case you still needs this 😅

@Khady thanks a lot for your previous work on this! Do you have any feedback or advice on what is needed to have this properly integrated with CI systems? Anything missing in your plugin that would be easier to have in core alcotest?

Write it as the file of the testsuite of the JUnit report, so that the reports of a workspace say which test wrote each, in the same form as the file of each test case.
Only CI jobs that collect the report need it, so a run without the flag
builds no report, installs no at_exit writer and, with --verbose,
creates no run directory.

The flag is read from the command line and the environment only, like
--tags: an optional argument of run would break code that restates
with_options.
@samoht
samoht merged commit 0741480 into mirage:main Nov 9, 2026
7 checks passed
@samoht
samoht deleted the junit-xml branch November 9, 2026 09:17
samoht added a commit to samoht/opam-repository that referenced this pull request Nov 10, 2026
CHANGES:

- Experimental: with `--junit` or `ALCOTEST_JUNIT=1`, write a JUnit XML report
  of each run to `junit/<name>-<time>-<id>.xml` of the log directory, with the
  `file` and `line` of each test, or of its failure (mirage/alcotest#449, mirage/alcotest#476, fixes mirage/alcotest#130,
  @samoht, report by @pbiggar)
- Record the source file that calls `run`, and write it as the `file` of the
  `<testsuite>` of the JUnit report (mirage/alcotest#449, @samoht)
- `Platform.S` now requires `timestamp` and `write_file` (mirage/alcotest#449, @samoht)
- Under dune, keep the output of tests in `_build/_tests` of the workspace,
  where dune does not delete it, rather than in `_build/default`. With mirage/alcotest#472, each
  `dune test` adds its runs to `_build/_tests/run/<name>/<time>-<id>`, which gives
  a local history of test results (mirage/alcotest#471, @samoht)
- Fix `alcotest-mirage`, which failed with `Assertion failed` in every run
  without `--verbose` since 1.5.0 (mirage/alcotest#473, @samoht)
- Keep the logs of each run in `run/<name>/<time>-<id>`, where `<name>` is the
  name given to `run`, so that the runs of each test executable are listed in
  order. `latest` still points to the last run (mirage/alcotest#472, @samoht)
- Fix the link named after each test executable, which did not always point to
  its last run. It is replaced by the `run/<name>` folder (mirage/alcotest#472, @samoht)
- Fix a crash when the name of a `suite` or `group` is longer than a file name
  can be (mirage/alcotest#472, @samoht)
- Fix log files hidden when the name of a `suite` or `group` starts with a dot
  (mirage/alcotest#472, @samoht)
- Custom platforms: `prepare_log_trap` takes the name given to `run` and returns
  the folder of the run (mirage/alcotest#472, @samoht)
- Fix creating a log directory written with `/` on Windows, as in `-o a/b`, which
  failed with `ENOENT` (mirage/alcotest#470, @samoht)
- Add `test ?tags name f` and `suite name cases`, to write `suite "a" [ test "b" f ]`
  instead of `("a", [ test_case "b" `Quick f ])`. `tags` defaults to `` `Always ``
  (mirage/alcotest#468, @samoht, combinators suggested by @yawaramin in mirage/alcotest#393)
- Add `?tags` to `group`: its cases only run when these tags hold as well as
  their own (mirage/alcotest#468, @samoht, API from mirage/alcotest#294 by @craigfe)
- Add `group name cases`, a `test_case` that nests `cases` to any depth. They
  run in place, are reported as `parent.name` and are selected with
  `test '^parent\.name'` (mirage/alcotest#453, fixes mirage/alcotest#275, @samoht, API from mirage/alcotest#294 by @craigfe,
  dotted names suggested by @mjambon)
- Show the file and line of every failing test, not only of a failing `check`.
  A failure in a helper points to the line of the test that calls it.
  `ALCOTEST_SOURCE_CODE_POSITION` still turns this off (mirage/alcotest#451, @samoht, based on
  `B0_testing` by @dbuenzli and the b0 programmers)
- Print the message of a failing check once: `ASSERT` now only shows the checks
  that passed (mirage/alcotest#465, @samoht)
- Add wasm_of_ocaml support (mirage/alcotest#461, mirage/alcotest#436, mirage/alcotest#437, @hhugo, @samoht and @mefyl)
- Remove code made redundant by OCaml 4.14 (mirage/alcotest#460, @samoht)
- Close a failed test's log file when printing it raises, instead of leaking the
  file descriptor (mirage/alcotest#460, @samoht)
- Remove the `alcotest-js` package, which had no code and only added dependencies.
  To test with js_of_ocaml, depend on `js_of_ocaml-compiler` directly (mirage/alcotest#463, @samoht)
- Require OCaml 4.14 and replace `uutf` with the stdlib UTF-8 decoder. Test names
  with invalid UTF-8 can get a different log file name (mirage/alcotest#456, @samoht)
- Print valid UTF-8 as is in `string` failures, when the output supports it
  (mirage/alcotest#456, fixes mirage/alcotest#408, @samoht, report by @feverdreme, suggestion by @lindig)
- Add `iarray`, a testable for `Iarray.t`, available from OCaml 5.4
  (mirage/alcotest#455, fixes mirage/alcotest#434, @samoht, report by @JanBeh)
- Fix UTF-8 locale detection, which never matched since 1.8.0, so `Fmt.if_utf_8`
  printers always took the non-UTF-8 branch. `LC_ALL` now overrides `LC_CTYPE` and
  `LANG`, as in POSIX, and `UTF8` also counts (mirage/alcotest#457, @samoht)
- Recognise `Alcotest.skip` and failed checks in `alcotest-async` tests: Async
  wrapped the exceptions, so a skip counted as a failure (mirage/alcotest#278, @vbmithr)
- Make `alcotest-async` unavailable on Windows, so that ocaml-ci tests the other
  packages there (mirage/alcotest#454, @samoht, suggested by @mtelvers)
- Remove stdlib-shims and ocaml-syntax-shims (mirage/alcotest#452, @copy)
- Update ocamlformat to 0.29.0 (mirage/alcotest#450, @samoht)
- Fix and modernise CI (mirage/alcotest#445, mirage/alcotest#446, mirage/alcotest#436, mirage/alcotest#407, @samoht, @hhugo and @MisterDA,
  report by @edwintorok and @ngorogiannis)
- Add `uchar`, a testable for `Uchar.t` (mirage/alcotest#435, @emengd, review by @dinosaure)
- Fix the `success` count in `--json` output, which included failures
  (mirage/alcotest#432, fixes mirage/alcotest#431, @1zumiSagiri, review by @MisterDA)
- Add conditions on tests: `test_case` and test tuples accept `` `Tag ``,
  `` `And ``, `` `Or ``, `` `Not `` and `` `Always ``, also as `Tag` combinators, which
  hold in a context set by a new `--tags` option or `ALCOTEST_TAGS`, as with Go
  build tags. A new `tags` subcommand lists them. `speed_level` is now an alias
  of the new `tag` type, which breaks code matching on it exhaustively or
  restating it as `` [ `Quick | `Slow ] ``
  (mirage/alcotest#448, fixes mirage/alcotest#124, mirage/alcotest#151 and mirage/alcotest#316, @samoht, report by @hannesm and @arvidj)
- Preserve absolute log directories: `mkdir_p` turned `/foo` into `//foo`, which
  fails with `EROFS` on Cygwin (mirage/alcotest#405, fixes mirage/alcotest#404, @bbonf, review by @MisterDA
  and @samoht)
- Allow callers to override `and_exit` and `record_backtrace` in the `test`
  subcommand (mirage/alcotest#439, fixes mirage/alcotest#438, @edwintorok)
- Keep `Unix.stdout` and `Unix.stderr` valid during tests on Windows
  (mirage/alcotest#444, fixes mirage/alcotest#443, @ngorogiannis)
- Test `alcotest-async` exception handling (mirage/alcotest#279, @craigfe)
- Add an `alcotest-async` example (mirage/alcotest#458, mirage/alcotest#421, @dannywillems)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support junit.xml format

1 participant