21#define MUST_UNDEF_WINSOCK
26#ifdef MUST_UNDEF_WINSOCK
28#undef MUST_UNDEF_WINSOCK
33#pragma warning( push )
34#pragma warning( disable : 4996 )
49#define UTPP_VERSION "3.0.1"
60#define DEFAULT_SUITE "DefaultSuite"
64#error Macro TEST is already defined
68#error Macro TEST_FIXTURE is already defined
72#error Macro SUITE is already defined
79#define _MSVC_LANG __cplusplus
98#if _MSVC_LANG < 201703L
99#define TEST_MAIN(ARGC, ARGV) \
100UnitTest::Test *UnitTest::CurrentTest; \
101UnitTest::Reporter *UnitTest::CurrentReporter; \
102double UnitTest::default_tolerance; \
103std::string UnitTest::CurrentSuite; \
106#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
126 namespace Suite##Name \
128 inline char const* GetSuiteName () { return #Name ; } \
130 namespace Suite##Name
139 class Test##Name : public UnitTest::Test \
142 Test##Name() : Test(#Name) {} \
144 void RunImpl() override; \
146 UnitTest::Test* Name##_maker() {return new Test##Name; } \
147 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
149 void Test##Name::RunImpl()
163#define TEST_FIXTURE(Fixture, Name) \
164 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
167 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
169 void RunImpl() override; \
171 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
172 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
173 __FILE__, __LINE__, Name##_maker); \
174 void Fixture##Name##Helper::RunImpl()
177#error Macro ABORT is already defined
186#define ABORT(value) \
189 if (UnitTest::Check(value)) \
190 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
194#error Macro ABORT_EX is already defined
203#define ABORT_EX(value, ...) \
206 if (UnitTest::Check(value)) { \
207 char message[UnitTest::MAX_MESSAGE_SIZE]; \
208 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
209 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
224#define UTPP_TIME_CONSTRAINT(tmax) \
225 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
232#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
242void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
248 Test (
const std::string& testName);
266 std::chrono::milliseconds
time;
271 Test& operator =(
Test const&) =
delete;
272 friend class TestSuite;
289 virtual ~Reporter () {};
313 virtual void Clear ();
332class ReporterDeferred :
public Reporter
335 ReporterDeferred () {};
340 void Clear ()
override;
347 TestResult (
const std::string& suite,
const std::string& test);
375 const std::string& test,
376 const std::string& file,
381 const std::string test_name,
386 friend class TestSuite;
389 explicit TestSuite (
const std::string&
name);
392 void Enable (
bool on_off);
398 std::deque <const Inserter*> test_list;
399 std::chrono::milliseconds max_runtime;
402 bool SetupCurrentTest (
const Inserter* inf);
403 void RunCurrentTest (
const Inserter* inf);
404 void TearDownCurrentTest (
const Inserter* inf);
417 std::chrono::steady_clock::time_point startTime;
424 template<
typename R,
typename P>
425 TimeConstraint (std::chrono::duration<R, P> t,
const char* file,
int line);
433 std::string filename;
435 std::chrono::milliseconds tmax;
442 int Run (
const std::string& suite,
Reporter& reporter, std::chrono::milliseconds max_time);
443 int RunAll (
Reporter& reporter, std::chrono::milliseconds max_time);
445 void Enable (
const std::string& suite,
bool enable =
true);
449 std::deque <TestSuite> suites;
453struct test_abort :
public std::runtime_error
455 test_abort (
const char* file_,
int line_,
const char* msg)
456 : std::runtime_error (msg)
464struct tolerance_not_set :
public std::runtime_error
467 : std::runtime_error (
"UnitTest::default_tolerance not set")
493int RunSuite (
const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
496void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
571 : suite_test_count (0)
572 , suite_failed_count (0)
573 , suite_failures_count (0)
575 , total_test_count (0)
576 , total_failed_count (0)
577 , total_failures_count (0)
631 using namespace std::chrono_literals;
698 results.back ().failures.push_back (failure);
722TestSuite::TestSuite (
const std::string& name_)
740 test_list.push_back (inf);
761 std::deque <const Inserter*>::iterator listp = test_list.begin ();
762 max_runtime = maxtime;
763 while (listp != test_list.end ())
766 if (SetupCurrentTest (*listp))
768 RunCurrentTest (*listp);
769 TearDownCurrentTest (*listp);
788bool TestSuite::SetupCurrentTest (
const Inserter* inf)
797 std::stringstream stream;
798 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
799 CurrentTest =
new Test (inf->test_name);
800 ReportFailure (x.file, x.line, stream.str ());
804 catch (
const std::exception& e)
806 std::stringstream stream;
807 stream <<
"Unhandled exception: " << e.what ()
808 <<
" while setting up test " << inf->test_name;
816 std::stringstream stream;
817 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
828void TestSuite::RunCurrentTest (
const Inserter* inf)
830 assert (CurrentTest);
837 catch (UnitTest::test_abort& x)
839 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
841 catch (
const std::exception& e)
843 std::stringstream stream;
844 stream <<
"Unhandled exception: " << e.what ()
845 <<
" while running test " << inf->test_name;
850 std::stringstream stream;
851 stream <<
"Unhandled exception while running test " << inf->test_name;
858 std::stringstream stream;
859 stream <<
"Global time constraint failed while running test " << inf->test_name
860 <<
" Expected time <"
861#if _MSVC_LANG >= 202002L
864 << max_runtime.count () <<
"ms"
867#if _MSVC_LANG >= 202002L
870 << actual_time.count ();
879void TestSuite::TearDownCurrentTest (
const Inserter* inf)
885 catch (
const std::exception& e)
887 std::stringstream stream;
888 stream <<
"Unhandled exception: " << e.what ()
889 <<
" while tearing down test " << inf->test_name;
894 std::stringstream stream;
895 stream <<
"Unhandled exception tearing down test " << inf->test_name;
927 const std::string& file,
int ln,
Testmaker func)
946 startTime = std::chrono::steady_clock::now ();
953 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
954 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
961 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
962 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
976template <
typename R,
typename P>
992 std::chrono::milliseconds t = timer.GetTimeInMs ();
995 std::stringstream stream;
996 stream <<
"Time constraint failed. Expected time <"
997#if _MSVC_LANG >= 202002L
1000 << tmax.count () <<
"ms"
1003#if _MSVC_LANG >= 202002L
1006 << t.count () <<
"ms";
1026 auto p = suites.begin ();
1027 while (p != suites.end () && p->name != suite_name)
1030 if (p == suites.end ())
1032 suites.emplace_back (suite_name);
1033 suites.back ().Add (inf);
1051 for (
auto& s : suites)
1053 if (s.name == suite_name)
1055 s.RunTests (reporter, max_time);
1072 for (
auto& s : suites)
1075 s.RunTests (reporter, max_time);
1102 for (
auto& s : suites)
1104 if (s.name == suite)
1188void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1192 Failure f = { filename, message, line };
1221 return the_default_reporter;
1224#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1225 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
1233#pragma warning ( pop )
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:712
void TestStart(const Test &test) override
Definition utpp.h:682
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:355
void ReportFailure(const Failure &failure) override
Definition utpp.h:693
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:670
void TestFinish(const Test &test) override
Definition utpp.h:706
Abstract base for all reporters.
Definition utpp.h:286
virtual void Clear()
Reset all statistics.
Definition utpp.h:629
int suites_count
number of suites ran
Definition utpp.h:326
int suite_failures_count
number of failures in suite
Definition utpp.h:318
int total_failures_count
total number of failures
Definition utpp.h:323
int suite_test_count
number of tests in suite
Definition utpp.h:316
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
int suite_failed_count
number of failed tests in suite
Definition utpp.h:317
std::chrono::milliseconds suite_time
total suite running time in milliseconds
Definition utpp.h:319
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 SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:292
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:439
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1024
int Run(const std::string &suite, Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1049
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1100
int RunAll(Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1070
static SuitesList & GetSuitesList()
Definition utpp.h:1086
Representation of a test case.
Definition utpp.h:246
std::chrono::milliseconds time
Run time.
Definition utpp.h:266
int failures
Number of failures in this test.
Definition utpp.h:265
void run()
Definition utpp.h:515
void failure()
Definition utpp.h:528
const std::string & test_name() const
Return test name.
Definition utpp.h:549
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:535
virtual void RunImpl()
Actual body of test.
Definition utpp.h:261
Test(const std::string &testName)
Constructor.
Definition utpp.h:501
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:563
std::string name
Name of this test.
Definition utpp.h:264
bool time_exempt
true if exempt from time constraints
Definition utpp.h:267
std::chrono::milliseconds test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:542
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:556
Constructor of this objects inserts the test in suite.
Definition utpp.h:372
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:926
std::string name
Suite name.
Definition utpp.h:395
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:902
void Add(const Inserter *inf)
Definition utpp.h:738
int RunTests(Reporter &reporter, std::chrono::milliseconds max_runtime)
Definition utpp.h:753
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:909
TimeConstraint(std::chrono::duration< R, P > t, const char *file, int line)
Definition utpp.h:977
~TimeConstraint()
Definition utpp.h:990
An object that can be interrogated to get elapsed time.
Definition utpp.h:409
std::chrono::microseconds GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:959
std::chrono::milliseconds GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:951
void Start()
Record starting time.
Definition utpp.h:944
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1173
int RunAllTests(Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from all test suites.
Definition utpp.h:1128
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:1146
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1160
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: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
Test results including all failure messages
Definition utpp.h:345
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:352
std::string suite_name
suite name
Definition utpp.h:349
std::string test_name
test name
Definition utpp.h:350
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:351
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:648
Exception thrown by ABORT macro.
Definition utpp.h:454
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:359
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:60
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1188
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:1218
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:56
const char * GetSuiteName()
Definition utpp.h:1204