kaycxx-test
C++ unit test framework
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
kaycxx::test Namespace Reference

Unit test framework functions and types. More...

Classes

class  assertion_error
 Thrown when an assertion fails. More...
 
class  failure
 Failure result passed to reporters when a test case or suite fails. More...
 
class  hook
 Registered setup or teardown hook. More...
 
class  reporter
 Receives lifecycle events from the test runner. More...
 
struct  run_options
 Configures the execution of registered tests. More...
 
class  skip_condition
 Condition used by a test case to decide whether it should be skipped before running hooks and test code. More...
 
class  skipped
 Result object passed to reporters when a test case was skipped. More...
 
struct  test_filter
 Selects registered tests by source path and full description. More...
 
class  test_registry
 Owns a set of test suites and the registration stack used while suite bodies execute. More...
 

Typedefs

using callback = std::move_only_function< void()>
 Callable block used by tests, suites, and hooks.
 

Functions

void after_all (callback body, std::source_location location=std::source_location::current())
 Registers a hook that runs once after all tests in the current suite have finished.
 
void after_all (hook value)
 Registers a hook that runs once after all tests in the current suite have finished.
 
void after_each (callback body, std::source_location location=std::source_location::current())
 Registers a hook that runs after each test in the current suite and its child suites.
 
void after_each (hook value)
 Registers a hook that runs after each test in the current suite and its child suites.
 
