Skip to content

ztest teardown function not called if test function is interrupted #16329

@rakons

Description

@rakons

Describe the bug
If a test function fails (call to ztest_test_fail) or a ztest_test_pass is called from the function no teardown function is called.

To Reproduce
It is really enough to look into the code, but I may provide some tests if really required.

Expected behavior
Teardown function should be called even if ztest_test_fail) or a ztest_test_pass is called from the test function body.

Impact
annoyance

Environment (please complete the following information):

  • Unit test on local machine, it means without a Kernel.

Additional context
The problem is that test teardown is called linearly here:

static void run_test_functions(struct unit_test *test)
{
	phase = TEST_PHASE_SETUP;
	test->setup();
	phase = TEST_PHASE_TEST;
	test->test();
	phase = TEST_PHASE_TEARDOWN;
	test->teardown();
	phase = TEST_PHASE_FRAMEWORK;
}

While calling ztest_test_fail or ztest_test_fail pass would finally lead to longjump into run_test function from witch run_test_functions is called.
As an effect - the line test->teardown(); would be never reached.

To solve this it should be enough to move the teardown call into run_test function just after out: label.

Metadata

Metadata

Assignees

Labels

area: TestsIssues related to a particular existing or missing testbugThe issue is a bug, or the PR is fixing a bugpriority: mediumMedium impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions