Commit graph zephyr/scripts/pylib
Author SHA1 Message Date
Anas Nashif
d84894ce7a twister: disable qemu with robot
Temporarily disable qemu with robot. Some qemu jobs are not being
terminated correctly slowing CI execution.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-05-27 21:18:08 -04:00
Anas Nashif
3eceb2e99e tests: twister: fix unit tests for robot support
Fix to support other simulators.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-05-27 08:32:42 +02:00
Anas Nashif
6489b436e9 twister: add qemu support using the Robot test framework
Now we are able to run tests using the Robot framework with Qemu.

Assisted-by: GitHub Copilot:claude-opus-4.6
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-05-27 08:32:42 +02:00
Anas Nashif
4867140218 twister: add support for running robot tests with native_sim
Support native sim with the Robot framework.

Assisted-by: GitHub Copilot:claude-opus-4.6
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-05-27 08:32:42 +02:00
Sebastian Modersohn
76c6c6020b scripts: west_commands: zspdx: handle ValueError of os.path.commonpath
os.path.commonpath will raise ValueError for paths with different
drives on Windows
Added new is_subpath method which handles ValueError and returns
False since it can't be a sub path. Replaces all 3 occurrences of
os.path.commonpath in walker.py.
Fixes #109641

Signed-off-by: Sebastian Modersohn <im24@msohn.de>
2026-05-26 15:24:36 +02:00
Parthiban Nallathambi
1df7cf5339 scripts: twister: tolerate qemu self-cleanup of qemu.pid
QEMUHandler.handle() reads the qemu pid file at the end of a run and
then unlinks it:

    if os.path.exists(self.pid_fn):
        with open(self.pid_fn) as pid_file:
            qemu_pid = int(pid_file.read())
        os.unlink(self.pid_fn)

The existence check, the open(), and the unlink() are not atomic.
When qemu exits on its own (slow benchmark hitting a harness
timeout, hostshutdown issued via the monitor, qemu killed by an
external signal, ...) it removes its own pid file. If that removal
lands between the os.path.exists() check and the read+unlink,
either open() or os.unlink() raises FileNotFoundError. The
exception escapes handle(), is caught in runner.py pipeline_mgr,
and the worker process exits, aborting the whole twister run.
Other tests that had not yet been scheduled never get to run, and
tests still in flight on other DUTs are abandoned.

Keep the existence check as the happy-path guard (so the no-pid-file
case still logs qemu_pid=None as before), and wrap the read+unlink
in a try/except FileNotFoundError to close the race window. A
missing pid file at cleanup time is not an error -- by definition
the qemu process is gone, which is exactly the post-condition the
cleanup is trying to reach.

Reproduced with a long-running benchmark in qemu where the guest
hit its harness eof and exited a few ms before twister got around
to the unlink. With this fix the failing test is marked FAILED and
the run continues; without it the pipeline aborts on the
FileNotFoundError.

Signed-off-by: Parthiban Nallathambi <parthiban@linumiz.com>
2026-05-26 15:23:59 +02:00
Gobi M
3a17a4f442 twister: make QEMU_BIN_PATH optional
QEMU_BIN_PATH is currently treated as a mandatory requirement for
QEMU-based tests on Windows.

Treat QEMU_BIN_PATH as an optional override and validate it only when
explicitly provided. This allows Twister to run QEMU tests using SDK or
other discovery mechanisms without requiring QEMU_BIN_PATH to be set.

Validation:
- QEMU_BIN_PATH unset -> qemu_x86 PASSED
- Invalid QEMU_BIN_PATH -> NOT RUN
- Valid QEMU_BIN_PATH -> qemu_x86 PASSED

Fixes #109332

Signed-off-by: Gobi M <gobi@aerlync.com>
2026-05-25 18:18:06 +02:00
Grzegorz Chwierut
019a118042 twister: Add testsuite option to skip build
Add a new testsuite YAML option `build` that allows a test
scenario to skip its own build step and instead use the build
artifacts from the first entry in `required_applications`.
Default is true; to skip building, set `build: false`.

This is useful for test scenarios that only run a harness (e.g.,
pytest, bsim) against an image built by another scenario, without
needing their own compiled binary.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-05-25 13:39:27 +02:00
Martin Jäger
f648774b71 twister: log non-numeric errors in catch_system_exit_exception
This helps to diagnose issues when sys.exit is called with a string
as a parameter (as done e.g. in scripts/list_hardware.py).

