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 _WIN32
18//Acrobatics to leave out winsock.h (for the benefit of winsock2.h)
19#ifndef _WINSOCKAPI_
20#define _WINSOCKAPI_
21#define MUST_UNDEF_WINSOCK
22#endif
23
24#include <windows.h>
25
26#ifdef MUST_UNDEF_WINSOCK
27#undef _WINSOCKAPI_
28#undef MUST_UNDEF_WINSOCK
29#endif
30#endif
31
32#ifdef _MSC_VER
33#pragma warning( push )
34#pragma warning( disable : 4996 ) //disable deprecation warnings
35#endif
36
37#include <string>
38#include <deque>
39#include <stdexcept>
40#include <sstream>
41#include <cassert>
42#include <chrono>
43#ifndef _WIN32
44#include <sys/stat.h>
45#include <unistd.h>
46#endif
47
48// --------------- Global configuration options -------------------------------
49#define UTPP_VERSION "3.0.1"
50
51// --------------- end of configuration options -------------------------------
52
53namespace UnitTest {
54
56const size_t MAX_MESSAGE_SIZE = 1024;
57
58}
60#define DEFAULT_SUITE "DefaultSuite"
61
62//------------------------------ Test macros ----------------------------------
63#ifdef TEST
64#error Macro TEST is already defined
65#endif
66
67#ifdef TEST_FIXTURE
68#error Macro TEST_FIXTURE is already defined
69#endif
70
71#ifdef SUITE
72#error Macro SUITE is already defined
73#endif
74
75/* VC doesn't define the proper value for __cplusplus but _MSVC_LANG is correct.
76 Fake it here for non-MS compilers
77*/
78#ifndef _MSVC_LANG
79#define _MSVC_LANG __cplusplus
80#endif
81
97
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; \
104int main (ARGC,ARGV)
105#else
106#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
107#endif
108
109
114
125#define SUITE(Name) \
126 namespace Suite##Name \
127 { \
128 inline char const* GetSuiteName () { return #Name ; } \
129 } \
130 namespace Suite##Name
131
138#define TEST(Name) \
139 class Test##Name : public UnitTest::Test \
140 { \
141 public: \
142 Test##Name() : Test(#Name) {} \
143 private: \
144 void RunImpl() override; \
145 }; \
146 UnitTest::Test* Name##_maker() {return new Test##Name; } \
147 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
148 Name##_maker); \
149 void Test##Name::RunImpl()
150
151
163#define TEST_FIXTURE(Fixture, Name) \
164 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
165 { \
166 public: \
167 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
168 private: \
169 void RunImpl() override; \
170 }; \
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()
175
176#ifdef ABORT
177#error Macro ABORT is already defined
178#endif
179
186#define ABORT(value) \
187 do \
188 { \
189 if (UnitTest::Check(value)) \
190 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
191 } while (0)
192
193#ifdef ABORT_EX
194#error Macro ABORT_EX is already defined
195#endif
196
203#define ABORT_EX(value, ...) \
204 do \
205 { \
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); \
210 } \
211 } while (0)
212
214
217
224#define UTPP_TIME_CONSTRAINT(tmax) \
225 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
226
232#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
233
235
236namespace UnitTest {
237
238// forward declarations
239struct Failure;
240class TestSuite;
241
242void ReportFailure(const std::string& filename, int line, const std::string& message);
243
245class Test
246{
247public:
248 Test (const std::string& testName);
249 virtual ~Test() {};
250 void no_time_constraint ();
251 bool is_time_constraint () const;
252
253 int failure_count () const;
254 std::chrono::milliseconds test_time_ms () const;
255 const std::string& test_name () const;
256
257 void failure ();
258 void run ();
259
261 virtual void RunImpl () {};
262
263protected:
264 std::string name;
266 std::chrono::milliseconds time;
268
269private:
270 Test (Test const&) = delete;
271 Test& operator =(Test const&) = delete;
272 friend class TestSuite;
273};
274
277{
278 std::string filename;
279 std::string message;
281};
282
283
285class Reporter
286{
287public:
288 Reporter ();
289 virtual ~Reporter () {};
290
292 void SetTrace (bool on_off) { trace = on_off; }
293
295 virtual void SuiteStart (const TestSuite& suite);
296
298 virtual void TestStart (const Test& test);
299
301 virtual void ReportFailure (const Failure& failure);
302
304 virtual void TestFinish (const Test& test);
305
307 virtual int SuiteFinish (const TestSuite& suite);
308
310 virtual int Summary () { return total_failed_count; }
311
313 virtual void Clear ();
314
315protected:
319 std::chrono::milliseconds suite_time;
320
324 std::chrono::milliseconds total_time;
325
327 bool trace;
328
329};
330
332class ReporterDeferred : public Reporter
333{
334public:
335 ReporterDeferred () {};
336 void SuiteStart (const TestSuite& suite) override;
337 void TestStart (const Test& test) override;
338 void ReportFailure (const Failure& failure) override;
339 void TestFinish (const Test& test) override;
340 void Clear () override;
341
342protected:
345 {
346 TestResult ();
347 TestResult (const std::string& suite, const std::string& test);
348
349 std::string suite_name;
350 std::string test_name;
351 std::chrono::milliseconds test_time;
352 std::deque<Failure> failures;
353 };
354
355 std::deque<TestResult> results;
356};
357
359typedef UnitTest::Test* (*Testmaker)();
360
367class TestSuite
368{
369public:
372 {
373 public:
374 Inserter (const std::string& suite,
375 const std::string& test,
376 const std::string& file,
377 int line,
378 Testmaker func);
379
380 private:
381 const std::string test_name,
382 file_name;
383 const int line;
384 const Testmaker maker;
385
386 friend class TestSuite;
387 };
388
389 explicit TestSuite (const std::string& name);
390 void Add (const Inserter* inf);
391 bool IsEnabled () const;
392 void Enable (bool on_off);
393 int RunTests (Reporter& reporter, std::chrono::milliseconds max_runtime);
394
395 std::string name;
396
397private:
398 std::deque <const Inserter*> test_list;
399 std::chrono::milliseconds max_runtime;
400 bool enabled;
401
402 bool SetupCurrentTest (const Inserter* inf);
403 void RunCurrentTest (const Inserter* inf);
404 void TearDownCurrentTest (const Inserter* inf);
405};
406
408class Timer
409{
410public:
411 Timer ();
412 void Start ();
413 std::chrono::milliseconds GetTimeInMs () const;
414 std::chrono::microseconds GetTimeInUs () const;
415
416private:
417 std::chrono::steady_clock::time_point startTime;
418};
419
422{
423public:
424 template<typename R, typename P>
425 TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line);
427
428private:
429 void operator=(TimeConstraint const&) = delete;
430 TimeConstraint (TimeConstraint const&) = delete;
431
432 Timer timer;
433 std::string filename;
434 int line_number;
435 std::chrono::milliseconds tmax;
436};
437
440public:
441 void Add (const std::string& suite, const TestSuite::Inserter* inf);
442 int Run (const std::string& suite, Reporter& reporter, std::chrono::milliseconds max_time);
443 int RunAll (Reporter& reporter, std::chrono::milliseconds max_time);
444 static SuitesList& GetSuitesList ();
445 void Enable (const std::string& suite, bool enable = true);
446
447private:
448
449 std::deque <TestSuite> suites;
450};
451
453struct test_abort : public std::runtime_error
454{
455 test_abort (const char* file_, int line_, const char* msg)
456 : std::runtime_error (msg)
457 , file (file_)
458 , line (line_)
459 {};
460 const char* file;
461 int line;
462};
463
464struct tolerance_not_set : public std::runtime_error
465{
466 tolerance_not_set ()
467 : std::runtime_error ("UnitTest::default_tolerance not set")
468 {};
469};
470
472extern Test* CurrentTest;
473
475extern std::string CurrentSuite;
476
479
482
484int RunAllTests (Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
485
487void DisableSuite (const std::string& suite_name);
488
490void EnableSuite (const std::string& suite_name);
491
493int RunSuite (const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
494
496void ReportFailure (const std::string& filename, int line, const std::string& message);
497
498//-------------------------- Test member functions ----------------------------
500inline
501Test::Test(const std::string& test_name)
502 : name(test_name)
503 , failures(0)
504 , time(0)
505 , time_exempt(false)
506{
507}
508
514inline
516{
517 Timer test_timer;
518 test_timer.Start();
519
520 RunImpl();
521 time = test_timer.GetTimeInMs();
522}
523
527inline
529{
530 failures++;
531}
532
534inline
536{
537 return failures;
538}
539
541inline
542std::chrono::milliseconds Test::test_time_ms () const
543{
544 return time;
545}
546
548inline
549const std::string& Test::test_name () const
550{
551 return name;
552}
553
555inline
557{
558 time_exempt = true;
559}
560
562inline
564{
565 return !time_exempt;
566}
567
568//--------------------- Reporter member functions -----------------------------
569inline
570Reporter::Reporter ()
571 : suite_test_count (0)
572 , suite_failed_count (0)
573 , suite_failures_count (0)
574 , suite_time (0)
575 , total_test_count (0)
576 , total_failed_count (0)
577 , total_failures_count (0)
578 , total_time (0)
579 , suites_count (0)
580 , trace (false)
581{
582}
583
585inline
591
592inline
594{
597}
598
599inline
601{
602}
603
604inline
606{
607 int f = t.failure_count ();
608 if (f)
609 {
614 }
615 auto ms = t.test_time_ms ();
616 suite_time += ms;
617 total_time += ms;
618}
619
621inline
623{
625}
626
627
628inline
630{
631 using namespace std::chrono_literals;
635 suite_time = 0ms;
636
640 total_time = 0ms;
641
642 suites_count = 0;
643}
644
645//------------------- ReporterDeferred member functions -----------------------
647inline
652
654inline
655ReporterDeferred::TestResult::TestResult (const std::string& suite, const std::string& test)
656 : suite_name (suite)
657 , test_name (test)
658 , test_time (0)
659{
660}
661
669inline
671{
672 results.push_back (TestResult (suite.name, std::string()));
673}
674
681inline
683{
684 Reporter::TestStart (test);
685 results.push_back (TestResult (CurrentSuite, test.test_name ()));
686}
687
692inline
694{
695 assert (!results.empty ());
696
697 Reporter::ReportFailure (failure);
698 results.back ().failures.push_back (failure);
699}
700
705inline
707{
709 results.back ().test_time = test.test_time_ms();
710}
711
713{
715 results.clear ();
716}
717
718
719//------------------- TestSuite member functions ------------------------------
720
721inline
722TestSuite::TestSuite (const std::string& name_)
723 : name (name_)
724 , max_runtime (0)
725 , enabled (true)
726{
727}
728
737inline
738void TestSuite::Add (const Inserter* inf)
739{
740 test_list.push_back (inf);
741}
742
752inline
753int TestSuite::RunTests (Reporter& rep, std::chrono::milliseconds maxtime)
754{
757 CurrentReporter = &rep;
758
760 CurrentReporter->SuiteStart (*this);
761 std::deque <const Inserter*>::iterator listp = test_list.begin ();
762 max_runtime = maxtime;
763 while (listp != test_list.end ())
764 {
766 if (SetupCurrentTest (*listp))
767 {
768 RunCurrentTest (*listp);
769 TearDownCurrentTest (*listp);
770 }
772 ++listp;
773 }
775 return CurrentReporter->SuiteFinish (*this);
776}
777
787inline
788bool TestSuite::SetupCurrentTest (const Inserter* inf)
789{
790 bool ok = false;
791 try {
792 CurrentTest = (inf->maker)();
793 ok = true;
794 }
795 catch (UnitTest::test_abort& x)
796 {
797 std::stringstream stream;
798 stream << " Aborted setup of " << inf->test_name << " - " << x.what ();
799 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
800 ReportFailure (x.file, x.line, stream.str ());
801 delete CurrentTest;
802 CurrentTest = 0;
803 }
804 catch (const std::exception& e)
805 {
806 std::stringstream stream;
807 stream << "Unhandled exception: " << e.what ()
808 << " while setting up test " << inf->test_name;
809 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
810 ReportFailure (inf->file_name, inf->line, stream.str ());
811 delete CurrentTest;
812 CurrentTest = 0;
813 }
814 catch (...)
815 {
816 std::stringstream stream;
817 stream << "Setup unhandled exception while setting up test " << inf->test_name;
818 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
819 ReportFailure (inf->file_name, inf->line, stream.str ());
820 delete CurrentTest;
821 CurrentTest = 0;
822 }
823 return ok;
824}
825
827inline
828void TestSuite::RunCurrentTest (const Inserter* inf)
829{
830 assert (CurrentTest);
831 CurrentReporter->TestStart (*CurrentTest);
832
833
834 try {
835 CurrentTest->run ();
836 }
837 catch (UnitTest::test_abort& x)
838 {
839 ReportFailure (x.file, x.line, std::string ("Test aborted: ") + x.what ());
840 }
841 catch (const std::exception& e)
842 {
843 std::stringstream stream;
844 stream << "Unhandled exception: " << e.what ()
845 << " while running test " << inf->test_name;
846 ReportFailure (inf->file_name, inf->line, stream.str ());
847 }
848 catch (...)
849 {
850 std::stringstream stream;
851 stream << "Unhandled exception while running test " << inf->test_name;
852 ReportFailure (inf->file_name, inf->line, stream.str ());
853 }
854
855 auto actual_time = CurrentTest->test_time_ms ();
856 if (CurrentTest->is_time_constraint () && max_runtime.count() && actual_time > max_runtime)
857 {
858 std::stringstream stream;
859 stream << "Global time constraint failed while running test " << inf->test_name
860 << " Expected time <"
861#if _MSVC_LANG >= 202002L
862 << max_runtime
863#else
864 << max_runtime.count () << "ms"
865#endif
866 << "; actual = "
867#if _MSVC_LANG >= 202002L
868 << actual_time;
869#else
870 << actual_time.count ();
871#endif
872 ReportFailure (inf->file_name, inf->line, stream.str ());
873 }
874 CurrentReporter->TestFinish (*CurrentTest);
875}
876
878inline
879void TestSuite::TearDownCurrentTest (const Inserter* inf)
880{
881 try {
882 delete CurrentTest;
883 CurrentTest = 0;
884 }
885 catch (const std::exception& e)
886 {
887 std::stringstream stream;
888 stream << "Unhandled exception: " << e.what ()
889 << " while tearing down test " << inf->test_name;
890 ReportFailure (inf->file_name, inf->line, stream.str ());
891 }
892 catch (...)
893 {
894 std::stringstream stream;
895 stream << "Unhandled exception tearing down test " << inf->test_name;
896 ReportFailure (inf->file_name, inf->line, stream.str ());
897 }
898}
899
901inline
903{
904 return enabled;
905}
906
908inline
909void TestSuite::Enable (bool on_off)
910{
911 enabled = on_off;
912}
913
914//------------------ TestSuite::Inserter --------------------------------------
925inline
926TestSuite::Inserter::Inserter (const std::string& suite, const std::string& test,
927 const std::string& file, int ln, Testmaker func)
928 : test_name (test)
929 , file_name (file)
930 , line (ln)
931 , maker (func)
932{
933 SuitesList::GetSuitesList ().Add (suite, this);
934}
935
936//-----------------Timer member functions -------------------------------------
937inline
938Timer::Timer ()
939{
940}
941
943inline
945{
946 startTime = std::chrono::steady_clock::now ();
947}
948
950inline
951std::chrono::milliseconds Timer::GetTimeInMs () const
952{
953 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
954 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
955}
956
958inline
959std::chrono::microseconds Timer::GetTimeInUs () const
960{
961 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
962 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
963}
964
965//------------------TimeConstraint member functions ---------------------------
966
976template <typename R, typename P>
977TimeConstraint::TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line)
978 : filename (file)
979 , line_number (line)
980 , tmax (t)
981{
982 timer.Start ();
983}
984
989inline
991{
992 std::chrono::milliseconds t = timer.GetTimeInMs ();
993 if (t > tmax)
994 {
995 std::stringstream stream;
996 stream << "Time constraint failed. Expected time <"
997#if _MSVC_LANG >= 202002L
998 << tmax
999#else
1000 << tmax.count () << "ms"
1001#endif
1002 << "; actual = "
1003#if _MSVC_LANG >= 202002L
1004 << t;
1005#else
1006 << t.count () << "ms";
1007#endif
1008 ReportFailure (filename, line_number, stream.str ());
1009 }
1010}
1011
1012
1013//-------------------SuitesList member functions ------------------------------
1014
1023inline
1024void SuitesList::Add (const std::string& suite_name, const TestSuite::Inserter* inf)
1025{
1026 auto p = suites.begin ();
1027 while (p != suites.end () && p->name != suite_name)
1028 p++;
1029
1030 if (p == suites.end ())
1031 {
1032 suites.emplace_back (suite_name);
1033 suites.back ().Add (inf);
1034 }
1035 else
1036 p->Add (inf);
1037}
1038
1048inline
1049int SuitesList::Run (const std::string& suite_name, Reporter& reporter, std::chrono::milliseconds max_time)
1050{
1051 for (auto& s : suites)
1052 {
1053 if (s.name == suite_name)
1054 {
1055 s.RunTests (reporter, max_time);
1056 return reporter.Summary ();
1057 }
1058 }
1059 return -1;
1060}
1061
1069inline
1070int SuitesList::RunAll (Reporter& reporter, std::chrono::milliseconds max_time)
1071{
1072 for (auto& s : suites)
1073 {
1074 if (s.IsEnabled ())
1075 s.RunTests (reporter, max_time);
1076 }
1077
1078 return reporter.Summary ();
1079}
1080
1085inline
1087{
1088 static SuitesList all_suites;
1089 return all_suites;
1090}
1091
1099inline
1100void SuitesList::Enable (const std::string& suite, bool enable)
1101{
1102 for (auto& s : suites)
1103 {
1104 if (s.name == suite)
1105 {
1106 s.Enable (enable);
1107 break;
1108 }
1109 }
1110}
1111
1113
1127inline
1128int RunAllTests (Reporter& rpt, std::chrono::milliseconds max_time)
1129{
1130 rpt.Clear ();
1131 return SuitesList::GetSuitesList ().RunAll (rpt, max_time);
1132}
1133
1145inline
1146int RunSuite (const char* suite_name, Reporter& rpt, std::chrono::milliseconds max_time)
1147{
1148 return SuitesList::GetSuitesList ().Run (suite_name, rpt, max_time);
1149}
1150
1159inline
1160void DisableSuite (const std::string& suite_name)
1161{
1162 SuitesList::GetSuitesList ().Enable (suite_name, false);
1163}
1164
1172inline
1173void EnableSuite (const std::string& suite_name)
1174{
1175 SuitesList::GetSuitesList ().Enable (suite_name, true);
1176}
1177
1187inline
1188void ReportFailure(const std::string& filename, int line, const std::string& message)
1189{
1190 if (CurrentTest)
1191 CurrentTest->failure();
1192 Failure f = { filename, message, line };
1193 CurrentReporter->ReportFailure(f);
1194}
1195
1196} // end of UnitTest namespace
1197
1198
1203inline
1204const char* GetSuiteName ()
1205{
1206 return DEFAULT_SUITE;
1207}
1208
1209#include "reporter_stream.h"
1210#include "reporter_xml.h"
1211#ifdef _WIN32
1212#include "reporter_dbgout.h"
1213#endif
1214#include "checks.h"
1215
1217inline
1219{
1220 static UnitTest::ReporterStream the_default_reporter;
1221 return the_default_reporter;
1222}
1223
1224#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1225 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
1226// In C++ 17 and later we have inline data. TEST_MAIN is not really needed.
1229inline std::string UnitTest::CurrentSuite;
1230#endif
1231
1232#ifdef _MSC_VER
1233#pragma warning ( pop )
1234#endif
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
Definition utpp.h:368
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