kaycxx-test
C++ unit test framework
Loading...
Searching...
No Matches
test_registry.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
11#include <cstddef>
12#include <functional>
13#include <memory>
14#include <source_location>
15#include <string>
16#include <string_view>
17#include <vector>
18
20#include <kaycxx/test/hook.hpp>
23#include <kaycxx/test/skip.hpp>
25
26namespace kaycxx::test::detail {
27
28class test_suite;
29
30} // namespace kaycxx::test::detail
31
32namespace kaycxx::test {
33
38public:
41
44
45 test_registry(test_registry const&) = delete;
46 test_registry& operator=(test_registry const&) = delete;
47
57 void add_suite(std::string_view description, callback body, std::source_location location = std::source_location::current());
58
66 void add_test(std::string_view description, callback body, std::source_location location = std::source_location::current());
67
76 void add_test(std::string_view description, skip_condition condition, callback body, std::source_location location = std::source_location::current());
77
84
91
98
105
113 bool run(reporter& reporter, run_options const& options = {});
114
125 bool run(reporter& reporter, test_filter const& filter, run_options const& options = {});
126
134 std::vector<std::string> list_tests() const;
135
144 std::vector<std::string> list_tests(test_filter const& filter) const;
145
151 std::size_t num_test_suites() const;
152
159 std::size_t num_test_suites(test_filter const& filter) const;
160
166 std::size_t num_test_cases() const;
167
174 std::size_t num_test_cases(test_filter const& filter) const;
175
176private:
178 std::vector<std::unique_ptr<detail::test_suite>> root_suites_;
179
181 std::vector<std::reference_wrapper<detail::test_suite>> suite_stack_;
182
188 detail::test_suite& current();
189
197 detail::test_suite& add_suite_node(std::string_view description, std::source_location location);
198};
199
206
216
224
227
228} // namespace kaycxx::test
Defines the callback type used by the test framework.
Registered setup or teardown hook.
Definition hook.hpp:25
Receives lifecycle events from the test runner.
Definition reporter.hpp:22
Condition used by a test case to decide whether it should be skipped before running hooks and test co...
Definition skip.hpp:20
Owns a set of test suites and the registration stack used while suite bodies execute.
Definition test_registry.hpp:37
std::size_t num_test_suites(test_filter const &filter) const
Returns the number of suites containing tests selected by a filter.
void add_after_all_hook(hook value)
Adds an after_all hook to the current suite.
void add_before_each_hook(hook value)
Adds a before_each hook to the current suite.
std::size_t num_test_cases() const
Returns the number of all registered test cases.
void add_before_all_hook(hook value)
Adds a before_all hook to the current suite.
bool run(reporter &reporter, test_filter const &filter, run_options const &options={})
Executes registered tests selected by a filter.
std::size_t num_test_suites() const
Returns the number of all registered top-level and nested suites.
bool run(reporter &reporter, run_options const &options={})
Executes all registered top-level suites in declaration order.
std::vector< std::string > list_tests() const
Lists all registered test cases in execution order.
~test_registry()
Destroys the test registry and all registered suites.
std::vector< std::string > list_tests(test_filter const &filter) const
Lists registered test cases selected by a filter in execution order.
std::size_t num_test_cases(test_filter const &filter) const
Returns the number of test cases selected by a filter.
void add_test(std::string_view description, callback body, std::source_location location=std::source_location::current())
Adds a test case to the current suite.
void add_test(std::string_view description, skip_condition condition, callback body, std::source_location location=std::source_location::current())
Adds a test case with a skip condition to the current suite.
void add_after_each_hook(hook value)
Adds an after_each hook to the current suite.
test_registry()
Creates an empty test registry.
void add_suite(std::string_view description, callback body, std::source_location location=std::source_location::current())
Adds a suite and immediately executes its registration body.
Defines registered test hooks.
Unit test framework functions and types.
Definition test.hpp:15
void reset_active_registry()
Resets the active registry to the default registry.
std::move_only_function< void()> callback
Callable block used by tests, suites, and hooks.
Definition callback.hpp:20
test_registry & set_active_registry(test_registry &registry)
Changes the registry used by the public DSL functions.
test_registry & default_registry()
Returns the global registry used by the public DSL functions.
test_registry & active_registry()
Returns the registry currently used by the public DSL functions.
Defines the reporter interface used by the test runner.
Defines test execution options.
Defines skip conditions for test cases.
Configures the execution of registered tests.
Definition run_options.hpp:14
Selects registered tests by source path and full description.
Definition test_filter.hpp:23
Defines test selection filters.