micro-test-plus 4.1.0
µ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_TEST_PLUS_TEST_RUNNER_H_
46#define MICRO_TEST_PLUS_TEST_RUNNER_H_
47
48// ----------------------------------------------------------------------------
49
50#ifdef __cplusplus
51
52// ----------------------------------------------------------------------------
53
54#include <functional>
55#include <memory>
56#include <string>
57
58#include "timings.h"
59#include "test.h"
60#include "reflection.h"
61
62// ----------------------------------------------------------------------------
63
64#if defined(__GNUC__)
65#pragma GCC diagnostic push
66#pragma GCC diagnostic ignored "-Wpadded"
67#if defined(__clang__)
68#pragma clang diagnostic ignored "-Wc++98-compat"
69#else // GCC only
70#pragma GCC diagnostic ignored "-Wsuggest-final-types"
71#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
72#pragma GCC diagnostic ignored "-Wredundant-tags"
73#endif
74#endif
75
76// ============================================================================
77
79{
80 // --------------------------------------------------------------------------
81
82 class suite;
83 class top_suite;
84 class reporter;
85
86 // ==========================================================================
87
111 {
112 public:
119 runner (void);
120
129 runner (const char* top_suite_name);
130
134 runner (const runner&) = delete;
135
139 runner (runner&&) = delete;
140
144 runner&
146 = delete;
147
151 runner&
153 = delete;
154
158 virtual ~runner () override;
159
169 class suite&
170 initialise (int argc, char* argv[], const char* top_suite_name = "");
171
179 int
180 exit_code (void);
181
196 template <typename Callable_T, typename... Args_T>
197 void
198 suite (const char* name, Callable_T&& callable, Args_T&&... arguments);
199
200 // ------------------------------------------------------------------------
201
209 [[noreturn]] void
212
213 // ------------------------------------------------------------------------
214 // Getters.
215
223 [[nodiscard]] virtual size_t
224 total_suites_count (void) const noexcept;
225
233 [[nodiscard]] class reporter&
234 reporter (void) const noexcept;
235
243 [[nodiscard]] detail::timestamps&
244 timings () noexcept;
245
253 [[nodiscard]] const detail::timestamps&
254 timings () const noexcept;
255
263 [[nodiscard]] size_t
264 suites_count (void) const noexcept;
265
266 protected:
273 virtual void
274 run_suites_ (void);
275
283 void
284 register_suite_ (std::unique_ptr<class suite> suite);
285
286 // ------------------------------------------------------------------------
287 protected:
292
301 std::vector<std::unique_ptr<class suite>> children_suites_;
302
306 std::unique_ptr<class reporter> reporter_;
307
311 detail::timestamps timings_;
312
316 std::string top_suite_name_;
317 };
318
319 // ==========================================================================
320
344 class static_runner final : public runner
345 {
346 public:
353 static_runner (void);
354
363 static_runner (const char* top_suite_name);
364
368 static_runner (const static_runner&) = delete;
369
374
380 = delete;
381
387 = delete;
388
392 virtual ~static_runner () override;
393
394 // ------------------------------------------------------------------------
395
402 static void
404
412 [[nodiscard]] size_t
413 static_suites_count (void) const noexcept;
414
423 [[nodiscard]] virtual size_t
424 total_suites_count (void) const noexcept final override;
425
426 protected:
433 void
434 run_suites_ (void) override;
435
436 protected:
450 std::vector<static_suite*>* static_children_suites_;
451 };
452
453} // namespace micro_os_plus::micro_test_plus
454
455#if defined(__GNUC__)
456#pragma GCC diagnostic pop
457#endif
458
459// ----------------------------------------------------------------------------
460
461#endif // __cplusplus
462
463// ============================================================================
464// Templates & constexpr implementations.
465
467
468// ----------------------------------------------------------------------------
469
470#endif // MICRO_TEST_PLUS_TEST_RUNNER_H_
471
472// ----------------------------------------------------------------------------
Base class for runners and runable tests.
Definition test.h:146
const char * name(void) const noexcept
Gets the node name.
A begin/end timestamp pair used to measure elapsed time.
Definition timings.h:205
Local implementation of source location information for diagnostics.
Definition reflection.h:138
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:186
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:316
virtual ~runner() override
Destructor for the runner class.
Definition runner.cpp:153
runner & operator=(const runner &)=delete
Deleted copy assignment operator to prevent copying.
runner(void)
Constructor for the runner class.
Definition runner.cpp:107
size_t suites_count(void) const noexcept
Returns the count of test suites.
Definition runner.cpp:459
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution immediately.
Definition runner.cpp:431
detail::timestamps timings_
Timings for this runner.
Definition runner.h:311
int exit_code(void)
Returns 0 if all tests were successful, 1 otherwise.
Definition runner.cpp:384
class top_suite top_suite_
The implicit top-level suite; always present and executed first.
Definition runner.h:291
std::vector< std::unique_ptr< class suite > > children_suites_
Owning collection of dynamically registered child suites.
Definition runner.h:301
void register_suite_(std::unique_ptr< class suite > suite)
Registers a test suite with the runner.
Definition runner.cpp:307
virtual size_t total_suites_count(void) const noexcept
Returns the total count of registered test suites.
Definition runner.cpp:470
std::unique_ptr< class reporter > reporter_
Pointer to the test reporter used for outputting test results.
Definition runner.h:306
class suite & initialise(int argc, char *argv[], const char *top_suite_name="")
Initialises the test runner with command-line arguments.
Definition runner.cpp:182
virtual void run_suites_(void)
Runs all registered test suites.
Definition runner.cpp:337
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:637
std::vector< static_suite * > * static_children_suites_
Pointer to the vector of registered static test suites.
Definition runner.h:450
static_runner(void)
Constructor for the runner class.
Definition runner.cpp:483
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:554
A test suite designed for static (namespace-scope) registration with a static_runner.
Definition test.h:924
A named, runnable test suite registered with the test runner.
Definition test.h:714
The implicit top-level suite owned by every runner instance.
Definition test.h:839
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.