18#pragma warning( push )
19#pragma warning( disable : 4996 )
39#define UTPP_CPP_LANG __cplusplus
41#define UTPP_CPP_LANG _MSVC_LANG
45#if ((UTPP_CPP_LANG >= 202002L && \
46 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 160000)))
47#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 1
49#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 0
53#define UTPP_VERSION "3.0.2"
64#define DEFAULT_SUITE "DefaultSuite"
68#error Macro TEST is already defined
72#error Macro TEST_FIXTURE is already defined
76#error Macro SUITE is already defined
95#if UTPP_CPP_LANG < 201703L
96#define TEST_MAIN(ARGC, ARGV) \
97UnitTest::Test *UnitTest::CurrentTest; \
98UnitTest::Reporter *UnitTest::CurrentReporter; \
99double UnitTest::default_tolerance; \
100std::string UnitTest::CurrentSuite; \
103#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
123 namespace Suite##Name \
125 inline char const* GetSuiteName () { return #Name ; } \
127 namespace Suite##Name
136 class Test##Name : public UnitTest::Test \
139 Test##Name() : Test(#Name) {} \
141 void RunImpl() override; \
143 UnitTest::Test* Name##_maker() {return new Test##Name; } \
144 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
146 void Test##Name::RunImpl()
160#define TEST_FIXTURE(Fixture, Name) \
161 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
164 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
166 void RunImpl() override; \
168 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
169 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
170 __FILE__, __LINE__, Name##_maker); \
171 void Fixture##Name##Helper::RunImpl()
174#error Macro ABORT is already defined
183#define ABORT(value) \
186 if (UnitTest::Check(value)) \
187 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
191#error Macro ABORT_EX is already defined
200#define ABORT_EX(value, ...) \
203 if (UnitTest::Check(value)) { \
204 char message[UnitTest::MAX_MESSAGE_SIZE]; \
205 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
206 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
221#define UTPP_TIME_CONSTRAINT(tmax) \
222 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
229#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
239void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
245 Test (
const std::string& testName);
263 std::chrono::milliseconds
time;
268 Test& operator =(
Test const&) =
delete;
269 friend class TestSuite;
286 virtual ~Reporter () {};
310 virtual void Clear ();
329class ReporterDeferred :
public Reporter
332 ReporterDeferred () {};
337 void Clear ()
override;
344 TestResult (
const std::string& suite,
const std::string& test);
372 const std::string& test,
373 const std::string& file,
378 const std::string test_name,
383 friend class TestSuite;
386 explicit TestSuite (
const std::string&
name);
389 void Enable (
bool on_off);
395 std::deque <const Inserter*> test_list;
396 std::chrono::milliseconds max_runtime;
399 bool SetupCurrentTest (
const Inserter* inf);
400 void RunCurrentTest (
const Inserter* inf);
401 void TearDownCurrentTest (
const Inserter* inf);
414 std::chrono::steady_clock::time_point startTime;
421 template<
typename R,
typename P>
422 TimeConstraint (std::chrono::duration<R, P> t,
const char* file,
int line);
430 std::string filename;
432 std::chrono::milliseconds tmax;
439 int Run (
const std::string& suite,
Reporter& reporter, std::chrono::milliseconds max_time);
440 int RunAll (
Reporter& reporter, std::chrono::milliseconds max_time);
442 void Enable (
const std::string& suite,
bool enable =
true);
446 std::deque <TestSuite> suites;
450struct test_abort :
public std::runtime_error
452 test_abort (
const char* file_,
int line_,
const char* msg)
453 : std::runtime_error (msg)
461struct tolerance_not_set :
public std::runtime_error
464 : std::runtime_error (
"UnitTest::default_tolerance not set")
490int RunSuite (
const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
493void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
568 : suite_test_count (0)
569 , suite_failed_count (0)
570 , suite_failures_count (0)
572 , total_test_count (0)
573 , total_failed_count (0)
574 , total_failures_count (0)
628 using namespace std::chrono_literals;
695 results.back ().failures.push_back (failure);
719TestSuite::TestSuite (
const std::string& name_)
737 test_list.push_back (inf);
758 std::deque <const Inserter*>::iterator listp = test_list.begin ();
759 max_runtime = maxtime;
760 while (listp != test_list.end ())
763 if (SetupCurrentTest (*listp))
765 RunCurrentTest (*listp);
766 TearDownCurrentTest (*listp);
785bool TestSuite::SetupCurrentTest (
const Inserter* inf)
794 std::stringstream stream;
795 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
796 CurrentTest =
new Test (inf->test_name);
797 ReportFailure (x.file, x.line, stream.str ());
801 catch (
const std::exception& e)
803 std::stringstream stream;
804 stream <<
"Unhandled exception: " << e.what ()
805 <<
" while setting up test " << inf->test_name;
813 std::stringstream stream;
814 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
825void TestSuite::RunCurrentTest (
const Inserter* inf)
827 assert (CurrentTest);
834 catch (UnitTest::test_abort& x)
836 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
838 catch (
const std::exception& e)
840 std::stringstream stream;
841 stream <<
"Unhandled exception: " << e.what ()
842 <<
" while running test " << inf->test_name;
847 std::stringstream stream;
848 stream <<
"Unhandled exception while running test " << inf->test_name;
855 std::stringstream stream;
856 stream <<
"Global time constraint failed while running test " << inf->test_name
857 <<
" Expected time <"
858#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
861 << max_runtime.count () <<
"ms"
864#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
867 << actual_time.count ();
876void TestSuite::TearDownCurrentTest (
const Inserter* inf)
882 catch (
const std::exception& e)
884 std::stringstream stream;
885 stream <<
"Unhandled exception: " << e.what ()
886 <<
" while tearing down test " << inf->test_name;
891 std::stringstream stream;
892 stream <<
"Unhandled exception tearing down test " << inf->test_name;
924 const std::string& file,
int ln,
Testmaker func)
943 startTime = std::chrono::steady_clock::now ();
950 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
951 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
958 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
959 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
973template <
typename R,
typename P>
989 std::chrono::milliseconds t = timer.GetTimeInMs ();
992 std::stringstream stream;
993 stream <<
"Time constraint failed. Expected time <"
994#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
997 << tmax.count () <<
"ms"
1000#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1003 << t.count () <<
"ms";
1023 auto p = suites.begin ();
1024 while (p != suites.end () && p->name != suite_name)
1027 if (p == suites.end ())
1029 suites.emplace_back (suite_name);
1030 suites.back ().Add (inf);
1048 for (
auto& s : suites)
1050 if (s.name == suite_name)
1052 s.RunTests (reporter, max_time);
1069 for (
auto& s : suites)
1072 s.RunTests (reporter, max_time);
1099 for (
auto& s : suites)
1101 if (s.name == suite)
1185void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1189 Failure f = { filename, message, line };
1218 return the_default_reporter;
1221#if UTPP_CPP_LANG >= 201703L
1229#pragma warning ( pop )
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:709
void TestStart(const Test &test) override
Definition utpp.h:679
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:352
void ReportFailure(const Failure &failure) override
Definition utpp.h:690
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:667
void TestFinish(const Test &test) override
Definition utpp.h:703
Abstract base for all reporters.
Definition utpp.h:283
virtual void Clear()
Reset all statistics.
Definition utpp.h:626
int suites_count
number of suites ran
Definition utpp.h:323
int suite_failures_count
number of failures in suite
Definition utpp.h:315
int total_failures_count
total number of failures
Definition utpp.h:320
int suite_test_count
number of tests in suite
Definition utpp.h:313
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:583
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:590
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:321
bool trace
true if tracing is enabled
Definition utpp.h:324
int suite_failed_count
number of failed tests in suite
Definition utpp.h:314
std::chrono::milliseconds suite_time
total suite running time in milliseconds
Definition utpp.h:316
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:602
int total_test_count
total number of tests
Definition utpp.h:318
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:597
virtual int Summary()
Generate results report.
Definition utpp.h:307
int total_failed_count
total number of failed tests
Definition utpp.h:319
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:619
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:289
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:436
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1021
int Run(const std::string &suite, Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1046
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1097
int RunAll(Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1067
static SuitesList & GetSuitesList()
Definition utpp.h:1083
Representation of a test case.
Definition utpp.h:243
std::chrono::milliseconds time
Run time.
Definition utpp.h:263
int failures
Number of failures in this test.
Definition utpp.h:262
void run()
Definition utpp.h:512
void failure()
Definition utpp.h:525
const std::string & test_name() const
Return test name.
Definition utpp.h:546
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:532
virtual void RunImpl()
Actual body of test.
Definition utpp.h:258
Test(const std::string &testName)
Constructor.
Definition utpp.h:498
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:560
std::string name
Name of this test.
Definition utpp.h:261
bool time_exempt
true if exempt from time constraints
Definition utpp.h:264
std::chrono::milliseconds test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:539
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:553
Constructor of this objects inserts the test in suite.
Definition utpp.h:369
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:923
std::string name
Suite name.
Definition utpp.h:392
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:899
void Add(const Inserter *inf)
Definition utpp.h:735
int RunTests(Reporter &reporter, std::chrono::milliseconds max_runtime)
Definition utpp.h:750
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:906
TimeConstraint(std::chrono::duration< R, P > t, const char *file, int line)
Definition utpp.h:974
~TimeConstraint()
Definition utpp.h:987
An object that can be interrogated to get elapsed time.
Definition utpp.h:406
std::chrono::microseconds GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:956
std::chrono::milliseconds GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:948
void Start()
Record starting time.
Definition utpp.h:941
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1170
int RunAllTests(Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from all test suites.
Definition utpp.h:1125
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:1143
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1157
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:274
int line_number
Line number where the failure has occurred.
Definition utpp.h:277
std::string message
Description of failure.
Definition utpp.h:276
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:275
Test results including all failure messages
Definition utpp.h:342
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:349
std::string suite_name
suite name
Definition utpp.h:346
std::string test_name
test name
Definition utpp.h:347
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:348
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:645
Exception thrown by ABORT macro.
Definition utpp.h:451
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:356
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:64
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1185
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:1215
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:60
const char * GetSuiteName()
Definition utpp.h:1201