Commit graph

272 commits

Author SHA1 Message Date
Al Semjonovs
b27c5d73ef ztest: Fix unused variable compile error in shuffle function
When CONFIG_ZTEST_SHUFFLE is enabled and ASSERTS are disabled
`start_pos` becomes an unused variable leading to a compile error.
Cleaned-up shuffling algorithm to not need a `start_pos` check.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2022-09-14 20:13:46 -04:00
Michał Barnaś
36f74e9837 ztest: remove msg parameter from zassert_* and zassume_* macros
This commit removes the requirement for msg parameter in zassert_*
and zassume_* macros. It will allow to use them without sending NULL
as the last parameter.

The default messages look like:

void* beauty_ptr = NULL;
zassert_not_null(beauty_ptr);
=>
Assertion failed at main.c:20: suite_test: beauty_ptr is NULL

void* ugly_ptr = (void*)0xbaadf00d;
zassume_is_null(ugly_ptr);
=>
Assumption failed at main.c:23: suite_test: ugly_ptr is not NULL

int apple = 3;
int tomato = 9;
zassume_equal(apple, tomato);
=>
Assumption failed at main.c:27: suite_test: apple not equal to tomato

Signed-off-by: Michał Barnaś <mb@semihalf.com>
2022-09-09 07:05:38 -04:00
Michał Barnaś
305ee1816a ztest: prepare zassert and zassume macro to be used without message
This commit adds intermediate macros that will allow to use the
zassert and zassume macros without setting the msg parameter as NULL
if it isn't used.

Signed-off-by: Michał Barnaś <mb@semihalf.com>
2022-09-09 07:05:38 -04:00
Michał Barnaś
ce5985b9a1 ztest: improve comments for ztest assert macros
Move params in comment about zassert and zassume macros.

Signed-off-by: Michał Barnaś <mb@semihalf.com>
2022-09-09 07:05:38 -04:00
Krzysztof Chruscinski
77617b0d98 testsuite: ztress: First sys_rand_get32 from thread
Added a workaround to call random generator once during the
initialization. It is done to handle XOSHIRO generator limitation
which performs initialization in the first sys_rand32_get call.
And for some entropy generators it cannot be done from an interrupt
context and it happens if k_timer context is used which expires first.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-09-08 05:43:17 -04:00
Gerard Marull-Paretas
79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00
Ming Shao
5b84b0ac87 ztress: Coherence fix for tests/lib/spsc_pbuf
When CONFIG_KERNEL_COHERENCE=y, it's illegal to place shared
data(like ztress_context_data in this test case) on the stack,
because that memory is incoherent with respect to other CPUs
in the system.

In this specific case, it will cause PC register to load invalid
handler function pointer and crash the Xtensa processor.

Make it static.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-09-01 10:27:08 +02:00
Ming Shao
be392fc23b ztress: fix the progress stats
Current ztress design supports both timer context and thread
context. The timer context always uses the first element of
several global array variables like exec_cnt[] and backoff[].
But progress_timeout() is using the last element of exec_cnt[]
to calculate the progress of timer context. It is not right.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-08-29 10:32:10 +02:00
Yuval Peress
61fb681036 ztest: Match cleanup ordering between unittest and normal tests
Tests with KERNEL enabled perform their cleanup logic after the suite's
after and test rules are executed. Unittests should do the same.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
5ceba48473 ztest: Safely handle fail/skip/pass outside tests
Updates the implementation when KERNEL is available to safely bail on
the test when the test calls fail, skip, or pass during invalid test
phases. Print a detailed message, and skip all other tests. The test
run will be marked as failed.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
20475bff0e ztest: move get_friendly_phase_name to common code
Move the function used for printing the phase name up so its available
for both unittest and KERNEL mode of tests.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
5b6b304e3c ztest: unittest: Fix fail/skip/pass outside of tests
When not running in setup, before, or in the test. Calling fail, skip,
or pass is invalid and should be considered an error. Properly handle
these cases by printing a more detailed error message and bailing the
process.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
2a110cc473 ztest: unittest: Add before/after phase
Add missing setters for the before/after phase when running without
KERNEL.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
08db645d81 ztest: move end_report implementation
Move the implementation of end_report so it can be used by both code
paths (with and without KERNEL).

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Yuval Peress
cdb961eac6 ztest: remove init_testing
When KERNEL was not defined (unittest), the call to init_testing was
used to set a longjump target using 'stack_fail'. When triggered,
this was actually causing a segfault, because longjmp is only valid
if going directly up the stack. Since init_testing returned, it was
no longer on the stack. Instead, that logic MUST be inlined.

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-29 10:27:00 +02:00
Gerson Fernando Budke
b0564dfd97 zephyr/ztest_assert.h: Fix implicit to bool conversion
The current zassert macro uses implicit conversion to boolean which
has implication on analysis tools like clang-tidy-14. This add an
aditional step to create a boolean value for the evaluation instead
use the string direct which allows run analysis tool without this
warning/error.

