blob: c6382f9a9e3379a1f8314a371c3d1837acbc66ce [file] [log] [blame]
Andrew Boie08ce5a52016-02-22 13:28:10 -08001#!/usr/bin/env python3
Anas Nashifa792a3d2017-04-04 18:47:49 -04002# vim: set syntax=python ts=4 :
Anas Nashifce2b4182020-03-24 14:40:28 -04003# Copyright (c) 2020 Intel Corporation
Anas Nashif3ae52622019-04-06 09:08:09 -04004# SPDX-License-Identifier: Apache-2.0
Anas Nashifb18f3112020-12-07 11:40:19 -05005"""Zephyr Test Runner (twister)
Andrew Boie6acbe632015-07-17 12:03:52 -07006
Marc Herberte5cedca2019-04-08 14:02:34 -07007Also check the "User and Developer Guides" at https://docs.zephyrproject.org/
8
Andrew Boie6acbe632015-07-17 12:03:52 -07009This script scans for the set of unit test applications in the git
10repository and attempts to execute them. By default, it tries to
11build each test case on one platform per architecture, using a precedence
Paul Sokolovskyff70add2017-06-16 01:31:54 +030012list defined in an architecture configuration file, and if possible
Anas Nashif83fc06a2019-06-22 11:04:10 -040013run the tests in any available emulators or simulators on the system.
Andrew Boie6acbe632015-07-17 12:03:52 -070014
Anas Nashifa792a3d2017-04-04 18:47:49 -040015Test cases are detected by the presence of a 'testcase.yaml' or a sample.yaml
16files in the application's project directory. This file may contain one or more
17blocks, each identifying a test scenario. The title of the block is a name for
18the test case, which only needs to be unique for the test cases specified in
Anas Nashif15bc98e2022-03-23 14:07:54 -040019that testsuite meta-data. The full canonical name for each test case is <path to
Anas Nashifa792a3d2017-04-04 18:47:49 -040020test case>/<block>.
Andrew Boie6acbe632015-07-17 12:03:52 -070021
Anas Nashif15bc98e2022-03-23 14:07:54 -040022Each test block in the testsuite meta data can define the following key/value
Anas Nashif3ba1d432017-12-05 15:28:44 -050023pairs:
Andrew Boie6acbe632015-07-17 12:03:52 -070024
Anas Nashiffa695d22017-10-04 16:14:27 -040025 tags: <list of tags> (required)
Anas Nashif15bc98e2022-03-23 14:07:54 -040026 A set of string tags for the testsuite. Usually pertains to
Andrew Boie6acbe632015-07-17 12:03:52 -070027 functional domains but can be anything. Command line invocations
28 of this script can filter the set of tests to run based on tag.
29
Anas Nashifa792a3d2017-04-04 18:47:49 -040030 skip: <True|False> (default False)
Anas Nashif15bc98e2022-03-23 14:07:54 -040031 skip testsuite unconditionally. This can be used for broken tests.
Anas Nashif2bd99bc2015-10-12 13:10:57 -040032
Anas Nashifa792a3d2017-04-04 18:47:49 -040033 slow: <True|False> (default False)
Andy Rossdc4151f2019-01-03 14:17:43 -080034 Don't build or run this test case unless --enable-slow was passed
35 in on the command line. Intended for time-consuming test cases
36 that are only run under certain circumstances, like daily
37 builds.
Andrew Boie6bb087c2016-02-10 13:39:00 -080038
Anas Nashifa792a3d2017-04-04 18:47:49 -040039 extra_args: <list of extra arguments>
Sebastian Bøec2182612017-11-09 12:25:02 +010040 Extra cache entries to pass to CMake when building or running the
Andrew Boie6acbe632015-07-17 12:03:52 -070041 test case.
42
Anas Nashifebc329d2017-10-17 09:00:33 -040043 extra_configs: <list of extra configurations>
44 Extra configuration options to be merged with a master prj.conf
45 when building or running the test case.
46
Jamie McCraebc97d8f2023-08-03 10:56:41 +010047 required_snippets: <list of snippets>
48 Snippets that must be applied for the test case to run.
49
Daniel DeGrasse602b8b42022-08-24 18:37:06 -050050 sysbuild: <True|False> (default False)
51 If true, build the sample using the sysbuild infrastructure. Filtering
52 will only be enabled for the main project, and is not supported for
53 other projects included by sysbuild.
54
Anas Nashifa792a3d2017-04-04 18:47:49 -040055 build_only: <True|False> (default False)
Marc Herberte5cedca2019-04-08 14:02:34 -070056 If true, don't try to run the test even if the selected platform
57 supports it.
Andrew Boie6acbe632015-07-17 12:03:52 -070058
Anas Nashifa792a3d2017-04-04 18:47:49 -040059 build_on_all: <True|False> (default False)
60 If true, attempt to build test on all available platforms.
61
62 depends_on: <list of features>
63 A board or platform can announce what features it supports, this option
64 will enable the test only those platforms that provide this feature.
65
66 min_ram: <integer>
67 minimum amount of RAM needed for this test to build and run. This is
68 compared with information provided by the board metadata.
69
70 min_flash: <integer>
71 minimum amount of ROM needed for this test to build and run. This is
72 compared with information provided by the board metadata.
73
Anas Nashiff429f332022-04-18 22:34:39 -040074 modules: <list of modules>
75 Add list of modules needed for this sample to build and run.
76
Anas Nashifa792a3d2017-04-04 18:47:49 -040077 timeout: <number of seconds>
Anas Nashif83fc06a2019-06-22 11:04:10 -040078 Length of time to run test in emulator before automatically killing it.
Andrew Boie6acbe632015-07-17 12:03:52 -070079 Default to 60 seconds.
80
Anas Nashifdca317c2020-08-26 11:28:25 -040081 arch_allow: <list of arches, such as x86, arm, arc>
Andrew Boie6acbe632015-07-17 12:03:52 -070082 Set of architectures that this test case should only be run for.
83
Anas Nashifa792a3d2017-04-04 18:47:49 -040084 arch_exclude: <list of arches, such as x86, arm, arc>
Anas Nashif30d13872015-10-05 10:02:45 -040085 Set of architectures that this test case should not run on.
86
Anas Nashifdca317c2020-08-26 11:28:25 -040087 platform_allow: <list of platforms>
Anas Nashif30d13872015-10-05 10:02:45 -040088 Set of platforms that this test case should only be run for.
89
Anas Nashifa792a3d2017-04-04 18:47:49 -040090 platform_exclude: <list of platforms>
Anas Nashif30d13872015-10-05 10:02:45 -040091 Set of platforms that this test case should not run on.
Andrew Boie6acbe632015-07-17 12:03:52 -070092
Yong Cong Sin9d314ff2023-12-12 15:40:49 +080093 simulation_exclude: <list of simulators>
94 Set of simulators that this test case should not run on.
95
Anas Nashifa792a3d2017-04-04 18:47:49 -040096 extra_sections: <list of extra binary sections>
Anas Nashifb18f3112020-12-07 11:40:19 -050097 When computing sizes, twister will report errors if it finds
Andrew Boie52fef672016-11-29 12:21:59 -080098 extra, unexpected sections in the Zephyr binary unless they are named
99 here. They will not be included in the size calculation.
100
Anas Nashifa792a3d2017-04-04 18:47:49 -0400101 filter: <expression>
Anas Nashif15bc98e2022-03-23 14:07:54 -0400102 Filter whether the testsuite should be run by evaluating an expression
Andrew Boie3ea78922016-03-24 14:46:00 -0700103 against an environment containing the following values:
104
105 { ARCH : <architecture>,
106 PLATFORM : <platform>,
Javier B Perez79414542016-08-08 12:24:59 -0500107 <all CONFIG_* key/value pairs in the test's generated defconfig>,
Anas Nashif45a97862019-01-09 08:46:42 -0500108 <all DT_* key/value pairs in the test's generated device tree file>,
109 <all CMake key/value pairs in the test's generated CMakeCache.txt file>,
Javier B Perez79414542016-08-08 12:24:59 -0500110 *<env>: any environment variable available
Andrew Boie3ea78922016-03-24 14:46:00 -0700111 }
112
113 The grammar for the expression language is as follows:
114
115 expression ::= expression "and" expression
116 | expression "or" expression
117 | "not" expression
118 | "(" expression ")"
119 | symbol "==" constant
120 | symbol "!=" constant
121 | symbol "<" number
122 | symbol ">" number
123 | symbol ">=" number
124 | symbol "<=" number
125 | symbol "in" list
Andrew Boie7a87f9f2016-06-02 12:27:54 -0700126 | symbol ":" string
Andrew Boie3ea78922016-03-24 14:46:00 -0700127 | symbol
128
129 list ::= "[" list_contents "]"
130
131 list_contents ::= constant
132 | list_contents "," constant
133
134 constant ::= number
135 | string
136
137
138 For the case where expression ::= symbol, it evaluates to true
139 if the symbol is defined to a non-empty string.
140
141 Operator precedence, starting from lowest to highest:
142
143 or (left associative)
144 and (left associative)
145 not (right associative)
146 all comparison operators (non-associative)
147
Andrew Boie7a87f9f2016-06-02 12:27:54 -0700148 The ':' operator compiles the string argument as a regular expression,
149 and then returns a true value only if the symbol's value in the environment
Anas Nashif578ae402019-07-12 07:54:35 -0700150 matches. For example, if CONFIG_SOC="stm32f107xc" then
Andrew Boie7a87f9f2016-06-02 12:27:54 -0700151
Anas Nashif578ae402019-07-12 07:54:35 -0700152 filter = CONFIG_SOC : "stm.*"
Andrew Boie7a87f9f2016-06-02 12:27:54 -0700153
154 Would match it.
155
Alberto Escolar Piedrasec273d12023-10-17 14:00:08 +0200156 Note that arch_allow, arch_exclude, platform_allow, platform_exclude
157 are not just syntactic sugar for filter expressions. For instance
158
159 arch_exclude = x86 arc
160
161 Can appear at first glance to have a similar effect to
162
163 filter = not ARCH in ["x86", "arc"]
164
165 but unlike "filter", these cause platforms to be filtered already during the testplan
166 generation. While "filter" does not exclue platforms at the testplan generation, and instead
167 relies on the result of running the build configuration stage. That is, to evaluate the filter
168 expression, cmake is run for that target, and then the filter evaluated as a gate for the
169 build and run steps.
170 Therefore filtering by using {platform|arch}_{exclude|allow} is much faster.
171
Anas Nashif15bc98e2022-03-23 14:07:54 -0400172The set of test cases that actually run depends on directives in the testsuite
Anas Nashif3dcc0382022-03-19 08:08:44 -0400173files and options passed in on the command line. If there is any confusion,
174running with -v or examining the test plan report (testplan.json)
Anas Nashif12d8cce2019-11-20 03:47:27 -0800175can help show why particular test cases were skipped.
Andrew Boie6acbe632015-07-17 12:03:52 -0700176
Genaro Saucedo Tejada28bba922016-10-24 18:00:58 -0500177To load arguments from a file, write '+' before the file name, e.g.,
178+file_name. File content must be one or more valid arguments separated by
179line break instead of white spaces.
180
Andrew Boie6acbe632015-07-17 12:03:52 -0700181Most everyday users will run with no arguments.
Andy Rossdc4151f2019-01-03 14:17:43 -0800182
Andrew Boie6acbe632015-07-17 12:03:52 -0700183"""
184
Sebastian Bøe56d74712019-01-21 15:48:46 +0100185import os
Andrew Boie6acbe632015-07-17 12:03:52 -0700186import sys
Martí Bolívar4fad5352020-04-13 16:05:44 -0700187from pathlib import Path
Anas Nashiffacc6852022-06-10 07:27:28 -0400188
Anas Nashifbb280352020-05-07 12:02:48 -0400189
Anas Nashifce2b4182020-03-24 14:40:28 -0400190ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
191if not ZEPHYR_BASE:
Anas Nashifb18f3112020-12-07 11:40:19 -0500192 # This file has been zephyr/scripts/twister for years,
Martí Bolívar4fad5352020-04-13 16:05:44 -0700193 # and that is not going to change anytime soon. Let the user
Anas Nashifb18f3112020-12-07 11:40:19 -0500194 # run this script as ./scripts/twister without making them
Martí Bolívar4fad5352020-04-13 16:05:44 -0700195 # set ZEPHYR_BASE.
196 ZEPHYR_BASE = str(Path(__file__).resolve().parents[1])
197
198 # Propagate this decision to child processes.
199 os.environ['ZEPHYR_BASE'] = ZEPHYR_BASE
200
201 print(f'ZEPHYR_BASE unset, using "{ZEPHYR_BASE}"')
Anas Nashifd9882382019-12-12 09:58:28 -0500202
Anas Nashif087f1e42022-06-23 17:40:57 -0400203sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/"))
Daniel DeGrasseb579a442022-10-05 16:06:29 -0500204sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/build_helpers"))
Anas Nashif434995c2019-12-01 13:55:11 -0500205
Al Semjonovsd09fd222022-11-17 14:05:12 -0700206from twisterlib.environment import add_parse_arguments, parse_arguments
207from twisterlib.twister_main import main
Anas Nashifd9882382019-12-12 09:58:28 -0500208
Andrew Boie6acbe632015-07-17 12:03:52 -0700209if __name__ == "__main__":
Anas Nashif76d55432022-06-13 15:50:32 -0400210 ret = 0
Andrew Boie1fe1f3a2020-01-02 19:27:40 -0800211 try:
Al Semjonovs067ba652022-09-26 15:38:00 -0600212 parser = add_parse_arguments()
213 options = parse_arguments(parser, sys.argv[1:])
Dmitrii Golovanovd45dbc22024-05-07 09:11:47 +0200214 default_options = parse_arguments(parser, [], on_init=False)
215 ret = main(options, default_options)
Andrew Boie1fe1f3a2020-01-02 19:27:40 -0800216 finally:
Piotr Golyzniak8b773482021-11-02 16:13:26 +0100217 if (os.name != "nt") and os.isatty(1):
218 # (OS is not Windows) and (stdout is interactive)
Jeremy Bettis562b2912022-08-24 12:44:05 -0600219 os.system("stty sane <&1")
Anas Nashif76d55432022-06-13 15:50:32 -0400220
221 sys.exit(ret)