UTPP
Loading...
Searching...
No Matches
utpp.h
Go to the documentation of this file.
1#pragma once
2/*
3 UTPP - A New Generation of UnitTest++
4 (c) Mircea Neacsu 2017-2025
5
6 See LICENSE file for full copyright information.
7*/
8
16
17#ifdef _MSC_VER
18#pragma warning( push )
19#pragma warning( disable : 4996 ) //disable deprecation warnings
20#endif
21
22#include <string>
23#include <deque>
24#include <stdexcept>
25#include <sstream>
26#include <cassert>
27#include <chrono>
28#ifndef _WIN32
29#include <sys/stat.h>
30#include <unistd.h>
31#endif
32
33/*
34 VC doesn't define the proper value for __cplusplus but _MSVC_LANG is correct.
35 We set UTPP_CPP_VERSION to correct value in all cases.
36*/
37
38#ifndef _MSVC_LANG
39#define UTPP_CPP_LANG __cplusplus
40#else
41#define UTPP_CPP_LANG _MSVC_LANG
42#endif
43
44
45#if ((UTPP_CPP_LANG >= 202002L && \
46 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 160000)))
47#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 1
48#else
49#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 0
50#endif
51
52// --------------- Global configuration options -------------------------------
53#define UTPP_VERSION "3.0.2"
54
55// --------------- end of configuration options -------------------------------
56
57namespace UnitTest {
58
60const size_t MAX_MESSAGE_SIZE = 1024;
61
62}
64#define DEFAULT_SUITE "DefaultSuite"
65
66//------------------------------ Test macros ----------------------------------
67#ifdef TEST
68#error Macro TEST is already defined
69#endif
70
71#ifdef TEST_FIXTURE
72#error Macro TEST_FIXTURE is already defined
73#endif
74
75#ifdef SUITE
76#error Macro SUITE is already defined
77#endif
78
94
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; \
101int main (ARGC,ARGV)
102#else
103#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
104#endif
105
106
111
122#define SUITE(Name) \
123 namespace Suite##Name \
124 { \
125 inline char const* GetSuiteName () { return #Name ; } \
126 } \
127 namespace Suite##Name
128
135#define TEST(Name) \
136 class Test##Name : public UnitTest::Test \
137 { \
138 public: \
139 Test##Name() : Test(#Name) {} \
140 private: \
141 void RunImpl() override; \
142 }; \
143 UnitTest::Test* Name##_maker() {return new Test##Name; } \
144 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
145 Name##_maker); \
146 void Test##Name::RunImpl()
147
148
160#define TEST_FIXTURE(Fixture, Name) \
161 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
162 { \
163 public: \
164 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
165 private: \
166 void RunImpl() override; \
167 }; \
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()
172
173#ifdef ABORT
174#error Macro ABORT is already defined
175#endif
176
183#define ABORT(value) \
184 do \
185 { \
186 if (UnitTest::Check(value)) \
187 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
188 } while (0)
189
190#ifdef ABORT_EX
191#error Macro ABORT_EX is already defined
192#endif
193
200#define ABORT_EX(value, ...) \
201 do \
202 { \
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); \
207 } \
208 } while (0)
209
211
214
221#define UTPP_TIME_CONSTRAINT(tmax) \
222 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
223
229#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
230
232
233namespace UnitTest {
234
235// forward declarations
236struct Failure;
237class TestSuite;
238
239void ReportFailure(const std::string& filename, int line, const std::string& message);
240
242class Test
243{
244public:
245 Test (const std::string& testName);
246 virtual ~Test() {};
247 void no_time_constraint ();
248 bool is_time_constraint () const;
249
250 int failure_count () const;
251 std::chrono::milliseconds test_time_ms () const;
252 const std::string& test_name () const;
253
254 void failure ();
255 void run ();
256
258 virtual void RunImpl () {};
259
260protected:
261 std::string name;
263 std::chrono::milliseconds time;
265
266private:
267 Test (Test const&) = delete;
268 Test& operator =(Test const&) = delete;
269 friend class TestSuite;
270};
271
274{
275 std::string filename;
276 std::string message;
278};
279
280
282class Reporter
283{
284public:
285 Reporter ();
286 virtual ~Reporter () {};
287
289 void SetTrace (bool on_off) { trace = on_off; }
290
292 virtual void SuiteStart (const TestSuite& suite);
293
295 virtual void TestStart (const Test& test);
296
298 virtual void ReportFailure (const Failure& failure);
299
301 virtual void TestFinish (const Test& test);
302
304 virtual int SuiteFinish (const TestSuite& suite);
305
307 virtual int Summary () { return total_failed_count; }
308
310 virtual void Clear ();
311
312protected:
316 std::chrono::milliseconds suite_time;
317
321 std::chrono::milliseconds total_time;
322
324 bool trace;
325
326};
327
329class ReporterDeferred : public Reporter
330{
331public:
332 ReporterDeferred () {};
333 void SuiteStart (const TestSuite& suite) override;
334 void TestStart (const Test& test) override;
335 void ReportFailure (const Failure& failure) override;
336 void TestFinish (const Test& test) override;
337 void Clear () override;
338
339protected:
342 {
343 TestResult ();
344 TestResult (const std::string& suite, const std::string& test);
345
346 std::string suite_name;
347 std::string test_name;
348 std::chrono::milliseconds test_time;
349 std::deque<Failure> failures;
350 };
351
352 std::deque<TestResult> results;
353};
354
356typedef UnitTest::Test* (*Testmaker)();
357
364class TestSuite
365{
366public:
369 {
370 public:
371 Inserter (const std::string& suite,
372 const std::string& test,
373 const std::string& file,
374 int line,
375 Testmaker func);
376
377 private:
378 const std::string test_name,
379 file_name;
380 const int line;
381 const Testmaker maker;
382
383 friend class TestSuite;
384 };
385
386 explicit TestSuite (const std::string& name);
387 void Add (const Inserter* inf);
388 bool IsEnabled () const;
389 void Enable (bool on_off);
390 int RunTests (Reporter& reporter, std::chrono::milliseconds max_runtime);
391
392 std::string name;
393
394private:
395 std::deque <const Inserter*> test_list;
396 std::chrono::milliseconds max_runtime;
397 bool enabled;
398
399 bool SetupCurrentTest (const Inserter* inf);
400 void RunCurrentTest (const Inserter* inf);
401 void TearDownCurrentTest (const Inserter* inf);
402};
403
405class Timer
406{
407public:
408 Timer ();
409 void Start ();
410 std::chrono::milliseconds GetTimeInMs () const;
411 std::chrono::microseconds GetTimeInUs () const;
412
413private:
414 std::chrono::steady_clock::time_point startTime;
415};
416
419{
420public:
421 template<typename R, typename P>
422 TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line);
424
425private:
426 void operator=(TimeConstraint const&) = delete;
427 TimeConstraint (TimeConstraint const&) = delete;
428
429 Timer timer;
430 std::string filename;
431 int line_number;
432 std::chrono::milliseconds tmax;
433};
434
437public:
438 void Add (const std::string& suite, const TestSuite::Inserter* inf);
439 int Run (const std::string& suite, Reporter& reporter, std::chrono::milliseconds max_time);
440 int RunAll (Reporter& reporter, std::chrono::milliseconds max_time);
441 static SuitesList& GetSuitesList ();
442 void Enable (const std::string& suite, bool enable = true);
443
444private:
445
446 std::deque <TestSuite> suites;
447};
448
450struct test_abort : public std::runtime_error
451{
452 test_abort (const char* file_, int line_, const char* msg)
453 : std::runtime_error (msg)
454 , file (file_)
455 , line (line_)
456 {};
457 const char* file;
458 int line;
459};
460
461struct tolerance_not_set : public std::runtime_error
462{
463 tolerance_not_set ()
464 : std::runtime_error ("UnitTest::default_tolerance not set")
465 {};
466};
467
469extern Test* CurrentTest;
470
472extern std::string CurrentSuite;
473
476
479
481int RunAllTests (Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
482
484void DisableSuite (const std::string& suite_name);
485
487void EnableSuite (const std::string& suite_name);
488
490int RunSuite (const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
491
493void ReportFailure (const std::string& filename, int line, const std::string& message);
494
495//-------------------------- Test member functions ----------------------------
497inline
498Test::Test(const std::string& test_name)
499 : name(test_name)
500 , failures(0)
501 , time(0)
502 , time_exempt(false)
503{
504}
505
511inline
513{
514 Timer test_timer;
515 test_timer.Start();
516
517 RunImpl();
518 time = test_timer.GetTimeInMs();
519}
520
524inline
526{
527 failures++;
528}
529
531inline
533{
534 return failures;
535}
536
538inline
539std::chrono::milliseconds Test::test_time_ms () const
540{
541 return time;
542}
543
545inline
546const std::string& Test::test_name () const
547{
548 return name;
549}
550
552inline
554{
555 time_exempt = true;
556}
557
559inline
561{
562 return !time_exempt;
563}
564
565//--------------------- Reporter member functions -----------------------------
566inline
567Reporter::Reporter ()
568 : suite_test_count (0)
569 , suite_failed_count (0)
570 , suite_failures_count (0)
571 , suite_time (0)
572 , total_test_count (0)
573 , total_failed_count (0)
574 , total_failures_count (0)
575 , total_time (0)
576 , suites_count (0)
577 , trace (false)
578{
579}
580
582inline
588
589inline
591{
594}
595
596inline
598{
599}
600
601inline
603{
604 int f = t.failure_count ();
605 if (f)
606 {
611 }
612 auto ms = t.test_time_ms ();
613 suite_time += ms;
614 total_time += ms;
615}
616
618inline
620{
622}
623
624
625inline
627{
628 using namespace std::chrono_literals;
632 suite_time = 0ms;
633
637 total_time = 0ms;
638
639 suites_count = 0;
640}
641
642//------------------- ReporterDeferred member functions -----------------------
644inline
649
651inline
652ReporterDeferred::TestResult::TestResult (const std::string& suite, const std::string& test)
653 : suite_name (suite)
654 , test_name (test)
655 , test_time (0)
656{
657}
658
666inline
668{
669 results.push_back (TestResult (suite.name, std::string()));
670}
671
678inline
680{
681 Reporter::TestStart (test);
682 results.push_back (TestResult (CurrentSuite, test.test_name ()));
683}
684
689inline
691{
692 assert (!results.empty ());
693
694 Reporter::ReportFailure (failure);
695 results.back ().failures.push_back (failure);
696}
697
702inline
704{
706 results.back ().test_time = test.test_time_ms();
707}
708
710{
712 results.clear ();
713}
714
715
716//------------------- TestSuite member functions ------------------------------
717
718inline
719TestSuite::TestSuite (const std::string& name_)
720 : name (name_)
721 , max_runtime (0)
722 , enabled (true)
723{
724}
725
734inline
735void TestSuite::Add (const Inserter* inf)
736{
737 test_list.push_back (inf);
738}
739
749inline
750int TestSuite::RunTests (Reporter& rep, std::chrono::milliseconds maxtime)
751{
754 CurrentReporter = &rep;
755
757 CurrentReporter->SuiteStart (*this);
758 std::deque <const Inserter*>::iterator listp = test_list.begin ();
759 max_runtime = maxtime;
760 while (listp != test_list.end ())
761 {
763 if (SetupCurrentTest (*listp))
764 {
765 RunCurrentTest (*listp);
766 TearDownCurrentTest (*listp);
767 }
769 ++listp;
770 }
772 return CurrentReporter->SuiteFinish (*this);
773}
774
784inline
785bool TestSuite::SetupCurrentTest (const Inserter* inf)
786{
787 bool ok = false;
788 try {
789 CurrentTest = (inf->maker)();
790 ok = true;
791 }
792 catch (UnitTest::test_abort& x)
793 {
794 std::stringstream stream;
795 stream << " Aborted setup of " << inf->test_name << " - " << x.what ();
796 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
797 ReportFailure (x.file, x.line, stream.str ());
798 delete CurrentTest;
799 CurrentTest = 0;
800 }
801 catch (const std::exception& e)
802 {
803 std::stringstream stream;
804 stream << "Unhandled exception: " << e.what ()
805 << " while setting up test " << inf->test_name;
806 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
807 ReportFailure (inf->file_name, inf->line, stream.str ());
808 delete CurrentTest;
809 CurrentTest = 0;
810 }
811 catch (...)
812 {
813 std::stringstream stream;
814 stream << "Setup unhandled exception while setting up test " << inf->test_name;
815 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
816 ReportFailure (inf->file_name, inf->line, stream.str ());
817 delete CurrentTest;
818 CurrentTest = 0;
819 }
820 return ok;
821}
822
824inline
825void TestSuite::RunCurrentTest (const Inserter* inf)
826{
827 assert (CurrentTest);
828 CurrentReporter->TestStart (*CurrentTest);
829
830
831 try {
832 CurrentTest->run ();
833 }
834 catch (UnitTest::test_abort& x)
835 {
836 ReportFailure (x.file, x.line, std::string ("Test aborted: ") + x.what ());
837 }
838 catch (const std::exception& e)
839 {
840 std::stringstream stream;
841 stream << "Unhandled exception: " << e.what ()
842 << " while running test " << inf->test_name;
843 ReportFailure (inf->file_name, inf->line, stream.str ());
844 }
845 catch (...)
846 {
847 std::stringstream stream;
848 stream << "Unhandled exception while running test " << inf->test_name;
849 ReportFailure (inf->file_name, inf->line, stream.str ());
850 }
851
852 auto actual_time = CurrentTest->test_time_ms ();
853 if (CurrentTest->is_time_constraint () && max_runtime.count() && actual_time > max_runtime)
854 {
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
859 << max_runtime
860#else
861 << max_runtime.count () << "ms"
862#endif
863 << "; actual = "
864#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
865 << actual_time;
866#else
867 << actual_time.count ();
868#endif
869 ReportFailure (inf->file_name, inf->line, stream.str ());
870 }
871 CurrentReporter->TestFinish (*CurrentTest);
872}
873
875inline
876void TestSuite::TearDownCurrentTest (const Inserter* inf)
877{
878 try {
879 delete CurrentTest;
880 CurrentTest = 0;
881 }
882 catch (const std::exception& e)
883 {
884 std::stringstream stream;
885 stream << "Unhandled exception: " << e.what ()
886 << " while tearing down test " << inf->test_name;
887 ReportFailure (inf->file_name, inf->line, stream.str ());
888 }
889 catch (...)
890 {
891 std::stringstream stream;
892 stream << "Unhandled exception tearing down test " << inf->test_name;
893 ReportFailure (inf->file_name, inf->line, stream.str ());
894 }
895}
896
898inline
900{
901 return enabled;
902}
903
905inline
906void TestSuite::Enable (bool on_off)
907{
908 enabled = on_off;
909}
910
911//------------------ TestSuite::Inserter --------------------------------------
922inline
923TestSuite::Inserter::Inserter (const std::string& suite, const std::string& test,
924 const std::string& file, int ln, Testmaker func)
925 : test_name (test)
926 , file_name (file)
927 , line (ln)
928 , maker (func)
929{
930 SuitesList::GetSuitesList ().Add (suite, this);
931}
932
933//-----------------Timer member functions -------------------------------------
934inline
935Timer::Timer ()
936{
937}
938
940inline
942{
943 startTime = std::chrono::steady_clock::now ();
944}
945
947inline
948std::chrono::milliseconds Timer::GetTimeInMs () const
949{
950 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
951 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
952}
953
955inline
956std::chrono::microseconds Timer::GetTimeInUs () const
957{
958 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
959 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
960}
961
962//------------------TimeConstraint member functions ---------------------------
963
973template <typename R, typename P>
974TimeConstraint::TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line)
975 : filename (file)
976 , line_number (line)
977 , tmax (t)
978{
979 timer.Start ();
980}
981
986inline
988{
989 std::chrono::milliseconds t = timer.GetTimeInMs ();
990 if (t > tmax)
991 {
992 std::stringstream stream;
993 stream << "Time constraint failed. Expected time <"
994#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
995 << tmax
996#else
997 << tmax.count () << "ms"
998#endif
999 << "; actual = "
1000#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1001 << t;
1002#else
1003 << t.count () << "ms";
1004#endif
1005 ReportFailure (filename, line_number, stream.str ());
1006 }
1007}
1008
1009
1010//-------------------SuitesList member functions ------------------------------
1011
1020inline
1021void SuitesList::Add (const std::string& suite_name, const TestSuite::Inserter* inf)
1022{
1023 auto p = suites.begin ();
1024 while (p != suites.end () && p->name != suite_name)
1025 p++;
1026
1027 if (p == suites.end ())
1028 {
1029 suites.emplace_back (suite_name);
1030 suites.back ().Add (inf);
1031 }
1032 else
1033 p->Add (inf);
1034}
1035
1045inline
1046int SuitesList::Run (const std::string& suite_name, Reporter& reporter, std::chrono::milliseconds max_time)
1047{
1048 for (auto& s : suites)
1049 {
1050 if (s.name == suite_name)
1051 {
1052 s.RunTests (reporter, max_time);
1053 return reporter.Summary ();
1054 }
1055 }
1056 return -1;
1057}
1058
1066inline
1067int SuitesList::RunAll (Reporter& reporter, std::chrono::milliseconds max_time)
1068{
1069 for (auto& s : suites)
1070 {
1071 if (s.IsEnabled ())
1072 s.RunTests (reporter, max_time);
1073 }
1074
1075 return reporter.Summary ();
1076}
1077
1082inline
1084{
1085 static SuitesList all_suites;
1086 return all_suites;
1087}
1088
1096inline
1097void SuitesList::Enable (const std::string& suite, bool enable)
1098{
1099 for (auto& s : suites)
1100 {
1101 if (s.name == suite)
1102 {
1103 s.Enable (enable);
1104 break;
1105 }
1106 }
1107}
1108
1110
1124inline
1125int RunAllTests (Reporter& rpt, std::chrono::milliseconds max_time)
1126{
1127 rpt.Clear ();
1128 return SuitesList::GetSuitesList ().RunAll (rpt, max_time);
1129}
1130
1142inline
1143int RunSuite (const char* suite_name, Reporter& rpt, std::chrono::milliseconds max_time)
1144{
1145 return SuitesList::GetSuitesList ().Run (suite_name, rpt, max_time);
1146}
1147
1156inline
1157void DisableSuite (const std::string& suite_name)
1158{
1159 SuitesList::GetSuitesList ().Enable (suite_name, false);
1160}
1161
1169inline
1170void EnableSuite (const std::string& suite_name)
1171{
1172 SuitesList::GetSuitesList ().Enable (suite_name, true);
1173}
1174
1184inline
1185void ReportFailure(const std::string& filename, int line, const std::string& message)
1186{
1187 if (CurrentTest)
1188 CurrentTest->failure();
1189 Failure f = { filename, message, line };
1190 CurrentReporter->ReportFailure(f);
1191}
1192
1193} // end of UnitTest namespace
1194
1195
1200inline
1201const char* GetSuiteName ()
1202{
1203 return DEFAULT_SUITE;
1204}
1205
1206#include "reporter_stream.h"
1207#include "reporter_xml.h"
1208#ifdef _WIN32
1209#include "reporter_dbgout.h"
1210#endif
1211#include "checks.h"
1212
1214inline
1216{
1217 static UnitTest::ReporterStream the_default_reporter;
1218 return the_default_reporter;
1219}
1220
1221#if UTPP_CPP_LANG >= 201703L
1222// In C++ 17 and later we have inline data. TEST_MAIN is not really needed.
1225inline std::string UnitTest::CurrentSuite;
1226#endif
1227
1228#ifdef _MSC_VER
1229#pragma warning ( pop )
1230#endif
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
Definition utpp.h:365
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