testsuite: coverage: Add CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS

Add an option to reset gcov counters before running tests.
This ensures that only code lines triggered by test itself are counted in
the coverage report and all the board initialization code and pre-test
bootstrap is not counted. This is useful when, for example, you are
testing code that is also executed during bootup

Test Plan:
west build -p always -b qemu_x86_64 tests/ztest/base/ -- \
-DCONFIG_COVERAGE=y -DCONFIG_COVERAGE_GCOV=y -DCONFIG_COVERAGE_DUMP=y \
-DCONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS=y
ninja -Cbuild run | tee log.log

Signed-off-by: Roman Studenikin <srv@meta.com>
This commit is contained in:
Roman Studenikin 2024-06-05 12:54:33 -07:00 committed by Johan Hedberg
parent 179585e54b
commit fdd81f87c2
5 changed files with 36 additions and 1 deletions

View File

@ -259,6 +259,22 @@ void gcov_reset_counts(struct gcov_info *info)
}
}
void gcov_reset_all_counts(void)
{
struct gcov_info *gcov_list = NULL;
k_sched_lock();
gcov_list = gcov_get_list_head();
while (gcov_list) {
gcov_reset_counts(gcov_list);
gcov_list = gcov_list->next;
}
k_sched_unlock();
}
void dump_on_console_start(const char *filename)
{
printk("\n%c", FILE_START_INDICATOR);

View File

@ -124,6 +124,6 @@ struct gcov_info {
struct gcov_info *gcov_get_list_head(void);
size_t gcov_populate_buffer(uint8_t *buffer, struct gcov_info *info);
size_t gcov_calculate_buff_size(struct gcov_info *info);
void gcov_reset_counts(struct gcov_info *info);
void gcov_reset_all_counts(void);
#endif /* _COVERAGE_H_ */

View File

@ -7,6 +7,7 @@ zephyr_syscall_header(
zephyr_include_directories(
${ZEPHYR_BASE}/subsys/testsuite/include
${ZEPHYR_BASE}/subsys/testsuite/coverage
${ZEPHYR_BASE}/subsys/testsuite/ztest/include
)

View File

@ -132,6 +132,16 @@ config ZTEST_VERIFY_RUN_ALL
help
This rule will fail the project if not all tests have been run.
config ZTEST_COVERAGE_RESET_BEFORE_TESTS
bool "Performs coverage counters reset"
depends on COVERAGE_GCOV
help
This rule will reset gcov counters before running tests. This ensures
that only code lines triggered by test itself are counted in the coverage report
and all the board initialization code and pre-test bootstrap is not counted.
This is useful when, for example, you are testing code that is also executed during bootup.
config ZTEST_SHUFFLE
bool "Shuffle the order of tests and suites"
select TEST_RANDOM_GENERATOR if !ENTROPY_HAS_DRIVER

View File

@ -38,6 +38,10 @@ static bool failed_expectation;
#define NUM_ITER_PER_TEST 1
#endif
#ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
#include <coverage.h>
#endif
/* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test */
/**
@ -1082,6 +1086,10 @@ int z_impl_ztest_run_test_suites(const void *state, bool shuffle, int suite_iter
return count;
}
#ifdef CONFIG_ZTEST_COVERAGE_RESET_BEFORE_TESTS
gcov_reset_all_counts();
#endif
#ifdef CONFIG_ZTEST_SHUFFLE
struct ztest_suite_node *suites_to_run[ZTEST_SUITE_COUNT];