Signed-off-by: Martin Jäger <martin@libre.solar>
2026-05-21 06:38:40 -04:00
Grzegorz Chwierut
2a781b7ec0 twister: pytest: use sys.executable to invoke pytest subprocess
Replace the bare 'pytest' string in the Pytest harness command
with sys.executable and '-m' so the subprocess always uses the
same Python interpreter that runs twister. This avoids a
FileNotFoundError when pytest is not on PATH, which happens
e.g. when launching twister from a VS Code debug configuration
that does not activate the virtual environment.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-05-20 19:22:31 -07:00
Jeremy Truttmann
4b58a7d1e6 twister: allow --flash-timeout and --base-timeout to override config
The --flash-timeout and --base-timeout options in plugin.py had
hardcoded defaults of 60. This breaks the 'or' fallback chain at
twister_harness_config.py:140-141 since 60 is truthy and always
wins over values from the YAML config file.

Result: Command line timeout options were completely ignored when
config file values were present.

Changes:
- Set plugin.py defaults to None so the fallback chain works correctly
- Add explicit 60 fallback at the end of the chain in
  twister_harness_config.py to preserve default behavior
- Update test to use non-default value (120s) to verify config values

Signed-off-by: Jeremy Truttmann <jetstreeam.code@outlook.com>
2026-05-19 14:59:02 +01:00
Anas Nashif
900df338bf twister: support unified test definition file
Now use tests.yaml or tests.yml as the test definition file, sample.yaml
and testcase.yaml are deprecated but will still work for a while while
we transition.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2026-05-14 21:53:09 +02:00
Hake Huang
4b25c9b617 twister: fix packaged sysbuild artifacts for west flash
Rework the sysbuild artifact relocation approach based on
reviewer feedback: instead of rewriting domains.yaml with
relative paths at package time, keep the original absolute
paths and rebase them transparently in Domains.from_file()
when the file has been moved to a different location.

This makes the fix generic - it works for any consumer of
Domains.from_file() (west flash, twister, pytest-twister-
harness), not just the --package-artifacts path.

Changes:
- domains.py: update from_file() to detect when build_dir
  stored in domains.yaml differs from the file's actual
  directory and rebase all domain paths accordingly.
  Remove the now-unused _resolve_build_dir() helper and
  get_domains_with_build_dirs_relative_to() method.
- runner.py: remove _sanitize_domains_file() and its call
  site; path fixup is now handled by Domains.from_file().
- test_domains.py: replace the mocked __init__ approach
  with end-to-end assertions; add a 'relocated artifacts'
  test case that exercises the new rebasing logic.
- test_runner.py: remove test_projectbuilder_sanitize_
  domains_file() and references to the deleted method.

Signed-off-by: Hake Huang <hake.huang@nxp.com>
2026-05-12 17:12:06 +02:00
Grzegorz Chwierut
86c3c6eead twister: add script harness for script execution to enable bsim
Introduce a Script harness base class to enable running
shell-script-based tests through Twister, primarily
targeting BabbleSim test scenarios. The class provides
subprocess execution, output streaming, and log handling
that Pytest, Ctest, and Bsim now share through inheritance.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-05-12 11:25:32 +02:00
Etienne Carriere
ac5f115bc8 twister: add dt_label_compat_enabled()
Add dt_label_compat_enabled() to filter targets that embed a node
label matching a target compatible string.

This helper function will be useful to consider zephyr,mapped-partition
compatible nodes that are now used instead of fixed-partition compatible
nodes for memory mapped flash partitions.

Signed-off-by: Etienne Carriere <etienne.carriere@st.com>
2026-05-12 11:25:12 +02:00
Grzegorz Chwierut
c4dbadaca9 twister: power-harness: Fix build_dir path
Fixes #105743

The build_dir path is not passed as a command-line argument but
through the YAML config file exchanged between Twister and the
pytest harness. Read build_dir from dut.device_config instead
of request.config.getoption.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-05-01 12:41:57 +01:00
Grzegorz Chwierut
3557bdf8a4 twister: multidut: Add native_sim support for multi-DUT tests
Allow multi-DUT test scenarios to run on native_sim simulator.

