micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::subtest Class Reference

A named, runnable test case that lives inside a suite. More...

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

Inheritance diagram for micro_os_plus::micro_test_plus::subtest:

Public Member Functions

template<typename Callable_T, typename... Args_T>
 subtest (const char *name, class runner &runner, suite &parent_suite, size_t own_index, size_t nesting_depth, Callable_T &&callable, Args_T &&... arguments)
 Constructs a subtest with a name, runner, parent suite, index, nesting depth, and callable.
 subtest (const subtest &)=delete
 Deleted copy constructor to prevent copying.
 subtest (subtest &&)=delete
 Deleted move constructor to prevent moving.
virtual ~subtest () override
 Virtual destructor.
void abort (const reflection::source_location &sl=reflection::source_location::current())
 Aborts test execution via the owning runner.
template<class Expr_T>
requires type_traits::checkable<Expr_T>
auto assume (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current())
 Check a condition and, if false, abort test execution.
size_t children_subtests_count (void) const noexcept
 Returns the number of direct child subtests owned by this node.
size_t current_subtest_index () const noexcept
 Returns the index of the most recently created child subtest.
template<class Expr_T>
requires type_traits::checkable<Expr_T>
auto expect (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current())
 Evaluate a generic condition and report the results.
size_t increment_subtest_index () noexcept
 Increments and returns the child subtest sequential index.
const char * name (void) const noexcept
 Gets the node name.
size_t nesting_depth () const noexcept
 Returns the nesting depth of this subtest.
subtestoperator= (const subtest &)=delete
 Deleted copy assignment operator to prevent copying.
subtestoperator= (subtest &&)=delete
 Deleted move assignment operator to prevent moving.
size_t own_index () const noexcept
 Returns the positional index of this object within its parent.
void own_index (size_t index) noexcept
 Sets the positional index of this object within its parent.
class reporter & reporter (void) const noexcept
 Gets the test reporter associated with this test runnable.
virtual void run (void) override
 Executes the subtest body by invoking the stored callable.
class runner & runner (void) const noexcept
 Gets the test runner associated with this test runnable.
template<typename Callable_T, typename... Args_T>
void test (const char *name, Callable_T &&callable, Args_T &&... arguments)
 Adds a test case to the suite.
const runner_totals & totals () const noexcept
 Gets the totals for the test (const overload).
runner_totals & totals () noexcept
 Gets the totals for the test.

Protected Member Functions

void after_subtest_create_ (std::unique_ptr< subtest > child_test, suite &suite)
 Registers a newly constructed child subtest and executes it immediately.

Protected Attributes

std::function< void(subtest &)> callable_
 Callable storing the test body and any bound arguments. Invoked with a reference to the derived Self_T instance.
std::vector< std::unique_ptr< subtest > > children_subtests_
 Owning collection of direct child subtests.
size_t current_subtest_index_
 The subtest index, counting from 1.
const char * name_
 The test node name.
size_t nesting_depth_
 The nesting depth of this subtest within the suite.
size_t own_index_
 The test index, counting from 1.
suiteparent_suite_
 Reference to the parent suite that owns this subtest.
class runnerrunner_
 Reference to the test runner that owns this object.
runner_totals totals_
 Totals for the test node, including nested cases.

Detailed Description

subtest represents a single, named test case or a nested group of checks within a parent suite or subtest. It is constructed by calling suite::test() or subtest::test(), both of which create the object, immediately execute its callable body via run(), and register the result with the parent node.

The body of the subtest is supplied as a callable (typically a lambda) that receives a subtest& reference as its first argument. Inside the body, expect() and assume() are used to evaluate conditions and record the results. Subtests may be nested to an arbitrary depth.

The class is non-copyable and non-movable to preserve unique ownership and consistent state throughout the test session.

Definition at line 541 of file test.h.

Constructor & Destructor Documentation

◆ subtest() [1/3]

template<typename Callable_T, typename... Args_T>
micro_os_plus::micro_test_plus::subtest::subtest ( const char * name,
class runner & runner,
suite & parent_suite,
size_t own_index,
size_t nesting_depth,
Callable_T && callable,
Args_T &&... arguments )
Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
[in]nameThe subtest name or description, used in reports.
[in]runnerThe test runner managing this subtest.
[in]parent_suiteThe suite that owns this subtest.
[in]own_indexThe one-based positional index within the parent.
[in]nesting_depthThe depth of nesting; 1 for top-level subtests.
[in]callableA generic callable object, usually a lambda, invoked when the subtest executes.
[in]argumentsA possibly empty list of arguments forwarded to the callable after the leading subtest& reference.

Delegates to runnable, which binds the callable with its arguments.

Definition at line 262 of file test-inlines.h.

