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
37
38/*
39 MSVC doesn't define the proper value for __cplusplus but _MSVC_LANG is correct.
40 We set UTPP_CPP_LANG to correct value in all cases.
41*/
42
50#ifndef _MSVC_LANG
51#define UTPP_CPP_LANG __cplusplus
52#else
53#define UTPP_CPP_LANG _MSVC_LANG
54#endif
55
61#if ((UTPP_CPP_LANG >= 202002L && \
62 (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 160000)))
63#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 1
64#else
65#define UTPP_STD_CHRONO_OSTREAM_AVAILABLE 0
66#endif
67
68// --------------- Global configuration options -------------------------------
69
71#define UTPP_VERSION "3.0.3"
72
73// --------------- end of configuration options -------------------------------
75
76namespace UnitTest {
77
79const size_t MAX_MESSAGE_SIZE = 1024;
80
81}
83#define DEFAULT_SUITE "DefaultSuite"
84
85//------------------------------ Test macros ----------------------------------
86#ifdef TEST
87#error Macro TEST is already defined
88#endif
89
90#ifdef TEST_FIXTURE
91#error Macro TEST_FIXTURE is already defined
92#endif
93
94#ifdef SUITE
95#error Macro SUITE is already defined
96#endif
97
113
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; \
120int main (ARGC,ARGV)
121#else
122#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
123#endif
124
125
130
141#define SUITE(Name) \
142 namespace Suite##Name \
143 { \
144 inline char const* GetSuiteName () { return #Name ; } \
145 } \
146 namespace Suite##Name
147
154#define TEST(Name) \
155 class Test##Name : public UnitTest::Test \
156 { \
157 public: \
158 Test##Name() : Test(#Name) {} \
159 private: \
160 void RunImpl() override; \
161 }; \
162 UnitTest::Test* Name##_maker() {return new Test##Name; } \
163 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
164 Name##_maker); \
165 void Test##Name::RunImpl()
166
167
179#define TEST_FIXTURE(Fixture, Name) \
180 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
181 { \
182 public: \
183 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
184 private: \
185 void RunImpl() override; \
186 }; \
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()
191
192#ifdef ABORT
193#error Macro ABORT is already defined
194#endif
195
202#define ABORT(value) \
203 do \
204 { \
205 if (UnitTest::Check(value)) \
206 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
207 } while (0)
208
209#ifdef ABORT_EX
210#error Macro ABORT_EX is already defined
211#endif
212
219#define ABORT_EX(value, ...) \
220 do \
221 { \
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); \
226 } \
227 } while (0)
228
230
233
240#define UTPP_TIME_CONSTRAINT(tmax) \
241 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
242
248#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
249
251
252namespace UnitTest {
253
254// forward declarations
255struct Failure;
256class TestSuite;
257
258void ReportFailure(const std::string& filename, int line, const std::string& message);
259
261class Test
262{
263public:
264 Test (const std::string& testName);
265 virtual ~Test() {};
266 void no_time_constraint ();
267 bool is_time_constraint () const;
268
269 int failure_count () const;
270 std::chrono::milliseconds test_time_ms () const;
271 const std::string& test_name () const;
272
273 void failure ();
274 void run ();
275
277 virtual void RunImpl () {};
278
279protected:
280 std::string name;
282 std::chrono::milliseconds time;
284
285private:
286 Test (Test const&) = delete;
287 Test& operator =(Test const&) = delete;
288 friend class TestSuite;
289};
290
293{
294 std::string filename;
295 std::string message;
297};
298
299
301class Reporter
302{
303public:
304 Reporter ();
305 virtual ~Reporter () {};
306
308 void SetTrace (bool on_off) { trace = on_off; }
309
311 virtual void SuiteStart (const TestSuite& suite);
312
314 virtual void TestStart (const Test& test);
315
317 virtual void ReportFailure (const Failure& failure);
318
320 virtual void TestFinish (const Test& test);
321
323 virtual int SuiteFinish (const TestSuite& suite);
324
326 virtual int Summary () { return total_failed_count; }
327
329 virtual void Clear ();
330
331protected:
335 std::chrono::milliseconds suite_time;
336
340 std::chrono::milliseconds total_time;
341
343 bool trace;
344
345};
346
348class ReporterDeferred : public Reporter
349{
350public:
351 ReporterDeferred () {};
352 void SuiteStart (const TestSuite& suite) override;
353 void TestStart (const Test& test) override;
354 void ReportFailure (const Failure& failure) override;
355 void TestFinish (const Test& test) override;
356 void Clear () override;
357
358protected:
361 {
362 TestResult ();
363 TestResult (const std::string& suite, const std::string& test);
364
365 std::string suite_name;
366 std::string test_name;
367 std::chrono::milliseconds test_time;
368 std::deque<Failure> failures;
369 };
370
371 std::deque<TestResult> results;
372};
373
375typedef UnitTest::Test* (*Testmaker)();
376
383class TestSuite
384{
385public:
388 {
389 public:
390 Inserter (const std::string& suite,
391 const std::string& test,
392 const std::string& file,
393 int line,
394 Testmaker func);
395
396 private:
397 const std::string test_name,
398 file_name;
399 const int line;
400 const Testmaker maker;
401
402 friend class TestSuite;
403 };
404
405 explicit TestSuite (const std::string& name);
406 void Add (const Inserter* inf);
407 bool IsEnabled () const;
408 void Enable (bool on_off);
409 int RunTests (Reporter& reporter, std::chrono::milliseconds max_runtime);
410
411 std::string name;
412
413private:
414 std::deque <const Inserter*> test_list;
415 std::chrono::milliseconds max_runtime;
416 bool enabled;
417
418 bool SetupCurrentTest (const Inserter* inf);
419 void RunCurrentTest (const Inserter* inf);
420 void TearDownCurrentTest (const Inserter* inf);
421};
422
424class Timer
425{
426public:
427 Timer ();
428 void Start ();
429 std::chrono::milliseconds GetTimeInMs () const;
430 std::chrono::microseconds GetTimeInUs () const;
431
432private:
433 std::chrono::steady_clock::time_point startTime;
434};
435
438{
439public:
440 template<typename R, typename P>
441 TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line);
443
444private:
445 void operator=(TimeConstraint const&) = delete;
446 TimeConstraint (TimeConstraint const&) = delete;
447
448 Timer timer;
449 std::string filename;
450 int line_number;
451 std::chrono::milliseconds tmax;
452};
453
456public:
457 void Add (const std::string& suite, const TestSuite::Inserter* inf);
458 int Run (const std::string& suite, Reporter& reporter, std::chrono::milliseconds max_time);
459 int RunAll (Reporter& reporter, std::chrono::milliseconds max_time);
460 static SuitesList& GetSuitesList ();
461 void Enable (const std::string& suite, bool enable = true);
462
463private:
464
465 std::deque <TestSuite> suites;
466};
467
469struct test_abort : public std::runtime_error
470{
471 test_abort (const char* file_, int line_, const char* msg)
472 : std::runtime_error (msg)
473 , file (file_)
474 , line (line_)
475 {};
476 const char* file;
477 int line;
478};
479
480struct tolerance_not_set : public std::runtime_error
481{
482 tolerance_not_set ()
483 : std::runtime_error ("UnitTest::default_tolerance not set")
484 {};
485};
486
488extern Test* CurrentTest;
489
491extern std::string CurrentSuite;
492
495
499
501int RunAllTests (Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
502
504void DisableSuite (const std::string& suite_name);
505
507void EnableSuite (const std::string& suite_name);
508
510int RunSuite (const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
511
513void ReportFailure (const std::string& filename, int line, const std::string& message);
514
515//-------------------------- Test member functions ----------------------------
517inline
518Test::Test(const std::string& test_name)
519 : name(test_name)
520 , failures(0)
521 , time(0)
522 , time_exempt(false)
523{
524}
525
531inline
533{
534 Timer test_timer;
535 test_timer.Start();
536
537 RunImpl();
538 time = test_timer.GetTimeInMs();
539}
540
544inline
546{
547 failures++;
548}
549
551inline
553{
554 return failures;
555}
556
558inline
559std::chrono::milliseconds Test::test_time_ms () const
560{
561 return time;
562}
563
565inline
566const std::string& Test::test_name () const
567{
568 return name;
569}
570
572inline
574{
575 time_exempt = true;
576}
577
579inline
581{
582 return !time_exempt;
583}
584
585//--------------------- Reporter member functions -----------------------------
586inline
587Reporter::Reporter ()
588 : suite_test_count (0)
589 , suite_failed_count (0)
590 , suite_failures_count (0)
591 , suite_time (0)
592 , total_test_count (0)
593 , total_failed_count (0)
594 , total_failures_count (0)
595 , total_time (0)
596 , suites_count (0)
597 , trace (false)
598{
599}
600
602inline
608
609inline
611{
614}
615
616inline
618{
619}
620
621inline
623{
624 int f = t.failure_count ();
625 if (f)
626 {
631 }
632 auto ms = t.test_time_ms ();
633 suite_time += ms;
634 total_time += ms;
635}
636
638inline
640{
642}
643
644
645inline
647{
648 using namespace std::chrono_literals;
652 suite_time = 0ms;
653
657 total_time = 0ms;
658
659 suites_count = 0;
660}
661
662//------------------- ReporterDeferred member functions -----------------------
664inline
669
671inline
672ReporterDeferred::TestResult::TestResult (const std::string& suite, const std::string& test)
673 : suite_name (suite)
674 , test_name (test)
675 , test_time (0)
676{
677}
678
686inline
688{
689 results.push_back (TestResult (suite.name, std::string()));
690}
691
698inline
700{
701 Reporter::TestStart (test);
702 results.push_back (TestResult (CurrentSuite, test.test_name ()));
703}
704
709inline
711{
712 assert (!results.empty ());
713
714 Reporter::ReportFailure (failure);
715 results.back ().failures.push_back (failure);
716}
717
722inline
724{
726 results.back ().test_time = test.test_time_ms();
727}
728
730{
732 results.clear ();
733}
734
735
736//------------------- TestSuite member functions ------------------------------
737
738inline
739TestSuite::TestSuite (const std::string& name_)
740 : name (name_)
741 , max_runtime (0)
742 , enabled (true)
743{
744}
745
754inline
755void TestSuite::Add (const Inserter* inf)
756{
757 test_list.push_back (inf);
758}
759
769inline
770int TestSuite::RunTests (Reporter& rep, std::chrono::milliseconds maxtime)
771{
774 CurrentReporter = &rep;
775
778 std::deque <const Inserter*>::iterator listp = test_list.begin ();
779 max_runtime = maxtime;
780 while (listp != test_list.end ())
781 {
783 if (SetupCurrentTest (*listp))
784 {
785 RunCurrentTest (*listp);
786 TearDownCurrentTest (*listp);
787 }
789 ++listp;
790 }
792 return CurrentReporter->SuiteFinish (*this);
793}
794
804inline
805bool TestSuite::SetupCurrentTest (const Inserter* inf)
806{
807 bool ok = false;
808 try {
809 CurrentTest = (inf->maker)();
810 ok = true;
811 }
812 catch (UnitTest::test_abort& x)
813 {
814 std::stringstream stream;
815 stream << " Aborted setup of " << inf->test_name << " - " << x.what ();
816 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
817 ReportFailure (x.file, x.line, stream.str ());
818 delete CurrentTest;
819 CurrentTest = 0;
820 }
821 catch (const std::exception& e)
822 {
823 std::stringstream stream;
824 stream << "Unhandled exception: " << e.what ()
825 << " while setting up test " << inf->test_name;
826 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
827 ReportFailure (inf->file_name, inf->line, stream.str ());
828 delete CurrentTest;
829 CurrentTest = 0;
830 }
831 catch (...)
832 {
833 std::stringstream stream;
834 stream << "Setup unhandled exception while setting up test " << inf->test_name;
835 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
836 ReportFailure (inf->file_name, inf->line, stream.str ());
837 delete CurrentTest;
838 CurrentTest = 0;
839 }
840 return ok;
841}
842
844inline
845void TestSuite::RunCurrentTest (const Inserter* inf)
846{
847 assert (CurrentTest);
848 CurrentReporter->TestStart (*CurrentTest);
849
850
851 try {
852 CurrentTest->run ();
853 }
854 catch (UnitTest::test_abort& x)
855 {
856 ReportFailure (x.file, x.line, std::string ("Test aborted: ") + x.what ());
857 }
858 catch (const std::exception& e)
859 {
860 std::stringstream stream;
861 stream << "Unhandled exception: " << e.what ()
862 << " while running test " << inf->test_name;
863 ReportFailure (inf->file_name, inf->line, stream.str ());
864 }
865 catch (...)
866 {
867 std::stringstream stream;
868 stream << "Unhandled exception while running test " << inf->test_name;
869 ReportFailure (inf->file_name, inf->line, stream.str ());
870 }
871
872 auto actual_time = CurrentTest->test_time_ms ();
873 if (CurrentTest->is_time_constraint () && max_runtime.count() && actual_time > max_runtime)
874 {
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
879 << max_runtime
880#else
881 << max_runtime.count () << "ms"
882#endif
883 << "; actual = "
884#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
885 << actual_time;
886#else
887 << actual_time.count ();
888#endif
889 ReportFailure (inf->file_name, inf->line, stream.str ());
890 }
891 CurrentReporter->TestFinish (*CurrentTest);
892}
893
895inline
896void TestSuite::TearDownCurrentTest (const Inserter* inf)
897{
898 try {
899 delete CurrentTest;
900 CurrentTest = 0;
901 }
902 catch (const std::exception& e)
903 {
904 std::stringstream stream;
905 stream << "Unhandled exception: " << e.what ()
906 << " while tearing down test " << inf->test_name;
907 ReportFailure (inf->file_name, inf->line, stream.str ());
908 }
909 catch (...)
910 {
911 std::stringstream stream;
912 stream << "Unhandled exception tearing down test " << inf->test_name;
913 ReportFailure (inf->file_name, inf->line, stream.str ());
914 }
915}
916
918inline
920{
921 return enabled;
922}
923
925inline
926void TestSuite::Enable (bool on_off)
927{
928 enabled = on_off;
929}
930
931//------------------ TestSuite::Inserter --------------------------------------
942inline
943TestSuite::Inserter::Inserter (const std::string& suite, const std::string& test,
944 const std::string& file, int ln, Testmaker func)
945 : test_name (test)
946 , file_name (file)
947 , line (ln)
948 , maker (func)
949{
950 SuitesList::GetSuitesList ().Add (suite, this);
951}
952
953//-----------------Timer member functions -------------------------------------
954inline
955Timer::Timer ()
956{
957}
958
960inline
962{
963 startTime = std::chrono::steady_clock::now ();
964}
965
967inline
968std::chrono::milliseconds Timer::GetTimeInMs () const
969{
970 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
971 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
972}
973
975inline
976std::chrono::microseconds Timer::GetTimeInUs () const
977{
978 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
979 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
980}
981
982//------------------TimeConstraint member functions ---------------------------
983
993template <typename R, typename P>
994TimeConstraint::TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line)
995 : filename (file)
996 , line_number (line)
997 , tmax (t)
998{
999 timer.Start ();
1000}
1001
1006inline
1008{
1009 std::chrono::milliseconds t = timer.GetTimeInMs ();
1010 if (t > tmax)
1011 {
1012 std::stringstream stream;
1013 stream << "Time constraint failed. Expected time <"
1014#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1015 << tmax
1016#else
1017 << tmax.count () << "ms"
1018#endif
1019 << "; actual = "
1020#if UTPP_STD_CHRONO_OSTREAM_AVAILABLE
1021 << t;
1022#else
1023 << t.count () << "ms";
1024#endif
1025 ReportFailure (filename, line_number, stream.str ());
1026 }
1027}
1028
1029
1030//-------------------SuitesList member functions ------------------------------
1031
1040inline
1041void SuitesList::Add (const std::string& suite_name, const TestSuite::Inserter* inf)
1042{
1043 auto p = suites.begin ();
1044 while (p != suites.end () && p->name != suite_name)
1045 p++;
1046
1047 if (p == suites.end ())
1048 {
1049 suites.emplace_back (suite_name);
1050 suites.back ().Add (inf);
1051 }
1052 else
1053 p->Add (inf);
1054}
1055
1065inline
1066int SuitesList::Run (const std::string& suite_name, Reporter& reporter, std::chrono::milliseconds max_time)
1067{
1068 for (auto& s : suites)
1069 {
1070 if (s.name == suite_name)
1071 {
1072 s.RunTests (reporter, max_time);
1073 return reporter.Summary ();
1074 }
1075 }
1076 return -1;
1077}
1078
1086inline
1087int SuitesList::RunAll (Reporter& reporter, std::chrono::milliseconds max_time)
1088{
1089 for (auto& s : suites)
1090 {
1091 if (s.IsEnabled ())
1092 s.RunTests (reporter, max_time);
1093 }
1094
1095 return reporter.Summary ();
1096}
1097
1102inline
1104{
1105 static SuitesList all_suites;
1106 return all_suites;
1107}
1108
1116inline
1117void SuitesList::Enable (const std::string& suite, bool enable)
1118{
1119 for (auto& s : suites)
1120 {
1121 if (s.name == suite)
1122 {
1123 s.Enable (enable);
1124 break;
1125 }
1126 }
1127}
1128
1130
1144inline
1145int RunAllTests (Reporter& rpt, std::chrono::milliseconds max_time)
1146{
1147 rpt.Clear ();
1148 return SuitesList::GetSuitesList ().RunAll (rpt, max_time);
1149}
1150
1162inline
1163int RunSuite (const char* suite_name, Reporter& rpt, std::chrono::milliseconds max_time)
1164{
1165 return SuitesList::GetSuitesList ().Run (suite_name, rpt, max_time);
1166}
1167
1176inline
1177void DisableSuite (const std::string& suite_name)
1178{
1179 SuitesList::GetSuitesList ().Enable (suite_name, false);
1180}
1181
1189inline
1190void EnableSuite (const std::string& suite_name)
1191{
1192 SuitesList::GetSuitesList ().Enable (suite_name, true);
1193}
1194
1204inline
1205void ReportFailure(const std::string& filename, int line, const std::string& message)
1206{
1207 if (CurrentTest)
1209 Failure f = { filename, message, line };
1211}
1212
1213} // end of UnitTest namespace
1214
1215
1220inline
1221const char* GetSuiteName ()
1222{
1223 return DEFAULT_SUITE;
1224}
1225
1226#include "reporter_stream.h"
1227#include "reporter_xml.h"
1228#ifdef _WIN32
1229#include "reporter_dbgout.h"
1230#endif
1231#include "checks.h"
1232
1234inline
1236{
1237 static UnitTest::ReporterStream the_default_reporter;
1238 return the_default_reporter;
1239}
1240
1241#if UTPP_CPP_LANG >= 201703L
1242// In C++ 17 and later we have inline data. TEST_MAIN is not really needed.
1245inline std::string UnitTest::CurrentSuite;
1246#endif
1247
1248#ifdef _MSC_VER
1249#pragma warning ( pop )
1250#endif
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
Definition utpp.h:384
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