Signed-off-by: Gerson Fernando Budke <nandojve@gmail.com>
2022-08-24 10:06:53 +02:00
Torsten Rasmussen
7a65bdbd64 cmake: kconfig: introduce dedicated unit testing board
This commit introduces a dedicated unit testing board.

Today, a dedicated Zephyr unit testing scheme exists but is different
from how a Zephyr build generally works.

For example Kconfig is not possible, resulting on various different
hacks to pass Kconfig settings from test cases / testcase.yaml through
CMake to the code.
Some directly as compile definitions, some as header files with forced
inclusion on sources, some with wrapper flags which again results in
different define being enabled. There is even cases where a second
forced header inclusion undefines previous defines.

Unit test often does a manual check for the right boards, like this:
> if (NOT BOARD STREQUAL unit_testing)
>    message(FATAL_ERROR "This project can only be used with...")
> endif()

Introducing a dedicated unit_testing board under `tests/root` allows
us to use Kconfig in unit test samples, and thus proper `prj.conf` and
extra Kconfig fragments.
Generation of autoconf.h so the overall architecture follows regular
Zephyr builds.

Proper and uniform error messages when invalid board is selected.

The unit_testing board and arch is located under: `subsys/testsuite` so
that it is only available when find_package(Zephyr COMPONENTS unittest)
is used, and not available for regular Zephyr builds.

Kconfig generates autoconf.h which is applied as compile flag to
test binary which means that kconfig defines placed in ztest.h can now
be removed.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2022-08-18 14:29:14 +02:00
Aaron Massey
2bc23e0543 ztest: Add docstring for zassume macro
Add a detailed docstring to the zassume() macro that describes what it does
and how its usage is different from zassert.

Signed-off-by: Aaron Massey <aaronmassey@google.com>
2022-08-17 18:02:26 +02:00
Ming Shao
9a4b5e1d90 ztest: Add test duration to summary for new ztest
Add duration stats at both suite level and unit test
level into test summary. The duration is at second
level. Since the new ztest fx can execute a test suite
for multiple times, the worst/longest test duration is
collected.

Note that even a skipped test can have a duration greater
than 0 because the skip operation itself is not free.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-08-11 09:01:12 -04:00
Ming Shao
cce1ec6f42 ztest: Refine when to collect unit test duration
Previously, unit test duration is collected within the
TC_START and Z_TC_END_RESULT macros. With existing tests,
the TC_START macro can be invoked by both the ztest fx
and the tests themselves. And the TC_START macro definition
went lengths to avoid the interference when it is invoked
within a unit test. This commit decouple the time collection
and the TC_STRAT/Z_TC_END_RESULT macros to fix this issue.
Now only the (old) ztest framework is responsible for the
test duration measure. The test duration stats of new ztest
fx is different from this btw.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-08-11 09:01:12 -04:00
Ming Shao
77e1e39cff ztest: add test summary after all suites finish running
Add test summary after all test suites finish running.
The summary can be one-line or verbose, which is configured
with CONFIG_ZTEST_VERBOSE_SUMMARY. The one-line summary covers
overall suite stats. The verbose summary covers each test
function within the suite besides the one-line summary.

The new ztest output ultimately go through the printk. If
printk go through the logging subsystem, there may be log
messages dropped. And if log_panic is invoked, log messages
can be flushed in a mess. So several explicit log flush
are used when printing summary to ensure no content is lost
and content is in good shape.

