blob: 99c6262cd24520f371b6386d6138be4856f6b48f [file] [log] [blame]
onqtamcb6a29f2018-08-23 16:24:06 +03001<!DOCTYPE html>
2<html>
3<title>reporters</title>
4<xmp theme="united" style="display:none;">
5
6## Reporters
7
onqtam92dce5b2019-03-23 14:24:47 +02008Doctest has a modular reporter/listener system with which users can write their own reporters and register them. The reporter interface can also be used for "listening" to events.
9
10You can list all registered reporters with ```--list-reporters```. There are a few implemented reporters in the framework:
11- ```console``` - streaming - writes normal lines of text with coloring if a capable terminal is detected
12- ```xml``` - streaming - writes in xml format tailored to doctest
13
14Streaming means that results are delivered progressively and not at the end of the test run.
15
16The output is by default written to ```stdout``` but can be redirected with the use of the ```--out=<filename>``` [**command line option**](commandline.html).
onqtam9639cee2018-10-24 17:46:32 +030017
18Example how to define your own reporter:
19
20```
onqtam92dce5b2019-03-23 14:24:47 +020021#include <doctest/doctest.h>
22
23#include <mutex>
24
onqtam9639cee2018-10-24 17:46:32 +030025using namespace doctest;
26
onqtam92dce5b2019-03-23 14:24:47 +020027struct MyXmlReporter : public IReporter
onqtam9639cee2018-10-24 17:46:32 +030028{
onqtam92dce5b2019-03-23 14:24:47 +020029 // caching pointers/references to objects of these types - safe to do
30 std::ostream& stdout_stream;
31 const ContextOptions& opt;
onqtam9639cee2018-10-24 17:46:32 +030032 const TestCaseData* tc;
onqtam92dce5b2019-03-23 14:24:47 +020033 std::mutex mutex;
onqtam9639cee2018-10-24 17:46:32 +030034
onqtam92dce5b2019-03-23 14:24:47 +020035 // constructor has to accept the ContextOptions by ref as a single argument
36 MyXmlReporter(const ContextOptions& in)
37 : stdout_stream(*in.cout)
38 , opt(in) {}
onqtam9639cee2018-10-24 17:46:32 +030039
onqtam92dce5b2019-03-23 14:24:47 +020040 void report_query(const QueryData& /*in*/) override {}
onqtam9639cee2018-10-24 17:46:32 +030041
onqtam92dce5b2019-03-23 14:24:47 +020042 void test_run_start() override {}
43
44 void test_run_end(const TestRunStats& /*in*/) override {}
onqtam9639cee2018-10-24 17:46:32 +030045
46 void test_case_start(const TestCaseData& in) override { tc = &in; }
47
onqtam92dce5b2019-03-23 14:24:47 +020048 void test_case_end(const CurrentTestCaseStats& /*in*/) override {}
onqtam9639cee2018-10-24 17:46:32 +030049
onqtam92dce5b2019-03-23 14:24:47 +020050 void test_case_exception(const TestCaseException& /*in*/) override {}
onqtam9639cee2018-10-24 17:46:32 +030051
onqtam92dce5b2019-03-23 14:24:47 +020052 void subcase_start(const SubcaseSignature& /*in*/) override {}
onqtam9639cee2018-10-24 17:46:32 +030053
onqtam92dce5b2019-03-23 14:24:47 +020054 void subcase_end() override {}
onqtam9639cee2018-10-24 17:46:32 +030055
onqtam92dce5b2019-03-23 14:24:47 +020056 void log_assert(const AssertData& in) override {
57 // don't include successful asserts by default - this is done here
58 // instead of in the framework itself because doctest doesn't know
59 // if/when a reporter/listener cares about successful results
60 if(!in.m_failed && !opt.success)
61 return;
62
63 // make sure there are no races - this is done here instead of in the
64 // framework itself because doctest doesn't know if reporters/listeners
65 // care about successful asserts and thus doesn't lock a mutex unnecessarily
66 std::lock_guard<std::mutex> lock(mutex);
67
68 // ...
69 }
70
71 void log_message(const MessageData& /*in*/) override {
72 // messages too can be used in a multi-threaded context - like asserts
73 std::lock_guard<std::mutex> lock(mutex);
74
75 // ...
76 }
onqtam9639cee2018-10-24 17:46:32 +030077
78 void test_case_skipped(const TestCaseData& /*in*/) override {}
79};
80
onqtam92dce5b2019-03-23 14:24:47 +020081// "1" is the priority - used for ordering when multiple reporters/listeners are used
82REGISTER_REPORTER("my_xml", 1, MyXmlReporter);
onqtam9639cee2018-10-24 17:46:32 +030083```
84
onqtam92dce5b2019-03-23 14:24:47 +020085Multiple reporters can be used at the same time - just specify them through the ```--reporters=...``` [**command line filtering option**](commandline.html) using commas to separate them like this: ```--reporters=myListener,xml``` and their order of execution will be based on their priority - that is the number "1" in the case of the example reporter above (lower means earlier - the default console/xml reporters from the framework have 0 as their priority and negative numbers are accepted as well).
onqtam9639cee2018-10-24 17:46:32 +030086
onqtam92dce5b2019-03-23 14:24:47 +020087When implementing a reporter users are advised to follow the comments from the example above and look at the few implemented reporters in the framework itself. Also check out the [**example**](../../examples/all_features/reporters_and_listeners.cpp).
onqtamcb6a29f2018-08-23 16:24:06 +030088
89---------------
90
91[Home](readme.html#reference)
92
93<p align="center"><img src="../../scripts/data/logo/icon_2.svg"></p>
94
95
96</xmp>
97<script src="strapdown.js/strapdown.js"></script>
98</html>