| 1 | <a id="top"></a> |
| 2 | # Reporters |
| 3 | |
| 4 | Reporters are a customization point for most of Catch2's output, e.g. |
| 5 | formatting and writing out [assertions (whether passing or failing), |
| 6 | sections, test cases, benchmarks, and so on](reporter-events.md#top). |
| 7 | |
| 8 | Catch2 comes with a bunch of reporters by default (currently 8), and |
| 9 | you can also write your own reporter. Because multiple reporters can |
| 10 | be active at the same time, your own reporters do not even have to handle |
| 11 | all reporter event, just the ones you are interested in, e.g. benchmarks. |
| 12 | |
| 13 | |
| 14 | ## Using different reporters |
| 15 | |
| 16 | You can see which reporters are available by running the test binary |
| 17 | with `--list-reporters`. You can then pick one of them with the [`-r`, |
| 18 | `--reporter` option](command-line.md#choosing-a-reporter-to-use), followed |
| 19 | by the name of the desired reporter, like so: |
| 20 | |
| 21 | ``` |
| 22 | --reporter xml |
| 23 | ``` |
| 24 | |
| 25 | You can also select multiple reporters to be used at the same time. |
| 26 | In that case you should read the [section on using multiple |
| 27 | reporters](#multiple-reporters) to avoid any surprises from doing so. |
| 28 | |
| 29 | |
| 30 | <a id="multiple-reporters"></a> |
| 31 | ## Using multiple reporters |
| 32 | |
| 33 | > Support for having multiple parallel reporters was [introduced](https://github.com/catchorg/Catch2/pull/2183) in Catch2 3.0.1 |
| 34 | |
| 35 | Catch2 supports using multiple reporters at the same time while having |
| 36 | them write into different destinations. The two main uses of this are |
| 37 | |
| 38 | * having both human-friendly and machine-parseable (e.g. in JUnit format) |
| 39 | output from one run of binary |
| 40 | * having "partial" reporters that are highly specialized, e.g. having one |
| 41 | reporter that writes out benchmark results as markdown tables and does |
| 42 | nothing else, while also having standard testing output separately |
| 43 | |
| 44 | Specifying multiple reporter looks like this: |
| 45 | ``` |
| 46 | --reporter JUnit::out=result-junit.xml --reporter console::out=-::colour-mode=ansi |
| 47 | ``` |
| 48 | |
| 49 | This tells Catch2 to use two reporters, `JUnit` reporter that writes |
| 50 | its machine-readable XML output to file `result-junit.xml`, and the |
| 51 | `console` reporter that writes its user-friendly output to stdout and |
| 52 | uses ANSI colour codes for colouring the output. |
| 53 | |
| 54 | Using multiple reporters (or one reporter and one-or-more [event |
| 55 | listeners](event-listeners.md#top)) can have surprisingly complex semantics |
| 56 | when using customization points provided to reporters by Catch2, namely |
| 57 | capturing stdout/stderr from test cases. |
| 58 | |
| 59 | As long as at least one reporter (or listener) asks Catch2 to capture |
| 60 | stdout/stderr, captured stdout and stderr will be available to all |
| 61 | reporters and listeners. |
| 62 | |
| 63 | Because this might be surprising to the users, if at least one active |
| 64 | _reporter_ is non-capturing, then Catch2 tries to roughly emulate |
| 65 | non-capturing behaviour by printing out the captured stdout/stderr |
| 66 | just before `testCasePartialEnded` event is sent out to the active |
| 67 | reporters and listeners. This means that stdout/stderr is no longer |
| 68 | printed out from tests as it is being written, but instead it is written |
| 69 | out in batch after each runthrough of a test case is finished. |
| 70 | |
| 71 | |
| 72 | |
| 73 | ## Writing your own reporter |
| 74 | |
| 75 | You can also write your own custom reporter and tell Catch2 to use it. |
| 76 | When writing your reporter, you have two options: |
| 77 | |
| 78 | * Derive from `Catch::ReporterBase`. When doing this, you will have |
| 79 | to provide handling for all [reporter events](reporter-events.md#top). |
| 80 | * Derive from one of the provided [utility reporter bases in |
| 81 | Catch2](#utility-reporter-bases). |
| 82 | |
| 83 | Generally we recommend doing the latter, as it is less work. |
| 84 | |
| 85 | Apart from overriding handling of the individual reporter events, reporters |
| 86 | have access to some extra customization points, described below. |
| 87 | |
| 88 | |
| 89 | ### Utility reporter bases |
| 90 | |
| 91 | Catch2 currently provides two utility reporter bases: |
| 92 | |
| 93 | * `Catch::StreamingReporterBase` |
| 94 | * `Catch::CumulativeReporterBase` |
| 95 | |
| 96 | `StreamingReporterBase` is useful for reporters that can format and write |
| 97 | out the events as they come in. It provides (usually empty) implementation |
| 98 | for all reporter events, and if you let it handle the relevant events, |
| 99 | it also handles storing information about active test run and test case. |
| 100 | |
| 101 | `CumulativeReporterBase` is a base for reporters that need to see the whole |
| 102 | test run, before they can start writing the output, such as the JUnit |
| 103 | and SonarQube reporters. This post-facto approach requires the assertions |
| 104 | to be stringified when it is finished, so that the assertion can be written |
| 105 | out later. Because the stringification can be expensive, and not all |
| 106 | cumulative reporters need the assertions, this base provides customization |
| 107 | point to change whether the assertions are saved or not, separate for |
| 108 | passing and failing assertions. |
| 109 | |
| 110 | |
| 111 | _Generally we recommend that if you override a member function from either |
| 112 | of the bases, you call into the base's implementation first. This is not |
| 113 | necessarily in all cases, but it is safer and easier._ |
| 114 | |
| 115 | |
| 116 | Writing your own reporter then looks like this: |
| 117 | |
| 118 | ```cpp |
| 119 | #include <catch2/reporters/catch_reporter_streaming_base.hpp> |
| 120 | #include <catch2/catch_test_case_info.hpp> |
| 121 | #include <catch2/reporters/catch_reporter_registrars.hpp> |
| 122 | |
| 123 | #include <iostream> |
| 124 | |
| 125 | class PartialReporter : public Catch::StreamingReporterBase { |
| 126 | public: |
| 127 | using StreamingReporterBase::StreamingReporterBase; |
| 128 | |
| 129 | static std::string getDescription() { |
| 130 | return "Reporter for testing TestCasePartialStarting/Ended events"; |
| 131 | } |
| 132 | |
| 133 | void testCasePartialStarting(Catch::TestCaseInfo const& testInfo, |
| 134 | uint64_t partNumber) override { |
| 135 | std::cout << "TestCaseStartingPartial: " << testInfo.name << '#' << partNumber << '\n'; |
| 136 | } |
| 137 | |
| 138 | void testCasePartialEnded(Catch::TestCaseStats const& testCaseStats, |
| 139 | uint64_t partNumber) override { |
| 140 | std::cout << "TestCasePartialEnded: " << testCaseStats.testInfo->name << '#' << partNumber << '\n'; |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | CATCH_REGISTER_REPORTER("partial", PartialReporter) |
| 146 | ``` |
| 147 | |
| 148 | This create a simple reporter that responds to `testCasePartial*` events, |
| 149 | and calls itself "partial" reporter, so it can be invoked with |
| 150 | `--reporter partial` command line flag. |
| 151 | |
| 152 | |
| 153 | ### `ReporterPreferences` |
| 154 | |
| 155 | Each reporter instance contains instance of `ReporterPreferences`, a type |
| 156 | that holds flags for the behaviour of Catch2 when this reporter run. |
| 157 | Currently there are two customization options: |
| 158 | |
| 159 | * `shouldRedirectStdOut` - whether the reporter wants to handle |
| 160 | writes to stdout/stderr from user code, or not. This is useful for |
| 161 | reporters that output machine-parseable output, e.g. the JUnit |
| 162 | reporter, or the XML reporter. |
| 163 | * `shouldReportAllAssertions` - whether the reporter wants to handle |
| 164 | `assertionEnded` events for passing assertions as well as failing |
| 165 | assertions. Usually reporters do not report successful assertions |
| 166 | and don't need them for their output, but sometimes the desired output |
| 167 | format includes passing assertions even without the `-s` flag. |
| 168 | |
| 169 | |
| 170 | ### Per-reporter configuration |
| 171 | |
| 172 | > Per-reporter configuration was introduced in Catch2 3.0.1 |
| 173 | |
| 174 | Catch2 supports some configuration to happen per reporter. The configuration |
| 175 | options fall into one of two categories: |
| 176 | |
| 177 | * Catch2-recognized options |
| 178 | * Reporter-specific options |
| 179 | |
| 180 | The former is a small set of universal options that Catch2 handles for |
| 181 | the reporters, e.g. output file or console colour mode. The latter are |
| 182 | options that the reporters have to handle themselves, but the keys and |
| 183 | values can be arbitrary strings, as long as they don't contain `::`. This |
| 184 | allows writing reporters that can be significantly customized at runtime. |
| 185 | |
| 186 | Reporter-specific options always have to be prefixed with "X" (large |
| 187 | letter X). |
| 188 | |
| 189 | |
| 190 | ### Other expected functionality of a reporter |
| 191 | |
| 192 | When writing a custom reporter, there are few more things that you should |
| 193 | keep in mind. These are not important for correctness, but they are |
| 194 | important for the reporter to work _nicely_. |
| 195 | |
| 196 | * Catch2 provides a simple verbosity option for users. There are three |
| 197 | verbosity levels, "quiet", "normal", and "high", and if it makes sense |
| 198 | for reporter's output format, it should respond to these by changing |
| 199 | what, and how much, it writes out. |
| 200 | |
| 201 | * Catch2 operates with an rng-seed. Knowing what seed a test run had |
| 202 | is important if you want to replicate it, so your reporter should |
| 203 | report the rng-seed, if at all possible given the target output format. |
| 204 | |
| 205 | * Catch2 also operates with test filters, or test specs. If a filter |
| 206 | is present, you should also report the filter, if at all possible given |
| 207 | the target output format. |
| 208 | |
| 209 | |
| 210 | |
| 211 | --- |
| 212 | |
| 213 | [Home](Readme.md#top) |