Skip to main content

test-runner.cpp File

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

Included Headers

#include <micro-os-plus/micro-test-plus.h> #include <stdio.h> #include <stdlib.h> #include <vector>

Namespaces Index

namespacemicro_os_plus

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

namespacemicro_test_plus

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

Description

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

This source file contains the core implementations for the test runner facilities of the µTest++ framework. It provides the logic for initialising the test environment, registering and managing test suites, handling command-line arguments, orchestrating test execution, and determining the overall test result. The implementation supports automated discovery and execution of test suites, flexible verbosity control, and robust mechanisms for aborting test execution in critical scenarios.

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
38
39// ----------------------------------------------------------------------------
40
41#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
42#include <micro-os-plus/config.h>
43#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
44
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <vector>
50
51// ----------------------------------------------------------------------------
52
53#pragma GCC diagnostic ignored "-Waggregate-return"
54#if defined(__clang__)
55#pragma clang diagnostic ignored "-Wc++98-compat"
56#pragma clang diagnostic ignored "-Wc++98-c++11-c++14-compat"
57#pragma clang diagnostic ignored "-Wunknown-warning-option"
58#endif
59
61{
62 // --------------------------------------------------------------------------
63
74 {
75#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
76 printf ("%s\n", __PRETTY_FUNCTION__);
77#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
78 }
79
80#pragma GCC diagnostic push
81#if defined(__clang__)
82#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
83#endif
96 void
97 test_runner::initialize (int argc, char* argv[], const char* name)
98 {
99#if defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
100 printf ("%s\n", __PRETTY_FUNCTION__);
101#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
102
103#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
104#if defined(_WIN32)
105 timespec_get (&begin_time, TIME_UTC);
106#else
107 clock_gettime (CLOCK_MONOTONIC, &begin_time);
108#endif
109#endif
110
111 argc_ = argc;
112 argv_ = argv;
113
115
116#if !(defined(MICRO_OS_PLUS_INCLUDE_STARTUP) && defined(MICRO_OS_PLUS_TRACE))
117#if defined(MICRO_OS_PLUS_DEBUG)
118 printf ("argv[");
119 for (int i = 0; i < argc; ++i)
120 {
121 if (i > 0)
122 {
123 printf (", ");
124 }
125 printf ("'%s'", argv[i]);
126 }
127 puts ("]");
128#endif // defined(MICRO_OS_PLUS_DEBUG)
129#endif // !defined(MICRO_OS_PLUS_INCLUDE_STARTUP)
130
132 const char* reporter_name = "tap";
133 for (int i = 0; i < argc; ++i)
134 {
135 if (strcmp (argv[i], "--verbose") == 0)
136 {
138 }
139 else if (strcmp (argv[i], "--quiet") == 0)
140 {
142 }
143 else if (strcmp (argv[i], "--silent") == 0)
144 {
146 }
147 else if (strncmp (argv[i], "--reporter=", 11) == 0)
148 {
149 reporter_name = argv[i] + 11;
150 }
151 }
152
153 // Initialize and configure the reporter.
154 if (strcmp (reporter_name, "basic") == 0)
155 {
157 }
158 else if (strcmp (reporter_name, "tap") == 0)
159 {
161 }
162 else
163 {
164 fprintf (stderr, "error: unknown reporter '%s'\n", reporter_name);
165 exit (1);
166 }
167 reporter->verbosity = verbosity;
168
169 // ------------------------------------------------------------------------
170
171#if !(defined(MICRO_OS_PLUS_INCLUDE_STARTUP) && defined(MICRO_OS_PLUS_TRACE))
173 {
174#if defined(__clang__)
175 printf ("Built with clang " __VERSION__);
176#elif defined(__GNUC__)
177 printf ("Built with GCC " __VERSION__);
178#elif defined(_MSC_VER)
179 // https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
180 printf ("Built with MSVC %d", _MSC_VER);
181#else
182 printf ("Built with an unknown compiler");
183#endif
184#if !(defined(__APPLE__) || defined(__linux__) || defined(__unix__) \
185 || defined(WIN32))
186// This is relevant only on bare-metal.
187#if defined(__ARM_PCS_VFP) || defined(__ARM_FP)
188 printf (", with FP");
189#else
190 printf (", no FP");
191#endif
192#endif
193#if defined(__EXCEPTIONS)
194 printf (", with exceptions");
195#else
196 printf (", no exceptions");
197#endif
198#if defined(MICRO_OS_PLUS_DEBUG)
199 printf (", with MICRO_OS_PLUS_DEBUG");
200#endif
201 puts (".");
202 }
203#endif // !defined(MICRO_OS_PLUS_INCLUDE_STARTUP)
204
205 // ------------------------------------------------------------------------
206
209
210 // Deferred to first test case or test suite end, to allow various
211 // initialisations to display their messages.
212 // reporter->begin_test (test_suites_count ());
213 // default_test_suite_->begin_test_suite ();
214 }
215#pragma GCC diagnostic pop
216
217 int
219 {
220 bool was_successful = true;
221
222 if (!default_test_suite->unused ())
223 {
224 default_test_suite->end_test_suite ();
225 was_successful = default_test_suite->was_successful ();
226
227 totals.successful_checks += default_test_suite->successful_checks ();
228 totals.failed_checks += default_test_suite->failed_checks ();
229 totals.test_cases_count += default_test_suite->test_cases_count ();
230 }
231
232 if (test_suites != nullptr)
233 {
234 for (auto test_suite : *test_suites)
235 {
237
239 test_suite->run ();
241
242 was_successful &= test_suite->was_successful ();
243
244 totals.successful_checks += test_suite->successful_checks ();
245 totals.failed_checks += test_suite->failed_checks ();
246 totals.test_cases_count += test_suite->test_cases_count ();
247 }
248 if (reporter->verbosity != verbosity::silent)
249 {
250 // printf ("\n");
251 }
252 }
253
254#if defined(_WIN32) || defined(CLOCK_MONOTONIC)
255#if defined(_WIN32)
256 timespec_get (&end_time, TIME_UTC);
257#else
258 clock_gettime (CLOCK_MONOTONIC, &end_time);
259#endif
260#endif
261
262 reporter->end_test (*this);
263
264 return was_successful ? 0 : 1;
265 }
266
278 void
280 {
281#if 0 // defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
282 printf ("%s\n", __PRETTY_FUNCTION__);
283#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
284
285 if (test_suites == nullptr)
286 {
287 test_suites = new std::vector<test_suite_base*> ();
288 }
289 test_suites->push_back (suite);
290 suite->index = test_suites->size () + 1;
291 }
292
302 void
304 {
305 ::abort ();
306 }
307
308 // --------------------------------------------------------------------------
309} // namespace micro_os_plus::micro_test_plus
310
311// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.14.0.