For non-device handlers, create placeholder reserved_duts entries
using CompoundHardwareData so pytest harness can find DUT configs.
Multi-DUT on native_sim is only allowed when all required devices
use the same platform (no cross-platform multi-DUT in simulation).

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-04-29 16:29:29 -04:00
Grzegorz Chwierut
1ea17fd1e6 twister: replace required_applications dict with dataclass
Replace plain dict usage for required_application elements with
a typed RequiredApplication dataclass for improved type safety

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-04-29 16:29:29 -04:00
Grzegorz Chwierut
320a88ee1b twister: multidut: Support different app per required device
Add support for flashing a different application to a required
DUT in multi-DUT testing. When a required_device entry specifies
an 'application' field, twister matches it against the
required_applications build dirs and assigns the correct
build_dir to the reserved DUT before flashing.

Move the required applications readiness check from
process_tasks() into ProjectBuilder.process() at the 'run'
stage. This ensures that building is not blocked by the
required_applications mechanism. Mutual
dependencies between instances is also allowed.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-04-29 16:29:29 -04:00
Grzegorz Chwierut
3047951393 twister: Add support for reserving multiple DUTs
Main:
- Add HardwareReservationManager class and move DUT reservation
  logic from HardwareMap to improve separation of concerns
- Add 'required_devices' structure to testsuite YAML schema to
  define multiple device requirements for a test
- Add option to reserve multiple devices, filtered by platform
  and fixtures. Fixtures can have an extra parameter (after ':')
  that must match across the main DUT and required devices

Other:
- Add HarnessConfig dataclass to provide structured access to
  harness configuration data instead of raw dict access
- Change 'fixture' parameter in harness_config from string to
  list of strings for more flexibility when matching DUTs with
  multiple fixture requirements
- Move conversion of short platform names to full target names
  from hardware map into TestPlan module. This ensures platform
  names are resolved before test discovery
- Add unit tests for HardwareReservationManager
- Align existing tests with new dataclass structures

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-04-29 16:29:29 -04:00
Krzysztof Chruściński
ca750d6730 scripts: pylib: twister: shell: Add option to read more data
Add option to execute command and then read the resposne for a
specified time. It can be useful if we want to validate additional
data that is output as the result of a command execution.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
2026-04-17 13:43:51 +02:00
Kasper Sloth
231853800d twister: Move serial import to try except clause
Twister would incorrectly fail, if you didn't have
the serial library installed. Instead moving it
to the try except clause causes it to be handled
correctly

Signed-off-by: Kasper Sloth <kasper.sloth99@gmail.com>
2026-04-16 14:45:07 +02:00
Jeppe Odgaard
afc92ea7a1 scripts: west_commands: move zspdx to pylib
Allow twister to make use of zspdx by moving it to pylib to prevent code
duplication.

Signed-off-by: Jeppe Odgaard <jeppe.odgaard@prevas.dk>
2026-04-14 22:27:52 -04:00
Martin Moya
ba480d810b twister: add support for gtest parametrized tests
Parametrized tests in gtest are shown as the following example in a log:

[  FAILED  ] FooTest/FooTypedTest/0.TypedTestFoo, where TypeParam = <type>

current regex of the gtest harness didn't detect such test so it marks
this as pass or that the result wasn't found. This commit extends the
regex to detect such behavior and adds unit test to verify that.

Signed-off-by: Martin Moya <moyamartin1@gmail.com>
2026-04-14 22:04:44 -04:00
Lucas Romero
32f7b0f231 twister: fix harmful wait on virtual com port
that can never succeed because it is not in the com port filter list.
This would trigger every time that a combination of --flash-before and
--serial-pty is used.

The list of monitored serial ports - at least on linux - does not contain
/dev/pts but that list is constantly checked if the /dev/pts/X device might
magically appear some day.

