micro-test-plus 5.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
test.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
43
44// ----------------------------------------------------------------------------
45
48
49#if __has_include("micro-os-plus/diag/trace.h")
50#include "micro-os-plus/diag/trace.h"
51#endif // __has_include("micro-os-plus/diag/trace.h")
52
53// ----------------------------------------------------------------------------
54
55#if defined(__GNUC__)
56#if defined(__clang__)
57#pragma clang diagnostic ignored "-Wunknown-warning-option"
58#pragma clang diagnostic ignored "-Wc++98-compat"
59#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
60#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
61#else // GCC only
62#pragma GCC diagnostic ignored "-Wredundant-tags"
63#pragma GCC diagnostic ignored "-Wsuggest-final-types"
64#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
65#endif // defined(__clang__)
66#endif // defined(__GNUC__)
67
68// ============================================================================
69
70#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
71
72// ----------------------------------------------------------------------------
73
75{
76 namespace detail
77 {
78 // ========================================================================
79
87 {
88#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
89 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
90#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
91 }
92
100 {
101#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
102 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
103#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
104 }
105
106 // ========================================================================
107
119 size_t own_index)
121 {
122#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
123 trace::printf ("%s '%s' %zu\n", __PRETTY_FUNCTION__, name, own_index_);
124#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
125 }
126
135 {
136#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
137 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
138#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
139
140 // children_subtests_ holds unique_ptrs; destroyed automatically.
141 }
142
148 [[nodiscard]] reporter&
149 runnable_base::reporter (void) const noexcept
150 {
151 return runner_.reporter ();
152 }
153
160 [[noreturn]] void
162 {
163 runner_.abort (sl);
164 }
165
176 void
178 std::unique_ptr<class subtest> child_test, suite& suite)
179 {
180 // Transfer ownership into the vector.
181 children_subtests_.push_back (std::move (child_test));
182
183 // Run the child test case immediately.
184 class subtest& subtest = *children_subtests_.back ();
185 subtest.run ();
186
187 // This test executed one more subtest.
188#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
189 trace::printf ("%s subtest '%s' executed one more subtest\n",
190 __PRETTY_FUNCTION__, name ());
191#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
193 // Do not accumulate the totals from the child test into the current test
194 // totals, each subtest shows only its counters.
195
196#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
197 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
198 suite.name ());
199#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
200
201 // Accumulate the totals from the child test into the suite totals.
202 suite.totals () += subtest.totals ();
203 }
204 } // namespace detail
205
206 // ==========================================================================
207
218 {
219#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
220 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
221#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
222 }
223
229 void
231 {
232#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
233 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
234#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
235
236 class reporter& reporter = this->reporter ();
237
238 // For now, subtests do not record the time.
239 // this->timings.timestamp_begin ();
240 reporter.begin_subtest (*this);
241
242 // Invoke the callable, passing the self reference followed by the variadic
243 // arguments.
244 callable_ (*this);
245
246 // this->timings.timestamp_end ();
247 reporter.end_subtest (*this);
248 }
249
250 // ==========================================================================
251
259 {
260#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
261 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
262#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
263 }
264
270 void
272 {
273#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
274 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
275#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
276
277 class reporter& reporter = this->reporter ();
278
279 this->timings ().timestamp_begin ();
280 reporter.begin_suite (*this);
281
282 // Invoke the callable, passing the self reference followed by the variadic
283 // arguments.
284 callable_ (*this);
285
286 this->timings ().timestamp_end ();
287 reporter.end_suite (*this);
288 }
289
290 // ==========================================================================
291
300 : suite{ name, runner, [] (suite&) noexcept {} }
301 {
302#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
303 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
304#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
305
306 own_index (1);
307 }
308
316 {
317#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
318 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
319#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
320 }
321
322 // ==========================================================================
323
331 {
332#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
333 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
334#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
335 }
336
344 void
346 {
347#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED)
348 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
349#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_ENABLED
350
351 class reporter& reporter = this->reporter ();
352
353 this->timings ().timestamp_begin ();
354 reporter.begin_suite (*this);
355
356 static_callable_ (*this);
357
358 this->timings ().timestamp_end ();
359 reporter.end_suite (*this);
360 }
361
362 // --------------------------------------------------------------------------
363} // namespace micro_os_plus::micro_test_plus
364
365// ----------------------------------------------------------------------------
366
367#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
369// ----------------------------------------------------------------------------
class runner & runner(void) const noexcept
Gets the test runner associated with this test runnable.
runnable_base(const char *name, runner &runner, size_t own_index)
Constructs a runnable_base with a name, runner, and index.
Definition test.cpp:118
void after_subtest_create_(std::unique_ptr< subtest > child_test, suite &suite)
Registers a newly constructed child subtest and executes it immediately.
Definition test.cpp:177
size_t own_index_
The test index, counting from 1.
Definition test.h:415
size_t own_index() const noexcept
Returns the positional index of this object within its parent.
class reporter & reporter(void) const noexcept
Gets the test reporter associated with this test runnable.
Definition test.cpp:149
class runner & runner_
Reference to the test runner that owns this object.
Definition test.h:410
virtual ~runnable_base() override
Virtual destructor.
Definition test.cpp:134
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution via the owning runner.
Definition test.cpp:161
std::vector< std::unique_ptr< subtest > > children_subtests_
Owning collection of direct child subtests.
Definition test.h:436
void increment_executed_subtests(size_t count=1) noexcept
Increments the executed-subtests counter.
const char * name(void) const noexcept
Gets the node name.
virtual ~test_node()
Virtual destructor for the test_node class.
Definition test.cpp:99
runner_totals & totals() noexcept
Gets the totals for the test.
test_node(const char *name)
Constructs a test node.
Definition test.cpp:86
const char * name_
The test node name.
Definition test.h:233
void timestamp_begin(void) noexcept
Records the begin timestamp using the current system clock.
Definition timings.cpp:102
void timestamp_end(void) noexcept
Records the end timestamp using the current system clock.
Definition timings.cpp:135
Local implementation of source location information for diagnostics.
Definition reflection.h:150
Reporter to display test results, including operand values and types for failures.
Definition reporter.h:195
virtual void begin_subtest(subtest &subtest)=0
Mark the beginning of a subtest.
virtual void begin_suite(suite &suite)=0
Mark the beginning of a test suite.
virtual void end_suite(suite &suite)=0
Mark the end of a test suite.
The test runner for the µTest++ framework.
Definition runner.h:144
std::function< void(static_suite &)> static_callable_
Callable storing the static suite body and any bound arguments. Invoked with a reference to the concr...
Definition test.h:999
virtual void run(void) override
Executes the static suite body using the stored static callable.
Definition test.cpp:345
virtual ~static_suite() override
Virtual destructor.
Definition test.cpp:330
A named, runnable test case that lives inside a suite.
Definition test.h:551
virtual ~subtest() override
Virtual destructor.
Definition test.cpp:217
virtual void run(void) override
Executes the subtest body by invoking the stored callable.
Definition test.cpp:230
A named, runnable test suite registered with the test runner.
Definition test.h:723
suite(const char *name, class runner &runner, Callable_T &&callable, Args_T &&... arguments)
Constructs a suite with a name, runner, and callable body.
virtual void run(void) override
Executes the suite body by invoking the stored callable.
Definition test.cpp:271
detail::timestamps & timings() noexcept
Gets the timings for this suite.
virtual ~suite() override
Virtual destructor.
Definition test.cpp:258
void name(const char *new_name) noexcept
Sets the name of the top-level suite.
top_suite(const char *name, class runner &runner)
Constructs the top-level suite with a name and runner reference.
Definition test.cpp:299
virtual ~top_suite() override
Virtual destructor.
Definition test.cpp:315
Internal implementation details for the µTest++ framework.
Primary namespace for the µTest++ testing framework.
C++ header file with declarations for the µTest++ test runner.
C++ header file with declarations for the µTest++ test suite.