UTPP
Loading...
Searching...
No Matches
reporter_stream.h
Go to the documentation of this file.
1#pragma once
2/*
3 UTPP - A New Generation of UnitTest++
4 (c) Mircea Neacsu 2017-2023
5
6 See LICENSE file for full copyright information.
7*/
8
13
14#include <iostream>
15#include <iomanip>
16
17namespace UnitTest {
18
20class ReporterStream : public Reporter
21{
22public:
23 ReporterStream (std::ostream& strm = std::cout);
24
25protected:
26 void SuiteStart (const TestSuite& suite) override;
27 void TestStart (const Test& test) override;
28 void TestFinish (const Test& test) override;
29 int SuiteFinish (const TestSuite& suite) override;
30
31 void ReportFailure (const Failure& failure) override;
32 int Summary () override;
33
34 std::ostream& out;
35};
36
42inline
44 : out (strm)
45{
46}
47
49inline
51{
53 if (!trace)
54 return;
55 out << "Begin suite: " << suite.name << std::endl;
56}
57
59inline
61{
63 if (!trace)
64 return;
65 out << "Start test: " << test.test_name () << std::endl;
66}
67
69inline
71{
72 if (trace)
73 std::cout << "End test: " << test.test_name () << std::endl;
75}
76
78inline
80{
81 if (trace)
82 out << "End suite: " << suite.name << std::endl;
83 return Reporter::SuiteFinish (suite);
84}
85
86
93inline
95{
96 out << "Failure in ";
97 if (CurrentTest)
98 {
100 out << "suite " << CurrentSuite << ' ';
101 out << "test " << CurrentTest->test_name ();
102 }
103 auto f = out.flags (std::ios::dec);
104
105#if defined(__APPLE__) || defined(__GNUG__)
106 out << std::endl << failure.filename << ":" << failure.line_number << ": error: "
107 << failure.message << std::endl;
108#else
109 out << std::endl << failure.filename << "(" << failure.line_number << "): error: "
110 << failure.message << std::endl;
111#endif
112 out.flags (f);
113 Reporter::ReportFailure (failure);
114}
115
120inline
122{
123 using namespace std::chrono;
124 auto f = out.flags (std::ios::dec | std::ios::fixed);
125 auto p = out.precision (2);
126
127 if (total_failed_count > 0)
128 {
129 out << "FAILURE: " << total_failed_count << " out of "
130 << total_test_count << " tests failed (" << total_failures_count
131 << " failures)." << std::endl;
132 }
133 else
134 out << "Success: " << total_test_count << " tests passed." << std::endl;
135
136 auto total_time_s = duration_cast<duration<float, std::chrono::seconds::period>>(total_time);
137 out << "Run time: " << total_time_s.count() << " seconds" << std::endl;
138 out.flags (f);
139 out.precision (p);
140 return Reporter::Summary ();
141}
142
148class ReporterStdout : public ReporterStream
149{
150public:
151 ReporterStdout () : ReporterStream () {};
152};
153
154} //namespace UnitTest
int total_failures_count
total number of failures
Definition utpp.h:323
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:586
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:593
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:324
bool trace
true if tracing is enabled
Definition utpp.h:327
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:605
int total_test_count
total number of tests
Definition utpp.h:321
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:600
virtual int Summary()
Generate results report.
Definition utpp.h:310
int total_failed_count
total number of failed tests
Definition utpp.h:322
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:622
void TestFinish(const Test &test) override
If tracing is enabled, show a test finish message.
Definition reporter_stream.h:70
int Summary() override
Definition reporter_stream.h:121
ReporterStream(std::ostream &strm=std::cout)
Definition reporter_stream.h:43
void ReportFailure(const Failure &failure) override
Definition reporter_stream.h:94
void TestStart(const Test &test) override
If tracing is enabled, show a test start message.
Definition reporter_stream.h:60
int SuiteFinish(const TestSuite &suite) override
If tracing is enabled, show a suite finish message.
Definition reporter_stream.h:79
void SuiteStart(const TestSuite &suite) override
If tracing is enabled, show a suite start message.
Definition reporter_stream.h:50
Representation of a test case.
Definition utpp.h:246
const std::string & test_name() const
Return test name.
Definition utpp.h:549
Definition utpp.h:368
std::string name
Suite name.
Definition utpp.h:395
The failure object records the file name, the line number and a message.
Definition utpp.h:277
int line_number
Line number where the failure has occurred.
Definition utpp.h:280
std::string message
Description of failure.
Definition utpp.h:279
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:278
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:60
Test * CurrentTest
Currently executing test.
std::string CurrentSuite
Name of currently running suite.