18#pragma warning( push )
19#pragma warning( disable : 4996 )
51#define UTPP_CPP_LANG __cplusplus
53#define UTPP_CPP_LANG _MSVC_LANG
61#if ((UTPP_CPP_LANG >= 202002L && \
62 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 160000)))
63#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 1
65#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 0
71#define UTPP_VERSION "3.0.3"
83#define DEFAULT_SUITE "DefaultSuite"
87#error Macro TEST is already defined
91#error Macro TEST_FIXTURE is already defined
95#error Macro SUITE is already defined
114#if UTPP_CPP_LANG < 201703L
115#define TEST_MAIN(ARGC, ARGV) \
116UnitTest::Test *UnitTest::CurrentTest; \
117UnitTest::Reporter *UnitTest::CurrentReporter; \
118double UnitTest::default_tolerance; \
119std::string UnitTest::CurrentSuite; \
122#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
142 namespace Suite##Name \
144 inline char const* GetSuiteName () { return #Name ; } \
146 namespace Suite##Name
155 class Test##Name : public UnitTest::Test \
158 Test##Name() : Test(#Name) {} \
160 void RunImpl() override; \
162 UnitTest::Test* Name##_maker() {return new Test##Name; } \
163 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
165 void Test##Name::RunImpl()
179#define TEST_FIXTURE(Fixture, Name) \
180 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
183 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
185 void RunImpl() override; \
187 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
188 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
189 __FILE__, __LINE__, Name##_maker); \
190 void Fixture##Name##Helper::RunImpl()
193#error Macro ABORT is already defined
202#define ABORT(value) \
205 if (UnitTest::Check(value)) \
206 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
210#error Macro ABORT_EX is already defined
219#define ABORT_EX(value, ...) \
222 if (UnitTest::Check(value)) { \
223 char message[UnitTest::MAX_MESSAGE_SIZE]; \
224 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
225 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
240#define UTPP_TIME_CONSTRAINT(tmax) \
241 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
248#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
258void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
264 Test (
const std::string& testName);
282 std::chrono::milliseconds
time;
287 Test& operator =(
Test const&) =
delete;
288 friend class TestSuite;
305 virtual ~Reporter () {};
329 virtual void Clear ();
348class ReporterDeferred :
public Reporter
351 ReporterDeferred () {};
356 void Clear ()
override;
363 TestResult (
const std::string& suite,
const std::string& test);
391 const std::string& test,
392 const std::string& file,
397 const std::string test_name,
402 friend class TestSuite;
405 explicit TestSuite (
const std::string&
name);
408 void Enable (
bool on_off);
414 std::deque <const Inserter*> test_list;
415 std::chrono::milliseconds max_runtime;
418 bool SetupCurrentTest (
const Inserter* inf);
419 void RunCurrentTest (
const Inserter* inf);
420 void TearDownCurrentTest (
const Inserter* inf);
433 std::chrono::steady_clock::time_point startTime;
440 template<
typename R,
typename P>
441 TimeConstraint (std::chrono::duration<R, P> t,
const char* file,
int line);
449 std::string filename;
451 std::chrono::milliseconds tmax;
458 int Run (
const std::string& suite,
Reporter& reporter, std::chrono::milliseconds max_time);
459 int RunAll (
Reporter& reporter, std::chrono::milliseconds max_time);
461 void Enable (
const std::string& suite,
bool enable =
true);
465 std::deque <TestSuite> suites;
469struct test_abort :
public std::runtime_error
471 test_abort (
const char* file_,
int line_,
const char* msg)
472 : std::runtime_error (msg)
480struct tolerance_not_set :
public std::runtime_error
483 : std::runtime_error (
"UnitTest::default_tolerance not set")
510int RunSuite (
const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
513void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
588 : suite_test_count (0)
589 , suite_failed_count (0)
590 , suite_failures_count (0)
592 , total_test_count (0)
593 , total_failed_count (0)
594 , total_failures_count (0)
648 using namespace std::chrono_literals;
715 results.back ().failures.push_back (failure);
739TestSuite::TestSuite (
const std::string& name_)
757 test_list.push_back (inf);
778 std::deque <const Inserter*>::iterator listp = test_list.begin ();
779 max_runtime = maxtime;
780 while (listp != test_list.end ())
783 if (SetupCurrentTest (*listp))
785 RunCurrentTest (*listp);
786 TearDownCurrentTest (*listp);
805bool TestSuite::SetupCurrentTest (
const Inserter* inf)
814 std::stringstream stream;
815 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
816 CurrentTest =
new Test (inf->test_name);
817 ReportFailure (x.file, x.line, stream.str ());
821 catch (
const std::exception& e)
823 std::stringstream stream;
824 stream <<
"Unhandled exception: " << e.what ()
825 <<
" while setting up test " << inf->test_name;
833 std::stringstream stream;
834 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
845void TestSuite::RunCurrentTest (
const Inserter* inf)
847 assert (CurrentTest);
854 catch (UnitTest::test_abort& x)
856 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
858 catch (
const std::exception& e)
860 std::stringstream stream;
861 stream <<
"Unhandled exception: " << e.what ()
862 <<
" while running test " << inf->test_name;
867 std::stringstream stream;
868 stream <<
"Unhandled exception while running test " << inf->test_name;
875 std::stringstream stream;
876 stream <<
"Global time constraint failed while running test " << inf->test_name
877 <<
" Expected time <"
878#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
881 << max_runtime.count () <<
"ms"
884#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
887 << actual_time.count ();
896void TestSuite::TearDownCurrentTest (
const Inserter* inf)
902 catch (
const std::exception& e)
904 std::stringstream stream;
905 stream <<
"Unhandled exception: " << e.what ()
906 <<
" while tearing down test " << inf->test_name;
911 std::stringstream stream;
912 stream <<
"Unhandled exception tearing down test " << inf->test_name;
944 const std::string& file,
int ln,
Testmaker func)
963 startTime = std::chrono::steady_clock::now ();
970 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
971 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
978 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
979 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
993template <
typename R,
typename P>
1009 std::chrono::milliseconds t = timer.GetTimeInMs ();
1012 std::stringstream stream;
1013 stream <<
"Time constraint failed. Expected time <"
1014#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1017 << tmax.count () <<
"ms"
1020#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1023 << t.count () <<
"ms";
1043 auto p = suites.begin ();
1044 while (p != suites.end () && p->name != suite_name)
1047 if (p == suites.end ())
1049 suites.emplace_back (suite_name);
1050 suites.back ().Add (inf);
1068 for (
auto& s : suites)
1070 if (s.name == suite_name)
1072 s.RunTests (reporter, max_time);
1089 for (
auto& s : suites)
1092 s.RunTests (reporter, max_time);
1119 for (
auto& s : suites)
1121 if (s.name == suite)
1205void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1209 Failure f = { filename, message, line };
1238 return the_default_reporter;
1241#if UTPP_CPP_LANG >= 201703L
1249#pragma warning ( pop )
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:729
void TestStart(const Test &test) override
Definition utpp.h:699
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:371
void ReportFailure(const Failure &failure) override
Definition utpp.h:710
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:687
void TestFinish(const Test &test) override
Definition utpp.h:723
Abstract base for all reporters.
Definition utpp.h:302
virtual void Clear()
Reset all statistics.
Definition utpp.h:646
int suites_count
number of suites ran
Definition utpp.h:342
int suite_failures_count
number of failures in suite
Definition utpp.h:334
int total_failures_count
total number of failures
Definition utpp.h:339
int suite_test_count
number of tests in suite
Definition utpp.h:332
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:603
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:610
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:340
bool trace
true if tracing is enabled
Definition utpp.h:343
int suite_failed_count
number of failed tests in suite
Definition utpp.h:333
std::chrono::milliseconds suite_time
total suite running time in milliseconds
Definition utpp.h:335
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:622
int total_test_count
total number of tests
Definition utpp.h:337
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:617
virtual int Summary()
Generate results report.
Definition utpp.h:326
int total_failed_count
total number of failed tests
Definition utpp.h:338
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:639
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:308
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:455
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1041
int Run(const std::string &suite, Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1066
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1117
int RunAll(Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1087
static SuitesList & GetSuitesList()
Definition utpp.h:1103
Representation of a test case.
Definition utpp.h:262
std::chrono::milliseconds time
Run time.
Definition utpp.h:282
int failures
Number of failures in this test.
Definition utpp.h:281
void run()
Definition utpp.h:532
void failure()
Definition utpp.h:545
const std::string & test_name() const
Return test name.
Definition utpp.h:566
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:552
virtual void RunImpl()
Actual body of test.
Definition utpp.h:277
Test(const std::string &testName)
Constructor.
Definition utpp.h:518
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:580
std::string name
Name of this test.
Definition utpp.h:280
bool time_exempt
true if exempt from time constraints
Definition utpp.h:283
std::chrono::milliseconds test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:559
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:573
Constructor of this objects inserts the test in suite.
Definition utpp.h:388
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:943
std::string name
Suite name.
Definition utpp.h:411
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:919
void Add(const Inserter *inf)
Definition utpp.h:755
int RunTests(Reporter &reporter, std::chrono::milliseconds max_runtime)
Definition utpp.h:770
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:926
TimeConstraint(std::chrono::duration< R, P > t, const char *file, int line)
Definition utpp.h:994
~TimeConstraint()
Definition utpp.h:1007
An object that can be interrogated to get elapsed time.
Definition utpp.h:425
std::chrono::microseconds GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:976
std::chrono::milliseconds GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:968
void Start()
Record starting time.
Definition utpp.h:961
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1190
int RunAllTests(Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from all test suites.
Definition utpp.h:1145
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:1163
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1177
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:293
int line_number
Line number where the failure has occurred.
Definition utpp.h:296
std::string message
Description of failure.
Definition utpp.h:295
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:294
Test results including all failure messages
Definition utpp.h:361
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:368
std::string suite_name
suite name
Definition utpp.h:365
std::string test_name
test name
Definition utpp.h:366
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:367
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:665
Exception thrown by ABORT macro.
Definition utpp.h:470
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:83
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1205
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:1235
UnitTest::Test *(*) Testmaker()
Function pointer to a function that creates a test object.
Definition utpp.h:375
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:79
const char * GetSuiteName()
Definition utpp.h:1221