template<typename Actual , typename Expected >
requires detail::close_supported<Actual, Expected>
void assert_close (Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that two numeric values are close enough.
 
template<typename Actual , typename Expected >
requires detail::contains_supported<Actual, Expected>
void assert_contain (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that a string contains a substring or character, or that a range contains an element.
 
template<typename Actual , typename Expected >
requires detail::equals_supported<Actual, Expected>
void assert_equal (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that two values are equal.
 
void assert_false (bool actual, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument is false.
 
template<detail::ordered_value T>
void assert_greater (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the actual value is greater than the expected value.
 
template<detail::ordered_value T>
void assert_greater_or_equal (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the actual value is greater than or equal to the expected value.
 
template<detail::ordered_value T>
void assert_less (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the actual value is less than the expected value.
 
template<detail::ordered_value T>
void assert_less_or_equal (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the actual value is less than or equal to the expected value.
 
template<detail::string_like T>
void assert_match (T const &actual, std::string_view pattern, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument matches the given pattern.
 
template<detail::string_like T>
void assert_match (T const &actual, std::regex const &regex, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument matches the given regular expression.
 
template<typename Actual , typename Expected >
requires detail::close_supported<Actual, Expected>
void assert_not_close (Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that two numeric values are not close enough.
 
template<typename Actual , typename Expected >
requires detail::contains_supported<Actual, Expected>
void assert_not_contain (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that a string does not contain a substring or character, or that a range does not contain an element.
 
template<typename Actual , typename Expected >
requires detail::equals_supported<Actual, Expected>
void assert_not_equal (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that two values are not equal.
 
template<detail::string_like T>
void assert_not_match (T const &actual, std::string_view pattern, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument does not match the given pattern.
 
template<detail::string_like T>
void assert_not_match (T const &actual, std::regex const &regex, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument does not match the given regular expression.
 
template<detail::nullable T>
void assert_not_null (T const &actual, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument is not null.
 
template<std::invocable Function>
void assert_not_throw (Function &&function, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the function does not throw any exception.
 
template<detail::exception_type Exception, std::invocable Function>
void assert_not_throw (Function &&function, std::string_view unexpected_message, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the function does not throw an exception of the given type with the exact unexpected message.
 
template<detail::exception_type Exception, std::invocable Function>
void assert_not_throw (Function &&function, std::regex const &unexpected_message, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the function does not throw an exception of the given type with a message matching the regex.
 
template<detail::nullable T>
void assert_null (T const &actual, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument is null.
 
template<detail::exception_type Exception, std::invocable Function>
void assert_throw (Function &&function, std::string_view expected_message, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the function throws an exception of the expected type with the exact expected message.
 
template<detail::exception_type Exception, std::invocable Function>
void assert_throw (Function &&function, std::regex const &expected_message, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the function throws an exception of the expected type with a message matching the regex.
 
void assert_true (bool actual, std::string_view reason={}, std::optional< std::source_location > location=std::source_location::current())
 Asserts that the argument is true.
 
void before_all (callback body, std::source_location location=std::source_location::current())
 Registers a hook that runs once before any tests in the current suite are executed.
 
void before_all (hook value)
 Registers a hook that runs once before any tests in the current suite are executed.
 
void before_each (callback body, std::source_location location=std::source_location::current())
 Registers a hook that runs before each test in the current suite and its child suites.
 
void before_each (hook value)
 Registers a hook that runs before each test in the current suite and its child suites.
 
void describe (std::string_view description, callback body, std::source_location location=std::source_location::current())
 Defines a nested test suite and immediately executes its registration body.
 
void it (std::string_view description, callback body, std::source_location location=std::source_location::current())
 Registers a test case in the current suite.
 
void it (std::string_view description, skip_condition skip_condition, callback body, std::source_location location=std::source_location::current())
 Registers a test case in the current suite with a skip condition.
 
int run_tests ()
 Runs the global test registry and writes the report to standard output.
 
int run_tests (int argc, char *argv[])
 Runs the global test registry according to command-line arguments.
 
int run_tests (std::ostream &output)
 Runs the global test registry and writes the report to the given stream.
 
int run_tests (test_registry &registry, std::ostream &output)
 Runs the given test registry and writes the report to the given stream.
 
skip_condition skip (std::string_view reason="")
 Creates a condition that always skips the test.
 
skip_condition skip_if (std::move_only_function< bool()> condition, std::string_view reason="")
 Creates a condition that skips the test when the callback returns true.
 
skip_condition skip_unless (std::move_only_function< bool()> condition, std::string_view reason="")
 Creates a condition that skips the test unless the callback returns true.
 
test_registrydefault_registry ()
 Returns the global registry used by the public DSL functions.
 
test_registryactive_registry ()
 Returns the registry currently used by the public DSL functions.
 
test_registryset_active_registry (test_registry &registry)
 Changes the registry used by the public DSL functions.
 
void reset_active_registry ()
 Resets the active registry to the default registry.
 

Detailed Description

Unit test framework functions and types.

Typedef Documentation

◆ callback

using kaycxx::test::callback = typedef std::move_only_function<void()>

Callable block used by tests, suites, and hooks.

The callback is move-only so test code can capture move-only resources.

Function Documentation

◆ active_registry()

test_registry & kaycxx::test::active_registry ( )

Returns the registry currently used by the public DSL functions.

The active registry is the default registry unless it has been changed explicitly. This is mainly useful for self-tests and custom embedding scenarios that need to collect DSL registrations in an isolated registry.

Returns
The active test registry.

◆ after_all() [1/2]

void kaycxx::test::after_all ( callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a hook that runs once after all tests in the current suite have finished.

Parameters
bodyThe hook callback to register.
locationSource location used when the hook throws an exception without its own location.

◆ after_all() [2/2]

void kaycxx::test::after_all ( hook  value)

Registers a hook that runs once after all tests in the current suite have finished.

Parameters
valueThe hook to register.

◆ after_each() [1/2]

void kaycxx::test::after_each ( callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a hook that runs after each test in the current suite and its child suites.

Hooks run from the innermost suite to the outermost suite.

Parameters
bodyThe hook callback to register.
locationSource location used when the hook throws an exception without its own location.

◆ after_each() [2/2]

void kaycxx::test::after_each ( hook  value)

Registers a hook that runs after each test in the current suite and its child suites.

Parameters
valueThe hook to register.

◆ assert_close()

template<typename Actual , typename Expected >
requires detail::close_supported<Actual, Expected>
void kaycxx::test::assert_close ( Actual const actual,
Expected const expected,
int  precision = 2,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that two numeric values are close enough.

Values are close when they are equal after rounding to the given number of decimal digits. The default precision is 2 decimal digits.

Template Parameters
ActualThe actual value type.
ExpectedThe expected value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
precisionThe number of decimal digits to round to before comparing.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the values are not close enough.

◆ assert_contain()

template<typename Actual , typename Expected >
requires detail::contains_supported<Actual, Expected>
void kaycxx::test::assert_contain ( Actual const actual,
Expected const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that a string contains a substring or character, or that a range contains an element.

String-like values use substring semantics. Other ranges use std::ranges::find and therefore check whether one element compares equal to the expected value.

Template Parameters
ActualThe actual value type.
ExpectedThe expected contained value type.
Parameters
actualThe actual value to check.
expectedThe expected contained value.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value does not contain the expected value.

◆ assert_equal()

template<typename Actual , typename Expected >
requires detail::equals_supported<Actual, Expected>
void kaycxx::test::assert_equal ( Actual const actual,
Expected const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that two values are equal.

Non-string values must have the same unqualified type and are compared with operator==. C string forms may be compared with each other and with std::string or std::string_view values, and are compared by text content instead of pointer identity.

Template Parameters
ActualThe actual value type.
ExpectedThe expected value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the values are not equal.

◆ assert_false()

void kaycxx::test::assert_false ( bool  actual,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument is false.

Parameters
actualThe actual value to check.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value is not false.

◆ assert_greater()

template<detail::ordered_value T>
void kaycxx::test::assert_greater ( T const actual,
T const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the actual value is greater than the expected value.

The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.

Template Parameters
TThe ordered value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value is not greater than the expected value.

◆ assert_greater_or_equal()

template<detail::ordered_value T>
void kaycxx::test::assert_greater_or_equal ( T const actual,
T const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the actual value is greater than or equal to the expected value.

The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.

Template Parameters
TThe ordered value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value is less than the expected value.

◆ assert_less()

template<detail::ordered_value T>
void kaycxx::test::assert_less ( T const actual,
T const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the actual value is less than the expected value.

The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.

Template Parameters
TThe ordered value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value is not less than the expected value.

◆ assert_less_or_equal()

template<detail::ordered_value T>
void kaycxx::test::assert_less_or_equal ( T const actual,
T const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the actual value is less than or equal to the expected value.

The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.

Template Parameters
TThe ordered value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value is greater than the expected value.

◆ assert_match() [1/2]

template<detail::string_like T>
void kaycxx::test::assert_match ( T const actual,
std::regex const regex,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument matches the given regular expression.

Template Parameters
TThe string-like value type.
Parameters
actualThe actual value to check.
regexThe regular expression to match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value does not match the regular expression.

◆ assert_match() [2/2]

template<detail::string_like T>
void kaycxx::test::assert_match ( T const actual,
std::string_view  pattern,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument matches the given pattern.

Template Parameters
TThe string-like value type.
Parameters
actualThe actual value to check.
patternThe pattern to match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value does not match the pattern.

◆ assert_not_close()

template<typename Actual , typename Expected >
requires detail::close_supported<Actual, Expected>
void kaycxx::test::assert_not_close ( Actual const actual,
Expected const expected,
int  precision = 2,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that two numeric values are not close enough.

Values are close when they are equal after rounding to the given number of decimal digits. The default precision is 2 decimal digits.

Template Parameters
ActualThe actual value type.
ExpectedThe expected value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
precisionThe number of decimal digits to round to before comparing.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the values are close enough.

◆ assert_not_contain()

template<typename Actual , typename Expected >
requires detail::contains_supported<Actual, Expected>
void kaycxx::test::assert_not_contain ( Actual const actual,
Expected const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that a string does not contain a substring or character, or that a range does not contain an element.

String-like values use substring semantics. Other ranges use std::ranges::find and therefore check whether one element compares equal to the expected value.

Template Parameters
ActualThe actual value type.
ExpectedThe unexpected contained value type.
Parameters
actualThe actual value to check.
expectedThe unexpected contained value.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the actual value contains the expected value.

◆ assert_not_equal()

template<typename Actual , typename Expected >
requires detail::equals_supported<Actual, Expected>
void kaycxx::test::assert_not_equal ( Actual const actual,
Expected const expected,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that two values are not equal.

Non-string values must have the same unqualified type and are compared with operator==. C string forms may be compared with each other and with std::string or std::string_view values, and are compared by text content instead of pointer identity.

Template Parameters
ActualThe actual value type.
ExpectedThe expected value type.
Parameters
actualThe actual value to check.
expectedThe expected value to compare to.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the values are equal.

◆ assert_not_match() [1/2]

template<detail::string_like T>
void kaycxx::test::assert_not_match ( T const actual,
std::regex const regex,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument does not match the given regular expression.

Template Parameters
TThe string-like value type.
Parameters
actualThe actual value to check.
regexThe regular expression not to match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value matches the regular expression.

◆ assert_not_match() [2/2]

template<detail::string_like T>
void kaycxx::test::assert_not_match ( T const actual,
std::string_view  pattern,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument does not match the given pattern.

Template Parameters
TThe string-like value type.
Parameters
actualThe actual value to check.
patternThe pattern not to match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value matches the pattern.

◆ assert_not_null()

template<detail::nullable T>
void kaycxx::test::assert_not_null ( T const actual,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument is not null.

Template Parameters
TThe nullable value type.
Parameters
actualThe actual value to check.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value is null.

◆ assert_not_throw() [1/3]

template<detail::exception_type Exception, std::invocable Function>
void kaycxx::test::assert_not_throw ( Function &&  function,
std::regex const &  unexpected_message,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the function does not throw an exception of the given type with a message matching the regex.

Derived exception types match the unexpected type, like instanceof in JavaScript. Other exception types or non-matching messages are allowed.

Template Parameters
ExceptionThe unexpected exception type.
FunctionThe function type.
Parameters
functionThe function to call.
unexpected_messageThe regular expression the exception message must not match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the function throws the matching exception.

◆ assert_not_throw() [2/3]

template<std::invocable Function>
void kaycxx::test::assert_not_throw ( Function &&  function,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the function does not throw any exception.

Template Parameters
FunctionThe function type.
Parameters
functionThe function to call.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the function throws.

◆ assert_not_throw() [3/3]

template<detail::exception_type Exception, std::invocable Function>
void kaycxx::test::assert_not_throw ( Function &&  function,
std::string_view  unexpected_message,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the function does not throw an exception of the given type with the exact unexpected message.

Derived exception types match the unexpected type, like instanceof in JavaScript. Other exception types or different messages are allowed.

Template Parameters
ExceptionThe unexpected exception type.
FunctionThe function type.
Parameters
functionThe function to call.
unexpected_messageThe exact unexpected exception message.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the function throws the matching exception.

◆ assert_null()

template<detail::nullable T>
void kaycxx::test::assert_null ( T const actual,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument is null.

Template Parameters
TThe nullable value type.
Parameters
actualThe actual value to check.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value is not null.

◆ assert_throw() [1/2]

template<detail::exception_type Exception, std::invocable Function>
void kaycxx::test::assert_throw ( Function &&  function,
std::regex const expected_message,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the function throws an exception of the expected type with a message matching the regex.

Derived exception types match the expected type, like instanceof in JavaScript. The expected exception type must derive from std::exception because assert_throw checks the thrown message through what().

Template Parameters
ExceptionThe expected exception type.
FunctionThe function type.
Parameters
functionThe function to call.
expected_messageThe regular expression the exception message must match.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the function does not throw, throws the wrong type, or throws a wrong message.

◆ assert_throw() [2/2]

template<detail::exception_type Exception, std::invocable Function>
void kaycxx::test::assert_throw ( Function &&  function,
std::string_view  expected_message,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the function throws an exception of the expected type with the exact expected message.

Derived exception types match the expected type, like instanceof in JavaScript. The expected exception type must derive from std::exception because assert_throw checks the thrown message through what().

Template Parameters
ExceptionThe expected exception type.
FunctionThe function type.
Parameters
functionThe function to call.
expected_messageThe exact expected exception message.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the function does not throw, throws the wrong type, or throws a wrong message.

◆ assert_true()

void kaycxx::test::assert_true ( bool  actual,
std::string_view  reason = {},
std::optional< std::source_location >  location = std::source_location::current() 
)

Asserts that the argument is true.

Parameters
actualThe actual value to check.
reasonOptional reason added to the assertion error.
locationOptional source location of the assertion call.
Exceptions
assertion_errorWhen the value is not true.

◆ before_all() [1/2]

void kaycxx::test::before_all ( callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a hook that runs once before any tests in the current suite are executed.

Parameters
bodyThe hook callback to register.
locationSource location used when the hook throws an exception without its own location.

◆ before_all() [2/2]

void kaycxx::test::before_all ( hook  value)

Registers a hook that runs once before any tests in the current suite are executed.

Parameters
valueThe hook to register.

◆ before_each() [1/2]

void kaycxx::test::before_each ( callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a hook that runs before each test in the current suite and its child suites.

Hooks run from the outermost suite to the innermost suite.

Parameters
bodyThe hook callback to register.
locationSource location used when the hook throws an exception without its own location.

◆ before_each() [2/2]

void kaycxx::test::before_each ( hook  value)

Registers a hook that runs before each test in the current suite and its child suites.

Parameters
valueThe hook to register.

◆ default_registry()

test_registry & kaycxx::test::default_registry ( )

Returns the global registry used by the public DSL functions.

Returns
The default test registry.

◆ describe()

void kaycxx::test::describe ( std::string_view  description,
callback  body,
std::source_location  location = std::source_location::current() 
)

Defines a nested test suite and immediately executes its registration body.

Tests and hooks registered inside the body are added to the nested suite.

Parameters
descriptionHuman-readable suite description.
bodyRegistration callback that defines child suites, tests, and hooks.
locationSource location of the describe() call.

◆ it() [1/2]

void kaycxx::test::it ( std::string_view  description,
callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a test case in the current suite.

Parameters
descriptionHuman-readable test description.
bodyTest callback to execute when the test case runs.
locationSource location of the test registration.

◆ it() [2/2]

void kaycxx::test::it ( std::string_view  description,
skip_condition  skip_condition,
callback  body,
std::source_location  location = std::source_location::current() 
)

Registers a test case in the current suite with a skip condition.

The skip condition is checked before before_each hooks and test code are executed.

Parameters
descriptionHuman-readable test description.
skip_conditionCondition deciding if the test should be skipped.
bodyTest callback to execute when the test case runs.
locationSource location of the test registration.

◆ run_tests() [1/4]

int kaycxx::test::run_tests ( )

Runs the global test registry and writes the report to standard output.

Returns
Process exit code 0 when all tests pass, otherwise 1.

◆ run_tests() [2/4]

int kaycxx::test::run_tests ( int  argc,
char argv[] 
)

Runs the global test registry according to command-line arguments.

Positional path filters select tests by source file or directory. The repeatable -t and –test-name-pattern options select full descriptions with regular expressions. Supported actions are –help and –write-ctest.

Parameters
argcNumber of command-line arguments.
argvCommand-line argument values.
Returns
Process exit code 0 when the command succeeds, otherwise non-zero.

◆ run_tests() [3/4]

int kaycxx::test::run_tests ( std::ostream &  output)

Runs the global test registry and writes the report to the given stream.

Parameters
outputOutput stream receiving the test report.
Returns
Process exit code 0 when all tests pass, otherwise 1.

◆ run_tests() [4/4]

int kaycxx::test::run_tests ( test_registry registry,
std::ostream &  output 
)

Runs the given test registry and writes the report to the given stream.

Parameters
registryRegistry containing the suites to run.
outputOutput stream receiving the test report.
Returns
Process exit code 0 when all tests pass, otherwise 1.

◆ set_active_registry()

test_registry & kaycxx::test::set_active_registry ( test_registry registry)

Changes the registry used by the public DSL functions.

Parameters
registryRegistry to use for following DSL registrations.
Returns
The previously active registry.

◆ skip()

skip_condition kaycxx::test::skip ( std::string_view  reason = "")

Creates a condition that always skips the test.

Parameters
reasonOptional reason why the test should be skipped.
Returns
The skip condition.

◆ skip_if()

skip_condition kaycxx::test::skip_if ( std::move_only_function< bool()>  condition,
std::string_view  reason = "" 
)

Creates a condition that skips the test when the callback returns true.

Parameters
conditionCallback returning true when the test should be skipped.
reasonOptional reason why the test should be skipped.
Returns
The skip condition.

◆ skip_unless()

skip_condition kaycxx::test::skip_unless ( std::move_only_function< bool()>  condition,
std::string_view  reason = "" 
)

Creates a condition that skips the test unless the callback returns true.

Parameters
conditionCallback returning true when the test should run.
reasonOptional reason why the test should be skipped.
Returns
The skip condition.