18#if !defined(_CRT_SECURE_NO_WARNINGS)
19#define _CRT_SECURE_NO_WARNINGS 1
24#define MUST_UNDEF_WINSOCK
29#ifdef MUST_UNDEF_WINSOCK
31#undef MUST_UNDEF_WINSOCK
48#define UTPP_VERSION "2.2.0"
59#define DEFAULT_SUITE "DefaultSuite"
63#error Macro TEST is already defined
67#error Macro TEST_FIXTURE is already defined
71#error Macro SUITE is already defined
78#define _MSVC_LANG __cplusplus
97#if _MSVC_LANG < 201703L
98#define TEST_MAIN(ARGC, ARGV) \
99UnitTest::Test *UnitTest::CurrentTest; \
100UnitTest::Reporter *UnitTest::CurrentReporter; \
101double UnitTest::default_tolerance; \
102std::string UnitTest::CurrentSuite; \
105#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
125 namespace Suite##Name \
127 inline char const* GetSuiteName () { return #Name ; } \
129 namespace Suite##Name
138 class Test##Name : public UnitTest::Test \
141 Test##Name() : Test(#Name) {} \
143 void RunImpl() override; \
145 UnitTest::Test* Name##_maker() {return new Test##Name; } \
146 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
148 void Test##Name::RunImpl()
162#define TEST_FIXTURE(Fixture, Name) \
163 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
166 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
168 void RunImpl() override; \
170 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
171 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
172 __FILE__, __LINE__, Name##_maker); \
173 void Fixture##Name##Helper::RunImpl()
176#error Macro ABORT is already defined
185#define ABORT(value) \
188 if (UnitTest::Check(value)) \
189 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
193#error Macro ABORT_EX is already defined
202#define ABORT_EX(value, ...) \
205 if (UnitTest::Check(value)) { \
206 char message[UnitTest::MAX_MESSAGE_SIZE]; \
207 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
208 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
223#define UTPP_TIME_CONSTRAINT(tmax) \
224 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
231#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
241void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
247 Test (
const std::string& testName);
265 std::chrono::milliseconds
time;
270 Test& operator =(
Test const&) =
delete;
271 friend class TestSuite;
288 virtual ~Reporter () {};
312 virtual void Clear ();
331class ReporterDeferred :
public Reporter
334 ReporterDeferred () {};
339 void Clear ()
override;
346 TestResult (
const std::string& suite,
const std::string& test);
374 const std::string& test,
375 const std::string& file,
380 const std::string test_name,
385 friend class TestSuite;
388 explicit TestSuite (
const std::string&
name);
391 void Enable (
bool on_off);
397 std::deque <const Inserter*> test_list;
398 std::chrono::milliseconds max_runtime;
401 bool SetupCurrentTest (
const Inserter* inf);
402 void RunCurrentTest (
const Inserter* inf);
403 void TearDownCurrentTest (
const Inserter* inf);
416 std::chrono::steady_clock::time_point startTime;
423 template<
typename R,
typename P>
424 TimeConstraint (std::chrono::duration<R, P> t,
const char* file,
int line);
432 std::string filename;
434 std::chrono::milliseconds tmax;
441 int Run (
const std::string& suite,
Reporter& reporter, std::chrono::milliseconds max_time);
442 int RunAll (
Reporter& reporter, std::chrono::milliseconds max_time);
444 void Enable (
const std::string& suite,
bool enable =
true);
448 std::deque <TestSuite> suites;
452struct test_abort :
public std::runtime_error
454 test_abort (
const char* file_,
int line_,
const char* msg)
455 : std::runtime_error (msg)
463struct tolerance_not_set :
public std::runtime_error
466 : std::runtime_error (
"UnitTest::default_tolerance not set")
492int RunSuite (
const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
495void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
570 : suite_test_count (0)
571 , suite_failed_count (0)
572 , suite_failures_count (0)
574 , total_test_count (0)
575 , total_failed_count (0)
576 , total_failures_count (0)
630 using namespace std::chrono_literals;
697 results.back ().failures.push_back (failure);
721TestSuite::TestSuite (
const std::string& name_)
739 test_list.push_back (inf);
760 std::deque <const Inserter*>::iterator listp = test_list.begin ();
761 max_runtime = maxtime;
762 while (listp != test_list.end ())
765 if (SetupCurrentTest (*listp))
767 RunCurrentTest (*listp);
768 TearDownCurrentTest (*listp);
787bool TestSuite::SetupCurrentTest (
const Inserter* inf)
796 std::stringstream stream;
797 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
798 CurrentTest =
new Test (inf->test_name);
799 ReportFailure (x.file, x.line, stream.str ());
803 catch (
const std::exception& e)
805 std::stringstream stream;
806 stream <<
"Unhandled exception: " << e.what ()
807 <<
" while setting up test " << inf->test_name;
815 std::stringstream stream;
816 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
827void TestSuite::RunCurrentTest (
const Inserter* inf)
829 assert (CurrentTest);
836 catch (UnitTest::test_abort& x)
838 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
840 catch (
const std::exception& e)
842 std::stringstream stream;
843 stream <<
"Unhandled exception: " << e.what ()
844 <<
" while running test " << inf->test_name;
849 std::stringstream stream;
850 stream <<
"Unhandled exception while running test " << inf->test_name;
857 std::stringstream stream;
858 stream <<
"Global time constraint failed while running test " << inf->test_name
859 <<
" Expected time <" << max_runtime <<
"; actual = " << actual_time;
868void TestSuite::TearDownCurrentTest (
const Inserter* inf)
874 catch (
const std::exception& e)
876 std::stringstream stream;
877 stream <<
"Unhandled exception: " << e.what ()
878 <<
" while tearing down test " << inf->test_name;
883 std::stringstream stream;
884 stream <<
"Unhandled exception tearing down test " << inf->test_name;
916 const std::string& file,
int ln,
Testmaker func)
935 startTime = std::chrono::steady_clock::now ();
942 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
943 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
950 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
951 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
965template <
typename R,
typename P>
981 std::chrono::milliseconds t = timer.GetTimeInMs ();
984 std::stringstream stream;
985 stream <<
"Time constraint failed. Expected time <" << tmax <<
"; actual = " << t;
1005 auto p = suites.begin ();
1006 while (p != suites.end () && p->name != suite_name)
1009 if (p == suites.end ())
1011 suites.emplace_back (suite_name);
1012 suites.back ().Add (inf);
1030 for (
auto& s : suites)
1032 if (s.name == suite_name)
1034 s.RunTests (reporter, max_time);
1051 for (
auto& s : suites)
1054 s.RunTests (reporter, max_time);
1081 for (
auto& s : suites)
1083 if (s.name == suite)
1167void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1171 Failure f = { filename, message, line };
1200 return the_default_reporter;
1203#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1204 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:711
void TestStart(const Test &test) override
Definition utpp.h:681
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:354
void ReportFailure(const Failure &failure) override
Definition utpp.h:692
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:669
void TestFinish(const Test &test) override
Definition utpp.h:705
Abstract base for all reporters.
Definition utpp.h:285
virtual void Clear()
Reset all statistics.
Definition utpp.h:628
int suites_count
number of suites ran
Definition utpp.h:325
int suite_failures_count
number of failures in suite
Definition utpp.h:317
int total_failures_count
total number of failures
Definition utpp.h:322
int suite_test_count
number of tests in suite
Definition utpp.h:315
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:585
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:592
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:323
bool trace
true if tracing is enabled
Definition utpp.h:326
int suite_failed_count
number of failed tests in suite
Definition utpp.h:316
std::chrono::milliseconds suite_time
total suite running time in milliseconds
Definition utpp.h:318
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:604
int total_test_count
total number of tests
Definition utpp.h:320
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:599
virtual int Summary()
Generate results report.
Definition utpp.h:309
int total_failed_count
total number of failed tests
Definition utpp.h:321
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:621
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:291
A Reporter that sends results directly to an output stream.
Definition reporter_stream.h:21
A singleton object containing all test suites.
Definition utpp.h:438
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1003
int Run(const std::string &suite, Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1028
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1079
int RunAll(Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1049
static SuitesList & GetSuitesList()
Definition utpp.h:1065
Representation of a test case.
Definition utpp.h:245
std::chrono::milliseconds time
Run time.
Definition utpp.h:265
int failures
Number of failures in this test.
Definition utpp.h:264
void run()
Definition utpp.h:514
void failure()
Definition utpp.h:527
const std::string & test_name() const
Return test name.
Definition utpp.h:548
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:534
virtual void RunImpl()
Actual body of test.
Definition utpp.h:260
Test(const std::string &testName)
Constructor.
Definition utpp.h:500
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:562
std::string name
Name of this test.
Definition utpp.h:263
bool time_exempt
true if exempt from time constraints
Definition utpp.h:266
std::chrono::milliseconds test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:541
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:555
Constructor of this objects inserts the test in suite.
Definition utpp.h:371
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:915
std::string name
Suite name.
Definition utpp.h:394
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:891
void Add(const Inserter *inf)
Definition utpp.h:737
int RunTests(Reporter &reporter, std::chrono::milliseconds max_runtime)
Definition utpp.h:752
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:898
TimeConstraint(std::chrono::duration< R, P > t, const char *file, int line)
Definition utpp.h:966
~TimeConstraint()
Definition utpp.h:979
An object that can be interrogated to get elapsed time.
Definition utpp.h:408
std::chrono::microseconds GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:948
std::chrono::milliseconds GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:940
void Start()
Record starting time.
Definition utpp.h:933
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1152
int RunAllTests(Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from all test suites.
Definition utpp.h:1107
int RunSuite(const char *suite_name, Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from one suite.
Definition utpp.h:1125
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1139
Definition of UnitTest::ReporterDbgout class.
Definition of UnitTest::ReporterStream class.
Definition of UnitTest::ReporterXml class.
The failure object records the file name, the line number and a message.
Definition utpp.h:276
int line_number
Line number where the failure has occurred.
Definition utpp.h:279
std::string message
Description of failure.
Definition utpp.h:278
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:277
Test results including all failure messages
Definition utpp.h:344
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:351
std::string suite_name
suite name
Definition utpp.h:348
std::string test_name
test name
Definition utpp.h:349
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:350
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:647
Exception thrown by ABORT macro.
Definition utpp.h:453
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:358
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:59
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1167
Reporter * CurrentReporter
Pointer to current reporter object.
Test * CurrentTest
Currently executing test.
std::string CurrentSuite
Name of currently running suite.
Reporter & GetDefaultReporter()
Return the default reporter object.
Definition utpp.h:1197
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:55
const char * GetSuiteName()
Definition utpp.h:1183