267 std::forward<Callable_T> (callable),
268 std::forward<Args_T> (arguments)... },
269 parent_suite_{ parent_suite }, nesting_depth_{ nesting_depth }
270 {
271#if defined(MICRO_OS_PLUS_TRACE) \
272 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
273#if defined(__GNUC__)
274#pragma GCC diagnostic push
275#if defined(__clang__)
276#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
277#endif
278#endif
279 trace::printf ("%s '%s' %zu %zu\n", __PRETTY_FUNCTION__, name, own_index_,
281#if defined(__GNUC__)
282#pragma GCC diagnostic pop
283#endif
284#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
285 }
class runner & runner(void) const noexcept
Gets the test runner associated with this test runnable.
size_t own_index_
The test index, counting from 1.
Definition test.h:406
size_t own_index() const noexcept
Returns the positional index of this object within its parent.
runnable(const char *name, class runner &runner, size_t own_index, Callable_T &&callable, Args_T &&... arguments)
const char * name(void) const noexcept
Gets the node name.
suite & parent_suite_
Reference to the parent suite that owns this subtest.
Definition test.h:683
size_t nesting_depth_
The nesting depth of this subtest within the suite.
Definition test.h:688
size_t nesting_depth() const noexcept
Returns the nesting depth of this subtest.

References micro_os_plus::micro_test_plus::detail::runnable< subtest >::runnable(), subtest(), micro_os_plus::micro_test_plus::detail::runnable< subtest >::name(), nesting_depth(), micro_os_plus::micro_test_plus::detail::runnable< subtest >::own_index(), and micro_os_plus::micro_test_plus::detail::runnable< subtest >::runner().

Referenced by subtest(), subtest(), subtest(), operator=(), and operator=().

◆ subtest() [2/3]

micro_os_plus::micro_test_plus::subtest::subtest ( const subtest & )
delete

References subtest().

◆ subtest() [3/3]

micro_os_plus::micro_test_plus::subtest::subtest ( subtest && )
delete

References subtest().

◆ ~subtest()

micro_os_plus::micro_test_plus::subtest::~subtest ( )
overridevirtual

The destructor releases any resources associated with the subtest instance. If tracing is enabled, it outputs the function signature for diagnostic purposes. This ensures that the test suite is properly cleaned up after execution, supporting robust and reliable test management across all files and folders within the µTest++ framework.

Definition at line 275 of file test.cpp.

276 {
277#if defined(MICRO_OS_PLUS_TRACE) \
278 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
279#if defined(__GNUC__)
280#pragma GCC diagnostic push
281#if defined(__clang__)
282#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
283#endif
284#endif
285 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
286#if defined(__GNUC__)
287#pragma GCC diagnostic pop
288#endif
289#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
290 }
const char * name_
The test node name.
Definition test.h:224

References micro_os_plus::micro_test_plus::detail::runnable< subtest >::name_.

Member Function Documentation

◆ abort()

Parameters
slThe source location from which the abort is triggered.
Returns
Does not return.

Delegates immediately to runner_.abort(), passing the supplied source location so that the error message identifies the call site before the process is terminated via ::abort().

Definition at line 369 of file test.cpp.

200 {
201 runner_.abort (sl);
202 }
void abort(const reflection::source_location &sl=reflection::source_location::current())
Aborts test execution via the owning runner.
Definition test.cpp:199
CRTP base class factoring out callable storage, rule-of-five, and run() logic shared by subtest and s...
Definition test.h:444

References name_, and reporter().

◆ after_subtest_create_()

void micro_os_plus::micro_test_plus::detail::runnable_base::after_subtest_create_ ( std::unique_ptr< subtest > child_test,
suite & suite )
protectedinherited
Parameters
child_testOwning pointer to the newly created subtest.
suiteThe parent suite to which execution results are reported.
Returns
Nothing.

Transfers ownership of child_test into children_subtests_ and immediately invokes subtest::run() on the newly stored subtest. The parent's executed-subtest counter is then incremented. The child's check counters are intentionally not merged into the parent totals; each subtest reports only its own counters. The child's totals are, however, accumulated into suite so that the suite summary reflects all checks performed by its subtests.

Definition at line 394 of file test.cpp.

217 {
218 // Transfer ownership into the vector.
220
221 // Run the child test case immediately.
222 class subtest& subtest = *children_subtests_.back ();
223 subtest.run ();
224
225 // This test executed one more subtest.
226#if defined(MICRO_OS_PLUS_TRACE) \
227 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
228#if defined(__GNUC__)
229#pragma GCC diagnostic push
230#if defined(__clang__)
231#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
232#endif
233#endif
234 trace::printf ("%s subtest '%s' executed one more subtest\n",
236#if defined(__GNUC__)
237#pragma GCC diagnostic pop
238#endif
239#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
241 // Do not accumulate the totals from the child test into the current test
242 // totals, each subtest shows only its counters.
243
244#if defined(MICRO_OS_PLUS_TRACE) \
245 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS)
246#if defined(__GNUC__)
247#pragma GCC diagnostic push
248#if defined(__clang__)
249#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
250#endif
251#endif
252 trace::printf ("%s suite '%s' totals\n", __PRETTY_FUNCTION__,
253 suite.name ());
254#if defined(__GNUC__)
255#pragma GCC diagnostic pop
256#endif
257#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS_CONSTRUCTORS
258
259 // Accumulate the totals from the child test into the suite totals.
260 suite.totals () += subtest.totals ();
261 }
virtual void run(void)=0
Runs the test function by invoking the stored callable with the derived self instance.
void increment_executed_subtests(size_t count=1) noexcept
Increments the executed-subtests counter.

◆ children_subtests_count()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::children_subtests_count ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The number of child subtests.

Returns the number of child subtests owned by this node.

Definition at line 349 of file test-inlines.h.

166 {
167 return children_subtests_.size ();
168 }

◆ current_subtest_index()

size_t micro_os_plus::micro_test_plus::detail::runnable_base::current_subtest_index ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The current child subtest sequential index.

Returns the sequential index of the most recently created child subtest.

Definition at line 329 of file test-inlines.h.

144 {
146 }

◆ increment_subtest_index()

Parameters
None.
Returns
The new index value after incrementing.

Each call to test() invokes this method before constructing the new subtest, so the index values form a strictly increasing, one-based sequence.

Definition at line 339 of file test-inlines.h.

156 {
157 return ++current_subtest_index_;
158 }

◆ name()

const char * micro_os_plus::micro_test_plus::detail::test_node::name ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A pointer to the null-terminated test node name.

Returns a pointer to the null-terminated name stored in name_.

Definition at line 194 of file test-inlines.h.

92 {
93 return name_;
94 }

Referenced by micro_os_plus::micro_test_plus::subtest::subtest(), micro_os_plus::micro_test_plus::subtest::operator=(), own_index(), runner(), micro_os_plus::micro_test_plus::subtest::test(), and totals().

◆ nesting_depth()

◆ operator=() [1/2]

subtest & micro_os_plus::micro_test_plus::subtest::operator= ( const subtest & )
delete

References subtest().

◆ operator=() [2/2]

◆ own_index() [1/2]

size_t micro_os_plus::micro_test_plus::detail::runnable_base::own_index ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The one-based own index.

Returns the one-based positional index of this object within its parent.

Definition at line 305 of file test-inlines.h.

124 {
125 return own_index_;
126 }

References name(), own_index(), and runner().

Referenced by micro_os_plus::micro_test_plus::subtest::subtest(), and own_index().

◆ own_index() [2/2]

void micro_os_plus::micro_test_plus::detail::runnable_base::own_index ( size_t index)
inlinenoexceptinherited
Note
This overload follows the same-name getter/setter pattern used throughout the framework: the getter is the const overload and the setter is the non-const single-argument overload.
Parameters
indexThe new index value.
Returns
Nothing.

Sets the positional index of this object within its parent.

Definition at line 319 of file test-inlines.h.

134 {
136 }

◆ reporter()

reporter & micro_os_plus::micro_test_plus::detail::runnable_base::reporter ( void ) const
nodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the test reporter.

Delegates immediately to runner_.reporter(), returning the reporter associated with the owning runner instance.

Definition at line 359 of file test.cpp.

188 {
189 return runner_.reporter ();
190 }
class reporter & reporter(void) const noexcept
Gets the test reporter associated with this test runnable.
Definition test.cpp:187

Referenced by abort(), micro_os_plus::micro_test_plus::subtest::assume(), micro_os_plus::micro_test_plus::subtest::expect(), and micro_os_plus::micro_test_plus::subtest::run().

◆ run()

void micro_os_plus::micro_test_plus::subtest::run ( void )
overridevirtual
Parameters
None.
Returns
Nothing.

Invokes the stored callable with the Self_T instance, surrounded by reporter calls to begin and end the subtest.

Implements micro_os_plus::micro_test_plus::detail::runnable< subtest >.

Definition at line 298 of file test.cpp.

299 {
300#if defined(MICRO_OS_PLUS_TRACE) \
301 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
302#if defined(__GNUC__)
303#pragma GCC diagnostic push
304#if defined(__clang__)
305#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
306#endif
307#endif
308 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name_);
309#if defined(__GNUC__)
310#pragma GCC diagnostic pop
311#endif
312#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
313
314 class reporter& reporter = this->reporter ();
315
316 // For now, subtests do not record the time.
317 // this->timings.timestamp_begin ();
318 reporter.begin_subtest (*this);
319
320 // Invoke the callable, passing the self reference followed by the variadic
321 // arguments.
322 callable_ (*this);
323
324 // this->timings.timestamp_end ();
325 reporter.end_subtest (*this);
326 }

