How do you make a GoogleTest binary write a JUnit-style XML report, and what does the report hold after a --gtest_repeat run?
answer
- xml, colon, path
- trailing slash means a directory
- never overwrites an old file
- rewritten every iteration
basics
~20 sPass --gtest_output=xml:path (or set GTEST_OUTPUT); plain xml writes test_detail.xml, and a path ending in / names a directory where the file is named after the binary. With --gtest_repeat the file is rewritten after every iteration, so it shows only the last one.
solid answer
~40 s`--gtest_output=xml:reports/flaky.xml` writes a report modelled on the Ant `junitreport` format: a `<testsuites>` root for the program, a `<testsuite>` per GoogleTest suite and a `<testcase>` per test, with a `<failure>` element per failed assertion. `--gtest_output=xml` alone writes `test_detail.xml` in the working directory. A value ending in a path separator, `xml:reports/`, is a directory: the file is named after the executable (`flaky_test.xml`), and if it already exists GoogleTest picks `flaky_test_1.xml` instead of overwriting. With `--gtest_repeat`, the XML printer runs at the end of **each** iteration and reopens the file for writing, so the final report describes only the last iteration; in the example it shows `failures="0"` although iteration 1 failed. `--gtest_output=json:...` writes the JSON equivalent. Under CTest, use `gtest_discover_tests(... XML_OUTPUT_DIR dir)` rather than one shared path.
code
console · 3 lines$ grep testsuites reports/flaky_test.xml
<testsuites tests="2" failures="0" disabled="0" errors="0" time="0." timestamp="2026-10-26T21:49:55.354" random_seed="44" name="AllTests">
</testsuites>go deeper
Know that --gtest_output=xml makes GoogleTest write a report file a CI server can read.
Explain the file, path and directory forms, the _1 naming rule, and how suites and tests map to XML elements.
Spot that a repeated run's report shows only the last iteration, and design the job around the exit status or per-attempt reports.
Decide which artefact is the source of truth for test results in the pipeline, so a report format quirk cannot hide a failure.
## Asking for the report GoogleTest prints results to the console by default. A CI system usually wants a file it can parse, and GoogleTest writes one when told to through the flag or its environment variable: | Setting | File written | |---|---| | `--gtest_output=xml` | `test_detail.xml` in the current directory | | `--gtest_output=xml:out/flaky.xml` | exactly that file | | `--gtest_output=xml:reports/` (trailing separator) | `reports/flaky_test.xml`, named after the executable | | `GTEST_OUTPUT=xml:...` | the same, unless the flag is also given, which wins | | `--gtest_output=json:...` | the JSON equivalent | For the directory form, GoogleTest avoids clobbering an earlier report: when `flaky_test.xml` already exists it writes `flaky_test_1.xml`, and so on. Running the example binary twice into `reports/` left both files. A CI job that does not clean the directory therefore accumulates reports, and a collector that reads only `flaky_test.xml` reads the oldest one. The flag is honoured only when `main` called `InitGoogleTest`, as `gtest_main` does. ## What is inside The report is based on the Ant `junitreport` task's format, adapted from Java test classes to GoogleTest's suites: - the root `<testsuites name="AllTests">` is the whole program, with totals `tests`, `failures`, `disabled`, `errors`, `time` and `timestamp`, plus `random_seed` when the run was shuffled; - each `<testsuite>` is one GoogleTest suite, with its own counts; - each `<testcase>` is one test, with `name`, `file`, `line`, `status`, `result`, `time` and `classname`; - each failed assertion becomes one `<failure message="...">` element inside its test case. How other tools interpret JUnit-style files in general, and where the dialects differ, belongs to the material on test result formats; for GoogleTest the point is what this writer puts where. ## The repeat trap The reserved CI scenario combines the report with `--gtest_repeat=100`. In GoogleTest 1.18 the XML printer writes its file in `OnTestIterationEnd`, opening it for writing each time. Two consequences: 1. the report on disk after the run describes **only the last iteration**; 2. failures in earlier iterations are **absent** from the file. A three-iteration run shows it: iteration 1 failed, iterations 2 and 3 passed, the process exited with status 1, and `reports/flaky_test.xml` showed `failures="0"` with `random_seed="44"`, the last iteration's seed. A pipeline that publishes the XML and trusts it alone would show a green flaky test. Ways to keep the evidence: - fail the job on the **exit status**, which is 1 if any iteration failed, and keep the console log as the record of which iteration failed; - for a per-attempt report, run the binary once per attempt from a loop, each with its own output path such as `xml:reports/attempt-$i.xml`; - use `--gtest_break_on_failure` or `--gtest_fail_fast` when stopping at the first failure is more useful than a count. ## Reading a failure A single run of the flaky subset, with no repeat, writes this test case for the failing test: ```xml <testcase name="FailsFirstTime" file="flaky_test.cpp" line="5" status="run" result="completed" time="0." classname="Flaky" ...> <failure message="flaky_test.cpp:5
Expected: (calls) != (1), actual: 1 vs 1
" type=""> <![CDATA[flaky_test.cpp:5 Expected: (calls) != (1), actual: 1 vs 1 ]]></failure> </testcase> ``` The `file` and `line` attributes on `<testcase>` point at the test's definition, `classname` carries the suite name, and the `<failure>` element repeats the assertion's location and message both in an attribute and as character data. A passing test is an empty `<testcase ... />`. Totals on `<testsuite>` and `<testsuites>` (`tests="2" failures="1"`) let a dashboard show counts without walking every case. ## Under CTest When CMake's `gtest_discover_tests` registers every test as a separate CTest test, each one is a separate process. Passing `EXTRA_ARGS --gtest_output=xml:report.xml` makes them all write one path, racing under `ctest -j`. The module's `XML_OUTPUT_DIR` option, added in CMake 3.18, gives each test its own file in that directory instead.
- Where do you find a failing test's source location in a GoogleTest XML report?On the `<testcase>` element: GoogleTest writes `file` and `line` attributes for where the test was defined, next to `name`, `classname`, `status` and `time`. The assertion that failed is described inside the nested `<failure>` element's `message` and text, which include the file and line of the assertion itself.
- How would you keep a report for every attempt when hunting a flaky test?Give each attempt its own process and its own path, for example a shell loop running `./flaky_test --gtest_filter='Flaky.*' --gtest_shuffle --gtest_output=xml:reports/attempt-$i.xml`. A single `--gtest_repeat` run cannot do it, because it rewrites one file after every iteration.
- Trusting the XML from a --gtest_repeat run to show every iteration's failures
- Expecting a directory path without a trailing separator to be treated as a directory
- Assuming a rerun overwrites reports/flaky_test.xml
- Passing one shared --gtest_output path to tests that CTest runs in parallel