ztest: add a weak implementation of test_main()

Introduce a weak implementation of test_main() which calls:
* ztest_run_registered_test_suites(NULL);
* ztest_verify_all_registered_test_suites_ran();

This will attempt to run all registered test suites and verify that
they each ran.

Signed-off-by: Yuval Peress <peress@chromium.org>
This commit is contained in:
Yuval Peress 2021-08-01 22:12:44 -06:00 committed by Anas Nashif
commit 27f6a5e07d
8 changed files with 106 additions and 9 deletions

View file

@ -92,6 +92,17 @@ extern struct ztest_suite_node _ztest_suite_node_list_end[];
*/
int ztest_run_registered_test_suites(const void *state);
/**
* @brief Fails the test if any of the registered tests did not run.
*
* When registering test suites, a pragma function can be provided to determine WHEN the test should
* run. It is possible that a test suite could be registered but the pragma always prevents it from
* running. In cases where a test should make sure that ALL suites ran at least once, this function
* may be called at the end of test_main(). It will cause the test to fail if any suite was
* registered but never ran.
*/
void ztest_verify_all_registered_test_suites_ran(void);
/**
* @brief Run a test suite.
*

View file

@ -465,6 +465,29 @@ int ztest_run_registered_test_suites(const void *state)
return count;
}
void ztest_verify_all_registered_test_suites_ran(void)
{
bool all_tests_run = true;
struct ztest_suite_node *ptr;
for (ptr = _ztest_suite_node_list_start; ptr < _ztest_suite_node_list_end; ++ptr) {
if (ptr->stats.run_count < 1) {
PRINT("ERROR: Test '%s' did not run.\n", ptr->name);
all_tests_run = false;
}
}
if (!all_tests_run) {
test_status = 1;
}
}
void __weak test_main(void)
{
ztest_run_registered_test_suites(NULL);
ztest_verify_all_registered_test_suites_ran();
}
#ifndef KERNEL
int main(void)
{