This is the proper fix instead of adding /dev/pts/* to the monitor list
because the reason why we need to wait is because in the codepath without
serial_pty, we might be resetting the target device, causing the dev node
to disappear/reappear. This is not the case for the virtual ports created
for serial_pty.

Signed-off-by: Lucas Romero <lucas.romero@a-labs.io>
2026-04-03 23:14:12 +09:00
Łukasz Fundakowski
e3c03691a7 twister: minor improvement for sys.path
Moved all `sys.path.insert` from twister's modules
to `twisterlib.__init__.py` to have only one place where all paths
are added.

Signed-off-by: Łukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-04-02 08:45:58 -05:00
Łukasz Fundakowski
79f47b7a5f twister: write unit tests to increase code coverage
Added unit tests for `twister_main.py` module to increase code coverage.
Did minor refactoring in `twister_main.py`.
Fixed type hints and added type ignores.
Removed unused variables.

Signed-off-by: Łukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-03-27 20:19:21 -04:00
Daniel Leung
223876cba4 twister: fix unnecessary trailing slash for toolchain variant
When TOOLCHAIN_VARIANT_COMPILER is empty, we should not append
a trailing slash (/) to ZEPHYR_TOOLCHAIN_VARIANT. Or else any
matching of ZEPHYR_TOOLCHAIN_VARIANT would be incorrect.

For example, ZEPHYR_TOOLCHAIN_VARIANT == 'xt-clang' and west
build would have it correct, while twister would have it as
'xt-clang/'. This prevents kconfig from matching correctly
(possibly among other things).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-03-23 14:58:50 -05:00
Daniel Leung
f225fd06e9 twister: add support for whisper simulator
This adds support for running with whisper simulator under
twister.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
2026-03-23 08:56:34 -04:00
Joel Holdsworth
09cd8302ef scripts: twister: Add OpenRISC as a supported test platform
Adds the openrisc CPU architecture to the twister platform definitions.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
2026-03-21 07:50:57 -05:00
Jjateen Gundesha
7386141720 scripts: twister: fix monitor_serial writing handler.log in binary mode
- monitor_serial() opens handler.log in binary mode (\"wb\") and writes
re-encoded bytes. When the runner (e.g. linkserver for frdm-mcxn947)
produces serial output that is not cleanly decodable as UTF-8, the
decode('utf-8', 'ignore') silently drops bytes and the resulting
handler.log is unreadable.

- Fix by opening handler.log in text mode with explicit UTF-8 encoding,
consistent with BinaryHandler.handle_serial() which uses open(\"w\").
Change the decode error handler from 'ignore' to 'replace' so
corruption becomes visible as replacement characters rather than
silently dropped bytes.

Signed-off-by: Jjateen Gundesha <jjateen97@gmail.com>
2026-03-19 14:53:58 -05:00
Grzegorz Chwierut
2eb3bb9588 twister: move hardware reservation to runner context manager
Hardware reservation logic moved from Handler class to ProjectBuilder
runner using context manager approach. When NoDeviceAvailableException
is raised due to no free devices, the task is re-queued for later
processing instead of blocking the execution pipeline.

The reserve_hardware() context manager handles device reservation,
yields execution control only when hardware is available, and ensures
proper cleanup in the finally block.

All DUT management logic is now consolidated in HardwareMap class,
removing DUT references from Handler classes. This improves code
maintainability by centralizing responsibilities and eliminating
shared ownership of hardware resources between modules.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-03-18 15:19:30 -05:00
Sylvio Alves
e2f6e190cb twister: increase ESP32 reset pulse duration
Increase the reset pulse from 10ms to 100ms to ensure reliable
boot across all Espressif SoCs. Shorter pulses may cause the UART
to miss early bootloader output, leading to false-positive test
failures.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
2026-03-18 08:45:23 -05:00
Stephanos Ioannidis
109099c962 scripts: twisterlib: Update Zephyr SDK gcov path
Update the gcov executable path for Zephyr SDK 1.0.0.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2026-03-17 15:43:52 -04:00
Anas Nashif
3bf6c859ef twister: support new toolchain variant syntax
support new syntax involving compiler and toolchains with multiple
compilers, i.e. zephyr/gnu

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
2026-03-17 15:43:52 -04:00
Lukasz Fundakowski
8168f74916 twister: Remove DisablePyTestCollectionMixin and related tests
Simplified the code by removing useless class.

Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-03-16 09:06:24 +01:00
Grzegorz Chwierut
b5ba2c991e tests: twister: Update pytest tests for YAML config serialization
Update unit tests to verify YAML-based parameter serialization instead
of CLI arguments. Add tests for twister config file parsing and update
existing tests to check pytest_params object and YAML output.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-03-09 11:10:57 -05:00
Grzegorz Chwierut
bedba6412a twister: Add YAML config file support in pytest-harness
Add comprehensive YAML configuration support for pytest-harness
integration, replacing CLI argument approach with structured
configuration files while maintaining backward compatibility.

Key changes:
- Add --twister-config option to load test parameters from YAML
- Refactor TwisterHarnessConfig to support CLI and YAML configuration
- Move pytest parameter generation from CLI args to config files

This enables more flexible test configuration through structured YAML
files and improves the overall architecture of pytest-twister
integration.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-03-09 11:10:57 -05:00
Hake Huang
b69a35d02a tests: scripts: add west debug support in twister
in some multi-core platform, `west flash` can not support all cores.
`west debug` is supported to test secondary core, add this to twister
such as with
`--west-flash-cmd=debug` in twister command
or with
`--west-flash-cmd: debug` in hwmap.yaml

and same with pytest

command line setting will overrid the one in hwmap

Signed-off-by: Hake Huang <hake.huang@nxp.com>
Co-authored-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-03-08 22:00:45 -04:00
Grzegorz Chwierut
106ea725a5 twister: Remove redundant DUT storage from classes
Simplify the design by removing the `detected` attribute from
HardwareMap class and DUT storage from ProjectBuilder class.
Instead of storing detected DUTs as instance attributes, pass
them as method parameters where needed.

This eliminates data duplication since ProjectBuilder can access DUTs
directly from the environment object, and HardwareMap doesn't need to
maintain its own copy of detected DUTs. The refactoring makes the code
cleaner and reduces the risk of inconsistent state between different
objects storing the same DUT information.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-02-27 21:02:56 +00:00
Benjamin Cabé
2c8957fa0a doc: build: ci: twister: Zephyr now requires Python 3.12 or higher
Update getting started guide, release notes, CI actions, ruff,
build system, and Python version compliance check to use Python
3.12 as minimum supported version.

Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
2026-02-26 21:25:03 +00:00
Grzegorz Chwierut
69edab8c97 twister: move ZEPHYR_BASE to constants to reduce dependencies
Moving ZEPHYR_BASE from environment.py to constants.py
allows importing classes like HardwareMap in
pytest-twister-harness module without pulling in unnecessary
dependencies from environment.py.

This refactoring improves module independence and makes selective
imports cleaner across the twister Python package structure.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-02-17 17:41:16 -06:00
Lukasz Fundakowski
d73ea02be1 twister: Do not clean previous results for listing tests
When user only wants to list tests or tags twister should
not clean previous results.
This change improves also performance for listing tests.

Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-02-17 13:22:35 -06:00
Lukasz Fundakowski
98c6fbfc9b twister: Refactor twister_main.py
Refactored main function for Twister execution.

- Rewrote the main function as a class and divided it into smaller,
  more maintainable methods.
- Improved code structure, making it easier
  to test with unit and module tests.
- Reduced the amount of code in a single function,
  enhancing readability and understanding

Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-02-17 10:32:02 +01:00
Guðni Már Gilbert
fc8e096ee1 scripts: twister: minor speedup in apply_filters
Move a check to run earlier in a for-loop to minimize doing unnecessary
work like creating TestInstance class instances.

This is a very minor optimization when the number of testsuites are low.
But starts to count when there are a lot of testsuites.

Signed-off-by: Guðni Már Gilbert <gudni.m.g@gmail.com>
2026-02-02 14:46:34 +01:00
Hake Huang
cdd9d63a46 twister: handler: add spsdk support with dev_id
spsdk now support dev_id as parameter

Signed-off-by: Hake Huang <hake.huang@nxp.com>
2026-01-31 10:56:06 +01:00
Guðni Már Gilbert
bcb00aaa9a scripts: twister: don't parse snippets multiple times
Currently apply_filters is parsing & validating a snippet YAML
file multiple times, duplicating expensive work.

Do the work once per testsuite instead of for each testsuite
multiplied by number of toolchains.

Signed-off-by: Guðni Már Gilbert <gudni.m.g@gmail.com>
2026-01-30 16:56:25 -06:00
Lukasz Fundakowski
2ad2ce9fe1 twister: Improve error handling for user's errors
Exit from twister in case of user's error e.g. empty list
of tests provided by user instead of throwing unhandled
exception.

Signed-off-by: Lukasz Fundakowski <lukasz.fundakowski@nordicsemi.no>
2026-01-30 16:55:29 -06:00
Piotr Kosycarz
aedada06c6 scripts: pylib: twister: twisterlib: coverage: catch hex parsing
As parsing hex was moved, move also catching its possible errors.

Signed-off-by: Piotr Kosycarz <piotr.kosycarz@nordicsemi.no>
2026-01-30 05:59:28 -06:00
Grzegorz Chwierut
1b1e902540 Revert "twister: pytest: fix duplicate log lines from pytest"
This reverts commit 96985a32e8.

Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
2026-01-29 14:26:41 +00:00