Some macros are shared between old and new ztests. Such as
TC_START_PRINT and TC_END_PRINT. The are defined accordingly.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-08-11 09:01:12 -04:00
Ming Shao
bb63d87b4b ztest: Add config switch for modes of test output
Support verbose or one-line summary at test suite level.
Support verbose or no output at test function level.

Totally 4 combinations configurable:
- function verbose + suite verbose
- function verbose + suite oneline
- no function output + suite verbose
- no function output + suite oneline

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-08-11 09:01:12 -04:00
Yuval Peress
84dfb8edf8 ztest: allow asserts anywhere
Updates the ztest_test_fail() function to allow failures in setup.
When executed, a failed assert will fail every test in the suite owning
the setup function. This was verified by adding a suite which asserts
in the setup function and has a test that should pass. During
exeuction, ztest marks the test as failing.

In order to verify exection I also added 2 new APIs:
- ZTEST_EXPECT_FAIL(suite_name, test_name)
- ZTEST_EXPECT_SKIP(suite_name, test_name)

Signed-off-by: Yuval Peress <peress@google.com>
2022-08-09 13:30:15 -04:00
Al Semjonovs
95cae9b870 zephyr: Fix verify run all check
In scenarios where test_main is overridden ztest_run_all
may be invoked multiple times leading to the verify check to
fail inadvertently.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2022-08-09 08:49:28 -04:00
Al Semjonovs
ccc0315e14 ztest: Handle assert failures without signal handler
The signal handler needs to return gracefully. When multiple
tests assert, the first assert will raise SIGABRT and the signal
handler will run and the test will stop running.
The second assert will raise SIGABRT but the signal handler
will not be called, therefore the test PASS/FAIL status is not
updated correctly.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2022-08-05 12:51:44 -04:00
Al Semjonovs
4a0040f346 ztest: Fix logic for SUITE_SKIP
When ZTEST setup fails z_assume, we should mark all tests within
suite as skipped.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2022-08-05 11:54:11 -04:00
Al Semjonovs
1463e1c26a ztest: Fix logic for listing tests once
A test case may invoke run_all multiple times, causing
logic for listing tests to run tests on second call.

Signed-off-by: Al Semjonovs <asemjonovs@google.com>
2022-08-03 14:32:54 -04:00
Anas Nashif
51cb40e5b3 tests: ztest: move TEST_LOGGING_FLUSH_AFTER_TEST under ztest
This config option is only used with ztest, so move it under ztest.
Otherwise it appears in .config when you build ANY application.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-08-03 11:22:20 +02:00
Fabio Baltieri
def230187b test: fix more legacy #include paths
Add a bunch of missing "zephyr/" prefixes to #include statements in
various test and test framework files.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2022-08-02 16:41:41 +01:00
Stephanos Ioannidis
e7263b19e1 Revert "ztest: Add config switch for modes of test output"
This reverts commit 5a850a5d06, which
broke multiple tests in the CI.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-29 15:44:32 -04:00
Stephanos Ioannidis
caed3931a4 Revert "ztest: add test summary after all suites finish running"
This reverts commit 0456ddc27d, which
broke multiple tests in the CI.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-29 15:44:32 -04:00
Stephanos Ioannidis
a2e2569684 testsuite: ztress: Remove idle thread NULL check
This commit removes the NULL check on the idle thread array
(`idle_tid`) because it is statically declared and will never be NULL.

This fixes the "comparison will always evaluate as ‘false’" warning
generated by the GCC 12.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2022-07-29 08:45:02 -04:00
Ming Shao
0456ddc27d ztest: add test summary after all suites finish running
Add test summary after all test suites finish running.
The summary can be one-line or verbose, which is configured
with CONFIG_ZTEST_VERBOSE_SUMMARY. The one-line summary covers
overall suite stats. The verbose summary covers each test
function within the suite besides the one-line summary.

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-07-28 18:07:01 -04:00
Ming Shao
5a850a5d06 ztest: Add config switch for modes of test output
Support verbose or one-line summary at test suite level.
Support verbose or no output at test function level.

Totally 4 combinations configurable:
- function verbose + suite verbose
- function verbose + suite oneline
- no function output + suite verbose
- no function output + suite oneline

Signed-off-by: Ming Shao <ming.shao@intel.com>
2022-07-28 18:07:01 -04:00
Yuval Peress
b513e621b0 docs: ztest: Update documentation for new API
Documentation now includes:
- How to create a test suite
- How to use predicates
- How to use setup/before/after/teardown
- How to use test rules
- Direct people to use FFF instead of ztest mock

