micro-test-plus 5.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
runner.h
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
44
45#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_TEST_RUNNER_H_
46#define MICRO_OS_PLUS_MICRO_TEST_PLUS_TEST_RUNNER_H_
47
48// ----------------------------------------------------------------------------
49
50#if defined(__cplusplus)
51
52// ----------------------------------------------------------------------------
53
54#if __has_include("micro-os-plus/project-config.h")
55#include "micro-os-plus/project-config.h"
56#endif // __has_include("micro-os-plus/project-config.h")
57
58#if __has_include("micro-os-plus/micro-test-plus-defines.h")
59#include "micro-os-plus/micro-test-plus-defines.h"
60#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
61
62#include "timings.h"
63#include "test.h"
64#include "reflection.h"
65
66#include <functional>
67#include <memory>
68#include <string>
69
70// ----------------------------------------------------------------------------
71
72#if defined(__GNUC__)
73#pragma GCC diagnostic push
74
75#pragma GCC diagnostic ignored "-Wpadded"
76#if defined(__clang__)
77#pragma clang diagnostic ignored "-Wc++98-compat"
78#else // GCC only
79#pragma GCC diagnostic ignored "-Wsuggest-final-types"
80#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
81#pragma GCC diagnostic ignored "-Wredundant-tags"
82#endif // defined(__clang__)
83#endif // defined(__GNUC__)
84
85// ============================================================================
86
112{
113 // --------------------------------------------------------------------------
114
115 class suite;
116 class top_suite;
117 class reporter;
118
119 // ==========================================================================
120
144 {
145 public:
152 runner (void);
153
162 runner (const char* top_suite_name);
163
167 runner (const runner&) = delete;
168
172 runner (runner&&) = delete;
173
177 runner&
179 = delete;
180
184 runner&
186 = delete;
187
191 virtual ~runner () override;
192
202 class suite&
203 initialise (int argc, char* argv[], const char* top_suite_name = "");
204
212 int
213 exit_code (void);
214
229 template <typename Callable_T, typename... Args_T>
230 void
231 suite (const char* name, Callable_T&& callable, Args_T&&... arguments);
232
233 // ------------------------------------------------------------------------
234
242 [[noreturn]] void
245
246 // ------------------------------------------------------------------------
247 // Getters.
248
256 [[nodiscard]] virtual size_t
257 total_suites_count (void) const noexcept;
258
266 [[nodiscard]] class reporter&
267 reporter (void) const noexcept;
268
276 [[nodiscard]] detail::timestamps&
277 timings () noexcept;
278
286 [[nodiscard]] const detail::timestamps&
287 timings () const noexcept;
288
296 [[nodiscard]] size_t
297 suites_count (void) const noexcept;
298
299 protected:
306 virtual void
307 run_suites_ (void);
308
316 void
317 register_suite_ (std::unique_ptr<class suite> suite);
318
319 // ------------------------------------------------------------------------
320 protected:
325
334 std::vector<std::unique_ptr<class suite>> children_suites_;
335
339 std::unique_ptr<class reporter> reporter_;
340
344 detail::timestamps timings_;
345
349 std::string top_suite_name_;
350 };
351
352 // ==========================================================================
353
377 class static_runner final : public runner
378 {
379 public:
386 static_runner (void);
387
396 static_runner (const char* top_suite_name);
397
401 static_runner (const static_runner&) = delete;
402
407
413 = delete;
414
420 = delete;
421
425 virtual ~static_runner () override;
426
427 // ------------------------------------------------------------------------
428
435 static void
437
445 [[nodiscard]] size_t
446 static_suites_count (void) const noexcept;
447
456 [[nodiscard]] virtual size_t
457 total_suites_count (void) const noexcept final override;
458
459 protected:
466 void
467 run_suites_ (void) override;
468
469 protected:
483 std::vector<static_suite*>* static_children_suites_;
484 };
485
486} // namespace micro_os_plus::micro_test_plus
487
488#if defined(__GNUC__)
489#pragma GCC diagnostic pop
490#endif // defined(__GNUC__)
491
492// ----------------------------------------------------------------------------
493
494#endif // defined(__cplusplus)
495
496// ============================================================================
497// Templates, inlines & constexpr implementations.
498
500
501// ----------------------------------------------------------------------------
502
503#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TEST_RUNNER_H_
504
505// ----------------------------------------------------------------------------
Base class for runners and runable tests.
Definition test.h:155
const char * name(void) const noexcept
Gets the node name.
A begin/end timestamp pair used to measure elapsed time.
Definition timings.h:214
Local implementation of source location information for diagnostics.
Definition reflection.h:150
static constexpr source_location current(const char *file="unknown", unsigned int line={}) noexcept
Obtain the current source location.
Reporter to display test results, including operand values and types for failures.
Definition reporter.h:195
runner(runner &&)=delete
Deleted move constructor to prevent moving.
detail::timestamps & timings() noexcept
Gets the timings for this runner.
class reporter & reporter(void) const noexcept
Returns a reference to the test reporter.
void suite(const char *name, Callable_T &&callable, Args_T &&... arguments)
Adds a test suite to the runner.
std::string top_suite_name_
Owned storage for the implicit top-suite name.
Definition runner.h:349
virtual ~runner() override
Destructor for the runner class.
Definition runner.cpp:135
runner & operator=(const runner &)=delete
Deleted copy assignment operator to prevent copying.
runner(void)
Constructor for the runner class.
Definition runner.cpp:109
size_t suites_count(void) const noexcept
Returns the count of test suites.
Definition runner.cpp:412
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution immediately.
Definition runner.cpp:394
detail::timestamps timings_
Timings for this runner.
Definition runner.h:344
int exit_code(void)
Returns 0 if all tests were successful, 1 otherwise.
Definition runner.cpp:358
class top_suite top_suite_
The implicit top-level suite; always present and executed first.
Definition runner.h:324
std::vector< std::unique_ptr< class suite > > children_suites_
Owning collection of dynamically registered child suites.
Definition runner.h:334
void register_suite_(std::unique_ptr< class suite > suite)
Registers a test suite with the runner.
Definition runner.cpp:291
virtual size_t total_suites_count(void) const noexcept
Returns the total count of registered test suites.
Definition runner.cpp:423
std::unique_ptr< class reporter > reporter_
Pointer to the test reporter used for outputting test results.
Definition runner.h:339
class suite & initialise(int argc, char *argv[], const char *top_suite_name="")
Initialises the test runner with command-line arguments.
Definition runner.cpp:165
virtual void run_suites_(void)
Runs all registered test suites.
Definition runner.cpp:311
runner(const runner &)=delete
Deleted copy constructor to prevent copying.
static void register_static_suite(static_runner &runner, static_suite &suite)
Registers a static test suite with the runner.
Definition runner.cpp:568
std::vector< static_suite * > * static_children_suites_
Pointer to the vector of registered static test suites.
Definition runner.h:483
static_runner(void)
Constructor for the runner class.
Definition runner.cpp:436
static_runner(static_runner &&)=delete
Deleted move constructor to prevent moving.
static_runner(const static_runner &)=delete
Deleted copy constructor to prevent copying.
size_t static_suites_count(void) const noexcept
Returns the total count of registered static test suites.
Definition runner.cpp:486
A test suite designed for static (namespace-scope) registration with a static_runner.
Definition test.h:933
A named, runnable test suite registered with the test runner.
Definition test.h:723
The implicit top-level suite owned by every runner instance.
Definition test.h:848
Internal implementation details for the µTest++ framework.
Primary namespace for the µTest++ testing framework.
C++ header file with declarations for the µTest++ reflection utilities.
C++ header file with inline implementations for the µTest++ test runner.
C++ header file with declarations for the µTest++ test suite.
C++ header file with declarations for the µTest++ timing utilities.