Skip to content

Commit f34f425

Browse files
author
Yuval Peress
committed
ztest: run before function in test thread
A lot of tests need to be able to get their current tid and do some action with it. It makes sense for the `before` function/rule to be able to run in the same thread as the test. Note that the `after` function does not run in the same thread because we need to guarantee that it will run. Signed-off-by: Yuval Peress <[email protected]>
1 parent 27cf263 commit f34f425

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

subsys/testsuite/ztest/src/ztest_new.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ static void test_cb(void *a, void *b, void *c)
369369
struct ztest_unit_test *test = b;
370370

371371
test_result = 1;
372+
if (suite->before) {
373+
suite->before(/*data=*/c);
374+
}
375+
run_test_rules(/*is_before=*/true, test, /*data=*/c);
372376
run_test_functions(suite, test, c);
373377
test_result = 0;
374378
}
@@ -380,10 +384,6 @@ static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test
380384
TC_START(test->name);
381385

382386
phase = TEST_PHASE_BEFORE;
383-
if (suite->before) {
384-
suite->before(data);
385-
}
386-
run_test_rules(/*is_before=*/true, test, data);
387387

388388
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
389389
k_thread_create(&ztest_thread, ztest_thread_stack,
@@ -399,6 +399,10 @@ static int run_test(struct ztest_suite_node *suite, struct ztest_unit_test *test
399399
k_thread_join(&ztest_thread, K_FOREVER);
400400
} else {
401401
test_result = 1;
402+
if (suite->before) {
403+
suite->before(data);
404+
}
405+
run_test_rules(/*is_before=*/true, test, data);
402406
run_test_functions(suite, test, data);
403407
}
404408

0 commit comments

Comments
 (0)