Fixes #47420

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-15 22:25:41 +00:00
Anas Nashif
32c22cc63a ztest: generate symbols for tests
This will help with parsing testcases in test binaries.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-07-11 11:56:39 +02:00
Andrei Emeltchenko
5b633db288 ztest: Add Z_TEST_SKIP_IFNDEF macro
Add macro Z_TEST_SKIP_IFNDEF which is used when you want to skip test
if configuration option is not enabled.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
2022-07-07 15:07:02 -04:00
Yuval Peress
10feb693f5 unittest: support ztest_new_api
Add support for the new Ztest API when using unittest.

Fixes #47420

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-07 18:22:14 +02:00
Aaron Massey
83253023cf ztest: Track skipping test versus entire suite
The addition of the assumption API introduced a bug where a skipped test
would subsequently skip later tests within the same suite. A skipped test
was being treated as an entire skipped test suite.

Keep track of tests being skipped due to skipping in the suite setup versus
being skipped within their test function.

TEST=west build -p -b native_posix tests/kernel/condvar/condvar_api/ -t run
TEST=twister -T tests/ztest -p native_posix

Signed-off-by: Aaron Massey <aaronmassey@google.com>
2022-07-06 18:50:35 -04:00
Anas Nashif
718750f962 fs/testsuite: add mising braces to single line if statements
Following zephyr's style guideline, all if statements, including single
line statements shall have braces.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2022-07-06 11:00:45 -04:00
Yuval Peress
e9e030f56b ztest: Add zassume* API
Add an assume API which works like JUnit's. Assumptions can be made
at any point before your test returns (setup, before, and during the
test). If an assumption fails, the test will be marked as skipped.

This helps avoid a cascading affect of failed tests when a base
feature breaks. The feature is expected to have tests and the tests
which depend on it should be skipped (not failed) if that feature
is broken.

Issue #42472

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-04 14:16:34 -04:00
Yuval Peress
16b9fdf6a0 ztest: run clang-format for easier diffs
Run clang-format on ztest-new.c to make diffs a bit easier

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-04 14:16:34 -04:00
Yuval Peress
54adba4633 ztest: use an enum for the test result
Update `test_result` to use an enum for better code clarity

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-04 14:16:34 -04:00
Yuval Peress
e26ea1b936 ztest: run clang-format on ztest_assert.h
Simple pass to make the diff easier to read for additional changes.

Signed-off-by: Yuval Peress <peress@google.com>
2022-07-04 14:16:34 -04:00
Krzysztof Chruscinski
2153c91877 testsuite: ztress: Add test skipping if system clock is too high
When system clock is set globally for the test which is executed
on various qemu platforms it may happen that clock is set too
high compared to CPU clock frequency. In that case test may stuck.
Added check and test skipping in that case.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-07-04 15:50:53 +02:00
Keith Packard
34473f6821 subsys/testsuite: Don't add empty z_malloc_partition to mem domain
The malloc partition may actually be empty, if so, don't try to add it to
the domain as that will generate an error.

Signed-off-by: Keith Packard <keithp@keithp.com>
2022-06-30 10:33:24 +02:00
Yuval Peress
a7ceba3710 testing: add zephyr/ prefix to testing include path
Continuation of issue #41543 to add prefix scope for Zephyr's include
files.

Signed-off-by: Yuval Peress <peress@google.com>
2022-06-29 06:04:32 -04:00
Krzysztof Chruscinski
7f1b4f88e3 logging: Rename files and test from log_msg2 to log_msg
Renaming log_msg2 files to log_msg.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-23 09:10:33 +02:00
Benjamin Gwin
ee22ae79a4 testsuite: Make ztest_error_hook compatible with C++
This lets ztest error hooks be used from C++ based tests.

Signed-off-by: Benjamin Gwin <bgwin@google.com>
2022-06-22 19:30:06 -04:00
Yuval Peress
d813e9b39f ztest: Fix incorrect use of this as fixture
Update `this` to `fixture` to avoid C++ keyword error.

Fixes #46459

Signed-off-by: Yuval Peress <peress@google.com>
2022-06-16 16:13:18 -04:00