reporters.md (4823B)
1 ## Reporters 2 3 Doctest 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. 4 5 You can list all registered reporters/listeners with ```--list-reporters```. There are a few implemented reporters in the framework: 6 - ```console``` - streaming - writes normal lines of text with coloring if a capable terminal is detected 7 - ```xml``` - streaming - writes in xml format tailored to doctest 8 - ```junit``` - buffering - writes in JUnit-compatible xml - for more information look [here](https://github.com/doctest/doctest/issues/318) and [here](https://github.com/doctest/doctest/issues/376). 9 10 Streaming means that results are delivered progressively and not at the end of the test run. 11 12 The output is by default written to ```stdout``` but can be redirected with the use of the ```--out=<filename>``` [**command line option**](commandline.md). 13 14 Example how to define your own reporter: 15 16 ```c++ 17 #include <doctest/doctest.h> 18 19 #include <mutex> 20 21 using namespace doctest; 22 23 struct MyXmlReporter : public IReporter 24 { 25 // caching pointers/references to objects of these types - safe to do 26 std::ostream& stdout_stream; 27 const ContextOptions& opt; 28 const TestCaseData* tc; 29 std::mutex mutex; 30 31 // constructor has to accept the ContextOptions by ref as a single argument 32 MyXmlReporter(const ContextOptions& in) 33 : stdout_stream(*in.cout) 34 , opt(in) {} 35 36 void report_query(const QueryData& /*in*/) override {} 37 38 void test_run_start() override {} 39 40 void test_run_end(const TestRunStats& /*in*/) override {} 41 42 void test_case_start(const TestCaseData& in) override { tc = ∈ } 43 44 // called when a test case is reentered because of unfinished subcases 45 void test_case_reenter(const TestCaseData& /*in*/) override {} 46 47 void test_case_end(const CurrentTestCaseStats& /*in*/) override {} 48 49 void test_case_exception(const TestCaseException& /*in*/) override {} 50 51 void subcase_start(const SubcaseSignature& /*in*/) override { 52 std::lock_guard<std::mutex> lock(mutex); 53 } 54 55 void subcase_end() override { 56 std::lock_guard<std::mutex> lock(mutex); 57 } 58 59 void log_assert(const AssertData& in) override { 60 // don't include successful asserts by default - this is done here 61 // instead of in the framework itself because doctest doesn't know 62 // if/when a reporter/listener cares about successful results 63 if(!in.m_failed && !opt.success) 64 return; 65 66 // make sure there are no races - this is done here instead of in the 67 // framework itself because doctest doesn't know if reporters/listeners 68 // care about successful asserts and thus doesn't lock a mutex unnecessarily 69 std::lock_guard<std::mutex> lock(mutex); 70 71 // ... 72 } 73 74 void log_message(const MessageData& /*in*/) override { 75 // messages too can be used in a multi-threaded context - like asserts 76 std::lock_guard<std::mutex> lock(mutex); 77 78 // ... 79 } 80 81 void test_case_skipped(const TestCaseData& /*in*/) override {} 82 }; 83 84 // "1" is the priority - used for ordering when multiple reporters are used 85 REGISTER_REPORTER("my_xml", 1, MyXmlReporter); 86 87 // registering the same class as a reporter and as a listener is nonsense but it's possible 88 REGISTER_LISTENER("my_listener", 1, MyXmlReporter); 89 ``` 90 91 Custom `IReporter` implementations must be registered with one of: 92 93 * `REGISTER_REPORTER`, for when the new reporter is an option that users may choose at run-time. 94 * `REGISTER_LISTENER`, for when the reporter is actually a listener and must always be executed, regardless of which reporters have been chosen at run-time. 95 96 Multiple reporters can be used at the same time - just specify them through the ```--reporters=...``` [**command line filtering option**](commandline.md) using commas to separate them like this: ```--reporters=myReporter,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). 97 98 All registered listeners (```REGISTER_LISTENER```) will be executed before any reporter - they do not need to be specified and cannot be filtered through the command line. 99 100 When 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). 101 102 --------------- 103 104 [Home](readme.md#reference) 105 106 <p align="center"><img src="../../scripts/data/logo/icon_2.svg"></p>