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#if !defined(_CRT_SECURE_NO_WARNINGS)
19#define _CRT_SECURE_NO_WARNINGS 1
20#endif
21//Acrobatics to leave out winsock.h (for the benefit of winsock2.h)
22#ifndef _WINSOCKAPI_
23#define _WINSOCKAPI_
24#define MUST_UNDEF_WINSOCK
25#endif
26
27#include <windows.h>
28
29#ifdef MUST_UNDEF_WINSOCK
30#undef _WINSOCKAPI_
31#undef MUST_UNDEF_WINSOCK
32#endif
33
34#endif
35
36#include <string>
37#include <deque>
38#include <stdexcept>
39#include <sstream>
40#include <cassert>
41#include <chrono>
42#ifndef _WIN32
43#include <sys/stat.h>
44#include <unistd.h>
45#endif
46
47// --------------- Global configuration options -------------------------------
48#define UTPP_VERSION "2.2.0"
49
50// --------------- end of configuration options -------------------------------
51
52namespace UnitTest {
53
55const size_t MAX_MESSAGE_SIZE = 1024;
56
57}
59#define DEFAULT_SUITE "DefaultSuite"
60
61//------------------------------ Test macros ----------------------------------
62#ifdef TEST
63#error Macro TEST is already defined
64#endif
65
66#ifdef TEST_FIXTURE
67#error Macro TEST_FIXTURE is already defined
68#endif
69
70#ifdef SUITE
71#error Macro SUITE is already defined
72#endif
73
74/* VC doesn't define the proper value for __cplusplus but _MSVC_LANG is correct.
75 Fake it here for non-MS compilers
76*/
77#ifndef _MSVC_LANG
78#define _MSVC_LANG __cplusplus
79#endif
80
96
97#if _MSVC_LANG < 201703L
98#define TEST_MAIN(ARGC, ARGV) \
99UnitTest::Test *UnitTest::CurrentTest; \
100UnitTest::Reporter *UnitTest::CurrentReporter; \
101double UnitTest::default_tolerance; \
102std::string UnitTest::CurrentSuite; \
103int main (ARGC,ARGV)
104#else
105#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
106#endif
107
108
113
124#define SUITE(Name) \
125 namespace Suite##Name \
126 { \
127 inline char const* GetSuiteName () { return #Name ; } \
128 } \
129 namespace Suite##Name
130
137#define TEST(Name) \
138 class Test##Name : public UnitTest::Test \
139 { \
140 public: \
141 Test##Name() : Test(#Name) {} \
142 private: \
143 void RunImpl() override; \
144 }; \
145 UnitTest::Test* Name##_maker() {return new Test##Name; } \
146 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
147 Name##_maker); \
148 void Test##Name::RunImpl()
149
150
162#define TEST_FIXTURE(Fixture, Name) \
163 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
164 { \
165 public: \
166 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
167 private: \
168 void RunImpl() override; \
169 }; \
170 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
171 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
172 __FILE__, __LINE__, Name##_maker); \
173 void Fixture##Name##Helper::RunImpl()
174
175#ifdef ABORT
176#error Macro ABORT is already defined
177#endif
178
185#define ABORT(value) \
186 do \
187 { \
188 if (UnitTest::Check(value)) \
189 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
190 } while (0)
191
192#ifdef ABORT_EX
193#error Macro ABORT_EX is already defined
194#endif
195
202#define ABORT_EX(value, ...) \
203 do \
204 { \
205 if (UnitTest::Check(value)) { \
206 char message[UnitTest::MAX_MESSAGE_SIZE]; \
207 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
208 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
209 } \
210 } while (0)
211
213
216
223#define UTPP_TIME_CONSTRAINT(tmax) \
224 UnitTest::TimeConstraint unitTest__timeConstraint__(tmax, __FILE__, __LINE__)
225
231#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
232
234
235namespace UnitTest {
236
237// forward declarations
238struct Failure;
239class TestSuite;
240
241void ReportFailure(const std::string& filename, int line, const std::string& message);
242
244class Test
245{
246public:
247 Test (const std::string& testName);
248 virtual ~Test() {};
249 void no_time_constraint ();
250 bool is_time_constraint () const;
251
252 int failure_count () const;
253 std::chrono::milliseconds test_time_ms () const;
254 const std::string& test_name () const;
255
256 void failure ();
257 void run ();
258
260 virtual void RunImpl () {};
261
262protected:
263 std::string name;
265 std::chrono::milliseconds time;
267
268private:
269 Test (Test const&) = delete;
270 Test& operator =(Test const&) = delete;
271 friend class TestSuite;
272};
273
276{
277 std::string filename;
278 std::string message;
280};
281
282
284class Reporter
285{
286public:
287 Reporter ();
288 virtual ~Reporter () {};
289
291 void SetTrace (bool on_off) { trace = on_off; }
292
294 virtual void SuiteStart (const TestSuite& suite);
295
297 virtual void TestStart (const Test& test);
298
300 virtual void ReportFailure (const Failure& failure);
301
303 virtual void TestFinish (const Test& test);
304
306 virtual int SuiteFinish (const TestSuite& suite);
307
309 virtual int Summary () { return total_failed_count; }
310
312 virtual void Clear ();
313
314protected:
318 std::chrono::milliseconds suite_time;
319
323 std::chrono::milliseconds total_time;
324
326 bool trace;
327
328};
329
331class ReporterDeferred : public Reporter
332{
333public:
334 ReporterDeferred () {};
335 void SuiteStart (const TestSuite& suite) override;
336 void TestStart (const Test& test) override;
337 void ReportFailure (const Failure& failure) override;
338 void TestFinish (const Test& test) override;
339 void Clear () override;
340
341protected:
344 {
345 TestResult ();
346 TestResult (const std::string& suite, const std::string& test);
347
348 std::string suite_name;
349 std::string test_name;
350 std::chrono::milliseconds test_time;
351 std::deque<Failure> failures;
352 };
353
354 std::deque<TestResult> results;
355};
356
358typedef UnitTest::Test* (*Testmaker)();
359
366class TestSuite
367{
368public:
371 {
372 public:
373 Inserter (const std::string& suite,
374 const std::string& test,
375 const std::string& file,
376 int line,
377 Testmaker func);
378
379 private:
380 const std::string test_name,
381 file_name;
382 const int line;
383 const Testmaker maker;
384
385 friend class TestSuite;
386 };
387
388 explicit TestSuite (const std::string& name);
389 void Add (const Inserter* inf);
390 bool IsEnabled () const;
391 void Enable (bool on_off);
392 int RunTests (Reporter& reporter, std::chrono::milliseconds max_runtime);
393
394 std::string name;
395
396private:
397 std::deque <const Inserter*> test_list;
398 std::chrono::milliseconds max_runtime;
399 bool enabled;
400
401 bool SetupCurrentTest (const Inserter* inf);
402 void RunCurrentTest (const Inserter* inf);
403 void TearDownCurrentTest (const Inserter* inf);
404};
405
407class Timer
408{
409public:
410 Timer ();
411 void Start ();
412 std::chrono::milliseconds GetTimeInMs () const;
413 std::chrono::microseconds GetTimeInUs () const;
414
415private:
416 std::chrono::steady_clock::time_point startTime;
417};
418
421{
422public:
423 template<typename R, typename P>
424 TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line);
426
427private:
428 void operator=(TimeConstraint const&) = delete;
429 TimeConstraint (TimeConstraint const&) = delete;
430
431 Timer timer;
432 std::string filename;
433 int line_number;
434 std::chrono::milliseconds tmax;
435};
436
439public:
440 void Add (const std::string& suite, const TestSuite::Inserter* inf);
441 int Run (const std::string& suite, Reporter& reporter, std::chrono::milliseconds max_time);
442 int RunAll (Reporter& reporter, std::chrono::milliseconds max_time);
443 static SuitesList& GetSuitesList ();
444 void Enable (const std::string& suite, bool enable = true);
445
446private:
447
448 std::deque <TestSuite> suites;
449};
450
452struct test_abort : public std::runtime_error
453{
454 test_abort (const char* file_, int line_, const char* msg)
455 : std::runtime_error (msg)
456 , file (file_)
457 , line (line_)
458 {};
459 const char* file;
460 int line;
461};
462
463struct tolerance_not_set : public std::runtime_error
464{
465 tolerance_not_set ()
466 : std::runtime_error ("UnitTest::default_tolerance not set")
467 {};
468};
469
471extern Test* CurrentTest;
472
474extern std::string CurrentSuite;
475
478
481
483int RunAllTests (Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
484
486void DisableSuite (const std::string& suite_name);
487
489void EnableSuite (const std::string& suite_name);
490
492int RunSuite (const char *suite_name, Reporter& rpt = GetDefaultReporter (), std::chrono::milliseconds max_time = std::chrono::milliseconds{ 0 });
493
495void ReportFailure (const std::string& filename, int line, const std::string& message);
496
497//-------------------------- Test member functions ----------------------------
499inline
500Test::Test(const std::string& test_name)
501 : name(test_name)
502 , failures(0)
503 , time(0)
504 , time_exempt(false)
505{
506}
507
513inline
515{
516 Timer test_timer;
517 test_timer.Start();
518
519 RunImpl();
520 time = test_timer.GetTimeInMs();
521}
522
526inline
528{
529 failures++;
530}
531
533inline
535{
536 return failures;
537}
538
540inline
541std::chrono::milliseconds Test::test_time_ms () const
542{
543 return time;
544}
545
547inline
548const std::string& Test::test_name () const
549{
550 return name;
551}
552
554inline
556{
557 time_exempt = true;
558}
559
561inline
563{
564 return !time_exempt;
565}
566
567//--------------------- Reporter member functions -----------------------------
568inline
569Reporter::Reporter ()
570 : suite_test_count (0)
571 , suite_failed_count (0)
572 , suite_failures_count (0)
573 , suite_time (0)
574 , total_test_count (0)
575 , total_failed_count (0)
576 , total_failures_count (0)
577 , total_time (0)
578 , suites_count (0)
579 , trace (false)
580{
581}
582
584inline
590
591inline
593{
596}
597
598inline
600{
601}
602
603inline
605{
606 int f = t.failure_count ();
607 if (f)
608 {
613 }
614 auto ms = t.test_time_ms ();
615 suite_time += ms;
616 total_time += ms;
617}
618
620inline
622{
624}
625
626
627inline
629{
630 using namespace std::chrono_literals;
634 suite_time = 0ms;
635
639 total_time = 0ms;
640
641 suites_count = 0;
642}
643
644//------------------- ReporterDeferred member functions -----------------------
646inline
651
653inline
654ReporterDeferred::TestResult::TestResult (const std::string& suite, const std::string& test)
655 : suite_name (suite)
656 , test_name (test)
657 , test_time (0)
658{
659}
660
668inline
670{
671 results.push_back (TestResult (suite.name, std::string()));
672}
673
680inline
682{
683 Reporter::TestStart (test);
684 results.push_back (TestResult (CurrentSuite, test.test_name ()));
685}
686
691inline
693{
694 assert (!results.empty ());
695
696 Reporter::ReportFailure (failure);
697 results.back ().failures.push_back (failure);
698}
699
704inline
706{
708 results.back ().test_time = test.test_time_ms();
709}
710
712{
714 results.clear ();
715}
716
717
718//------------------- TestSuite member functions ------------------------------
719
720inline
721TestSuite::TestSuite (const std::string& name_)
722 : name (name_)
723 , max_runtime (0)
724 , enabled (true)
725{
726}
727
736inline
737void TestSuite::Add (const Inserter* inf)
738{
739 test_list.push_back (inf);
740}
741
751inline
752int TestSuite::RunTests (Reporter& rep, std::chrono::milliseconds maxtime)
753{
756 CurrentReporter = &rep;
757
759 CurrentReporter->SuiteStart (*this);
760 std::deque <const Inserter*>::iterator listp = test_list.begin ();
761 max_runtime = maxtime;
762 while (listp != test_list.end ())
763 {
765 if (SetupCurrentTest (*listp))
766 {
767 RunCurrentTest (*listp);
768 TearDownCurrentTest (*listp);
769 }
771 ++listp;
772 }
774 return CurrentReporter->SuiteFinish (*this);
775}
776
786inline
787bool TestSuite::SetupCurrentTest (const Inserter* inf)
788{
789 bool ok = false;
790 try {
791 CurrentTest = (inf->maker)();
792 ok = true;
793 }
794 catch (UnitTest::test_abort& x)
795 {
796 std::stringstream stream;
797 stream << " Aborted setup of " << inf->test_name << " - " << x.what ();
798 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
799 ReportFailure (x.file, x.line, stream.str ());
800 delete CurrentTest;
801 CurrentTest = 0;
802 }
803 catch (const std::exception& e)
804 {
805 std::stringstream stream;
806 stream << "Unhandled exception: " << e.what ()
807 << " while setting up test " << inf->test_name;
808 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
809 ReportFailure (inf->file_name, inf->line, stream.str ());
810 delete CurrentTest;
811 CurrentTest = 0;
812 }
813 catch (...)
814 {
815 std::stringstream stream;
816 stream << "Setup unhandled exception while setting up test " << inf->test_name;
817 CurrentTest = new Test (inf->test_name); //mock-up to keep ReportFailure happy
818 ReportFailure (inf->file_name, inf->line, stream.str ());
819 delete CurrentTest;
820 CurrentTest = 0;
821 }
822 return ok;
823}
824
826inline
827void TestSuite::RunCurrentTest (const Inserter* inf)
828{
829 assert (CurrentTest);
830 CurrentReporter->TestStart (*CurrentTest);
831
832
833 try {
834 CurrentTest->run ();
835 }
836 catch (UnitTest::test_abort& x)
837 {
838 ReportFailure (x.file, x.line, std::string ("Test aborted: ") + x.what ());
839 }
840 catch (const std::exception& e)
841 {
842 std::stringstream stream;
843 stream << "Unhandled exception: " << e.what ()
844 << " while running test " << inf->test_name;
845 ReportFailure (inf->file_name, inf->line, stream.str ());
846 }
847 catch (...)
848 {
849 std::stringstream stream;
850 stream << "Unhandled exception while running test " << inf->test_name;
851 ReportFailure (inf->file_name, inf->line, stream.str ());
852 }
853
854 auto actual_time = CurrentTest->test_time_ms ();
855 if (CurrentTest->is_time_constraint () && max_runtime.count() && actual_time > max_runtime)
856 {
857 std::stringstream stream;
858 stream << "Global time constraint failed while running test " << inf->test_name
859 << " Expected time <" << max_runtime << "; actual = " << actual_time;
860
861 ReportFailure (inf->file_name, inf->line, stream.str ());
862 }
863 CurrentReporter->TestFinish (*CurrentTest);
864}
865
867inline
868void TestSuite::TearDownCurrentTest (const Inserter* inf)
869{
870 try {
871 delete CurrentTest;
872 CurrentTest = 0;
873 }
874 catch (const std::exception& e)
875 {
876 std::stringstream stream;
877 stream << "Unhandled exception: " << e.what ()
878 << " while tearing down test " << inf->test_name;
879 ReportFailure (inf->file_name, inf->line, stream.str ());
880 }
881 catch (...)
882 {
883 std::stringstream stream;
884 stream << "Unhandled exception tearing down test " << inf->test_name;
885 ReportFailure (inf->file_name, inf->line, stream.str ());
886 }
887}
888
890inline
892{
893 return enabled;
894}
895
897inline
898void TestSuite::Enable (bool on_off)
899{
900 enabled = on_off;
901}
902
903//------------------ TestSuite::Inserter --------------------------------------
914inline
915TestSuite::Inserter::Inserter (const std::string& suite, const std::string& test,
916 const std::string& file, int ln, Testmaker func)
917 : test_name (test)
918 , file_name (file)
919 , line (ln)
920 , maker (func)
921{
922 SuitesList::GetSuitesList ().Add (suite, this);
923}
924
925//-----------------Timer member functions -------------------------------------
926inline
927Timer::Timer ()
928{
929}
930
932inline
934{
935 startTime = std::chrono::steady_clock::now ();
936}
937
939inline
940std::chrono::milliseconds Timer::GetTimeInMs () const
941{
942 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
943 return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedTime);
944}
945
947inline
948std::chrono::microseconds Timer::GetTimeInUs () const
949{
950 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
951 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime);
952}
953
954//------------------TimeConstraint member functions ---------------------------
955
965template <typename R, typename P>
966TimeConstraint::TimeConstraint (std::chrono::duration<R, P> t, const char* file, int line)
967 : filename (file)
968 , line_number (line)
969 , tmax (t)
970{
971 timer.Start ();
972}
973
978inline
980{
981 std::chrono::milliseconds t = timer.GetTimeInMs ();
982 if (t > tmax)
983 {
984 std::stringstream stream;
985 stream << "Time constraint failed. Expected time <" << tmax << "; actual = " << t;
986
987 ReportFailure (filename, line_number, stream.str ());
988 }
989}
990
991
992//-------------------SuitesList member functions ------------------------------
993
1002inline
1003void SuitesList::Add (const std::string& suite_name, const TestSuite::Inserter* inf)
1004{
1005 auto p = suites.begin ();
1006 while (p != suites.end () && p->name != suite_name)
1007 p++;
1008
1009 if (p == suites.end ())
1010 {
1011 suites.emplace_back (suite_name);
1012 suites.back ().Add (inf);
1013 }
1014 else
1015 p->Add (inf);
1016}
1017
1027inline
1028int SuitesList::Run (const std::string& suite_name, Reporter& reporter, std::chrono::milliseconds max_time)
1029{
1030 for (auto& s : suites)
1031 {
1032 if (s.name == suite_name)
1033 {
1034 s.RunTests (reporter, max_time);
1035 return reporter.Summary ();
1036 }
1037 }
1038 return -1;
1039}
1040
1048inline
1049int SuitesList::RunAll (Reporter& reporter, std::chrono::milliseconds max_time)
1050{
1051 for (auto& s : suites)
1052 {
1053 if (s.IsEnabled ())
1054 s.RunTests (reporter, max_time);
1055 }
1056
1057 return reporter.Summary ();
1058}
1059
1064inline
1066{
1067 static SuitesList all_suites;
1068 return all_suites;
1069}
1070
1078inline
1079void SuitesList::Enable (const std::string& suite, bool enable)
1080{
1081 for (auto& s : suites)
1082 {
1083 if (s.name == suite)
1084 {
1085 s.Enable (enable);
1086 break;
1087 }
1088 }
1089}
1090
1092
1106inline
1107int RunAllTests (Reporter& rpt, std::chrono::milliseconds max_time)
1108{
1109 rpt.Clear ();
1110 return SuitesList::GetSuitesList ().RunAll (rpt, max_time);
1111}
1112
1124inline
1125int RunSuite (const char* suite_name, Reporter& rpt, std::chrono::milliseconds max_time)
1126{
1127 return SuitesList::GetSuitesList ().Run (suite_name, rpt, max_time);
1128}
1129
1138inline
1139void DisableSuite (const std::string& suite_name)
1140{
1141 SuitesList::GetSuitesList ().Enable (suite_name, false);
1142}
1143
1151inline
1152void EnableSuite (const std::string& suite_name)
1153{
1154 SuitesList::GetSuitesList ().Enable (suite_name, true);
1155}
1156
1166inline
1167void ReportFailure(const std::string& filename, int line, const std::string& message)
1168{
1169 if (CurrentTest)
1170 CurrentTest->failure();
1171 Failure f = { filename, message, line };
1172 CurrentReporter->ReportFailure(f);
1173}
1174
1175} // end of UnitTest namespace
1176
1177
1182inline
1183const char* GetSuiteName ()
1184{
1185 return DEFAULT_SUITE;
1186}
1187
1188#include "reporter_stream.h"
1189#include "reporter_xml.h"
1190#ifdef _WIN32
1191#include "reporter_dbgout.h"
1192#endif
1193#include "checks.h"
1194
1196inline
1198{
1199 static UnitTest::ReporterStream the_default_reporter;
1200 return the_default_reporter;
1201}
1202
1203#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1204 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
1205// In C++ 17 and later we have inline data. TEST_MAIN is not really needed.
1208inline std::string UnitTest::CurrentSuite;
1209#endif
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:711
void TestStart(const Test &test) override
Definition utpp.h:681
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:354
void ReportFailure(const Failure &failure) override
Definition utpp.h:692
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:669
void TestFinish(const Test &test) override
Definition utpp.h:705
Abstract base for all reporters.
Definition utpp.h:285
virtual void Clear()
Reset all statistics.
Definition utpp.h:628
int suites_count
number of suites ran
Definition utpp.h:325
int suite_failures_count
number of failures in suite
Definition utpp.h:317
int total_failures_count
total number of failures
Definition utpp.h:322
int suite_test_count
number of tests in suite
Definition utpp.h:315
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:585
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:592
std::chrono::milliseconds total_time
total running time in milliseconds
Definition utpp.h:323
bool trace
true if tracing is enabled
Definition utpp.h:326
int suite_failed_count
number of failed tests in suite
Definition utpp.h:316
std::chrono::milliseconds suite_time
total suite running time in milliseconds
Definition utpp.h:318
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:604
int total_test_count
total number of tests
Definition utpp.h:320
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:599
virtual int Summary()
Generate results report.
Definition utpp.h:309
int total_failed_count
total number of failed tests
Definition utpp.h:321
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:621
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:291
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:438
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1003
int Run(const std::string &suite, Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1028
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1079
int RunAll(Reporter &reporter, std::chrono::milliseconds max_time)
Definition utpp.h:1049
static SuitesList & GetSuitesList()
Definition utpp.h:1065
Representation of a test case.
Definition utpp.h:245
std::chrono::milliseconds time
Run time.
Definition utpp.h:265
int failures
Number of failures in this test.
Definition utpp.h:264
void run()
Definition utpp.h:514
void failure()
Definition utpp.h:527
const std::string & test_name() const
Return test name.
Definition utpp.h:548
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:534
virtual void RunImpl()
Actual body of test.
Definition utpp.h:260
Test(const std::string &testName)
Constructor.
Definition utpp.h:500
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:562
std::string name
Name of this test.
Definition utpp.h:263
bool time_exempt
true if exempt from time constraints
Definition utpp.h:266
std::chrono::milliseconds test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:541
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:555
Constructor of this objects inserts the test in suite.
Definition utpp.h:371
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:915
Definition utpp.h:367
std::string name
Suite name.
Definition utpp.h:394
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:891
void Add(const Inserter *inf)
Definition utpp.h:737
int RunTests(Reporter &reporter, std::chrono::milliseconds max_runtime)
Definition utpp.h:752
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:898
TimeConstraint(std::chrono::duration< R, P > t, const char *file, int line)
Definition utpp.h:966
~TimeConstraint()
Definition utpp.h:979
An object that can be interrogated to get elapsed time.
Definition utpp.h:408
std::chrono::microseconds GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:948
std::chrono::milliseconds GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:940
void Start()
Record starting time.
Definition utpp.h:933
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1152
int RunAllTests(Reporter &rpt=GetDefaultReporter(), std::chrono::milliseconds max_time=std::chrono::milliseconds{ 0 })
Run all tests from all test suites.
Definition utpp.h:1107
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:1125
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1139
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:276
int line_number
Line number where the failure has occurred.
Definition utpp.h:279
std::string message
Description of failure.
Definition utpp.h:278
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:277
Test results including all failure messages
Definition utpp.h:344
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:351
std::string suite_name
suite name
Definition utpp.h:348
std::string test_name
test name
Definition utpp.h:349
std::chrono::milliseconds test_time
test running time in milliseconds
Definition utpp.h:350
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:647
Exception thrown by ABORT macro.
Definition utpp.h:453
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:358
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:59
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1167
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:1197
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:55
const char * GetSuiteName()
Definition utpp.h:1183