References micro_os_plus::micro_test_plus::reporter::begin_subtest(), micro_os_plus::micro_test_plus::detail::runnable< subtest >::callable_, micro_os_plus::micro_test_plus::detail::runnable< subtest >::name_, and micro_os_plus::micro_test_plus::detail::runnable< subtest >::reporter().

Referenced by micro_os_plus::micro_test_plus::detail::runnable_base::after_subtest_create_(), and operator=().

◆ runner()

class runner & micro_os_plus::micro_test_plus::detail::runnable_base::runner ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the test runner.

Returns a reference to the owning test runner.

Definition at line 380 of file test-inlines.h.

176 {
177 return runner_;
178 }

References name(), and own_index_.

Referenced by micro_os_plus::micro_test_plus::subtest::subtest(), and own_index().

◆ test()

template<typename Callable_T, typename... Args_T>
void micro_os_plus::micro_test_plus::subtest::test ( const char * name,
Callable_T && callable,
Args_T &&... arguments )
Template Parameters
Callable_TThe type of a callable object.
Args_TThe types of the callable arguments.
Parameters
[in]nameThe test case name or description, used in reports.
[in]callableA generic callable object, usually a lambda, invoked to perform the test.
[in]argumentsA possibly empty list of arguments to be passed to the callable.

Allocates a child subtest on the heap, incrementing the subtest index and deepening the nesting level by one relative to this subtest's depth, then transfers ownership to the framework via after_subtest_create_().

