Skip to main content

test.cpp File

C++ source file with implementations for the µTest++ test suite methods. More...

Included Headers

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacedetail

Internal implementation details for the µTest++ framework. More...

Description

C++ source file with implementations for the µTest++ test suite methods.

This source file contains the core implementations for the test suite facilities of the µTest++ framework. It provides the logic for constructing, registering, and managing test suites and their associated test cases. The implementation covers initialisation and clean-up routines, execution of test suites and test cases, tracking of successful and failed checks, and integration with the test reporter for structured output.

The design ensures that test suites are non-copyable and non-movable, maintaining unique ownership and consistent state. Flexible support for callable objects enables a wide range of test suite definitions, facilitating expressive and maintainable test organisation across embedded and general C++ projects.

All definitions reside within the micro_os_plus::micro_test_plus namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

This file must be included when building the µTest++ library.

File Listing

The file content with the documentation metadata removed is:

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 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
299 top_suite::top_suite (const char* name, class runner& runner)
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// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.