Definition at line 295 of file test-inlines.h.

297 {
298#if defined(MICRO_OS_PLUS_TRACE) \
299 && defined(MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS)
300#if defined(__GNUC__)
301#pragma GCC diagnostic push
302#if defined(__clang__)
303#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
304#endif
305#endif
306 trace::printf ("%s '%s'\n", __PRETTY_FUNCTION__, name);
307#if defined(__GNUC__)
308#pragma GCC diagnostic pop
309#endif
310#endif // MICRO_OS_PLUS_TRACE_MICRO_TEST_PLUS
311
313 auto child_subtest = std::make_unique<subtest> (
315 std::forward<Callable_T> (callable),
316 std::forward<Args_T> (arguments)...);
317
318 after_subtest_create_ (std::move (child_subtest), parent_suite_);
319 }
void after_subtest_create_(std::unique_ptr< subtest > child_test, suite &suite)
Definition test.cpp:215

References micro_os_plus::micro_test_plus::detail::runnable< subtest >::name().

Referenced by operator=().

◆ totals() [1/2]

const runner_totals & micro_os_plus::micro_test_plus::detail::test_node::totals ( ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A const reference to the runner_totals instance.

Returns a const reference to the runner_totals member.

Definition at line 215 of file test-inlines.h.

112 {
113 return totals_;
114 }

References name(), and own_index_.

◆ totals() [2/2]

runner_totals & micro_os_plus::micro_test_plus::detail::test_node::totals ( )
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
A reference to the runner_totals instance.

Returns a reference to the runner_totals member.

Definition at line 205 of file test-inlines.h.

102 {
103 return totals_;
104 }

References callable_.

Member Data Documentation

◆ callable_

std::function<void (subtest&)> micro_os_plus::micro_test_plus::detail::runnable< subtest >::callable_
protectedinherited

Definition at line 512 of file test.h.

Referenced by micro_os_plus::micro_test_plus::subtest::run(), and totals().

◆ children_subtests_

Each call to test() appends a new subtest to this vector and runs it immediately. The vector retains ownership for the lifetime of the parent runnable.

Definition at line 427 of file test.h.

◆ current_subtest_index_

This index is used for reporting and tracking the execution order of subtests within a suite, especially when nested subtests are involved. It is incremented for each subtest created, allowing for clear identification of subtests in reports and diagnostics.

Definition at line 417 of file test.h.

◆ name_

Note
Derived classes may access this member directly in addition to the public name() getter.

Definition at line 224 of file test.h.

Referenced by micro_os_plus::micro_test_plus::subtest::~subtest(), abort(), and micro_os_plus::micro_test_plus::subtest::run().

◆ nesting_depth_

size_t micro_os_plus::micro_test_plus::subtest::nesting_depth_
protected

Definition at line 688 of file test.h.

Referenced by nesting_depth().

◆ own_index_

Definition at line 406 of file test.h.

Referenced by runner(), and totals().

◆ parent_suite_

suite& micro_os_plus::micro_test_plus::subtest::parent_suite_
protected

Definition at line 683 of file test.h.

◆ runner_

Definition at line 401 of file test.h.

◆ totals_

Definition at line 229 of file test.h.


The documentation for this class was generated from the following files: