Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/doc/en/how-to/usage.rst

raw
1
2 .. _usage:
3
4 How to invoke pytest
5 ==========================================
6
7 .. seealso:: :ref:`Complete pytest command-line flag reference <command-line-flags>`
8
9 In general, pytest is invoked with the command ``pytest`` (see below for :ref:`other ways to invoke pytest
10 <invoke-other>`). This will execute all tests in all files whose names follow the form ``test_*.py`` or ``\*_test.py``
11 in the current directory and its subdirectories. More generally, pytest follows :ref:`standard test discovery rules
12 <test discovery>`.
13
14
15 .. _select-tests:
16
17 Specifying which tests to run
18 ------------------------------
19
20 Pytest supports several ways to run and select tests from the command-line or from a file
21 (see below for :ref:`reading arguments from file <args-from-file>`).
22
23 **Run tests in a module**
24
25 .. code-block:: bash
26
27 pytest test_mod.py
28
29 **Run tests in a directory**
30
31 .. code-block:: bash
32
33 pytest testing/
34
35 **Run tests by keyword expressions**
36
37 .. code-block:: bash
38
39 pytest -k 'MyClass and not method'
40
41 This will run tests which contain names that match the given *string expression* (case-insensitive),
42 which can include Python operators that use filenames, class names and function names as variables.
43 The example above will run ``TestMyClass.test_something`` but not ``TestMyClass.test_method_simple``.
44 Use ``""`` instead of ``''`` in expression when running this on Windows
45
46 .. _nodeids:
47
48 **Run tests by collection arguments**
49
50 Pass the module filename relative to the working directory, followed by specifiers like the class name and function name
51 separated by ``::`` characters, and parameters from parameterization enclosed in ``[]``.
52
53 To run a specific test within a module:
54
55 .. code-block:: bash
56
57 pytest tests/test_mod.py::test_func
58
59 To run all tests in a class:
60
61 .. code-block:: bash
62
63 pytest tests/test_mod.py::TestClass
64
65 Specifying a specific test method:
66
67 .. code-block:: bash
68
69 pytest tests/test_mod.py::TestClass::test_method
70
71 Specifying a specific parametrization of a test:
72
73 .. code-block:: bash
74
75 pytest tests/test_mod.py::test_func[x1,y2]
76
77 **Run tests by marker expressions**
78
79 .. code-block:: bash
80
81 pytest -m slow
82
83 Will run all tests which are decorated with the ``@pytest.mark.slow`` decorator.
84
85 For more information see :ref:`marks <mark>`.
86
87 **Run tests from packages**
88
89 .. code-block:: bash
90
91 pytest --pyargs pkg.testing
92
93 This will import ``pkg.testing`` and use its filesystem location to find and run tests from.
94
95 .. _args-from-file:
96
97 **Read arguments from file**
98
99 .. versionadded:: 8.2
100
101 All of the above can be read from a file using the ``@`` prefix:
102
103 .. code-block:: bash
104
105 pytest @tests_to_run.txt
106
107 where ``tests_to_run.txt`` contains an entry per line, e.g.:
108
109 .. code-block:: text
110
111 tests/test_file.py
112 tests/test_mod.py::test_func[x1,y2]
113 tests/test_mod.py::TestClass
114 -m slow
115
116 This file can also be generated using ``pytest --collect-only -q`` and modified as needed.
117
118 Getting help on version, option names, environment variables
119 --------------------------------------------------------------
120
121 .. code-block:: bash
122
123 pytest --version # shows where pytest was imported from
124 pytest --fixtures # show available builtin function arguments
125 pytest -h | --help # show help on command line and config file options
126
127
128 .. _durations:
129
130 Profiling test execution duration
131 -------------------------------------
132
133 .. versionchanged:: 6.0
134
135 To get a list of the slowest 10 test durations over 1.0s long:
136
137 .. code-block:: bash
138
139 pytest --durations=10 --durations-min=1.0
140
141 By default, pytest will not show test durations that are too small (<0.005s) unless ``-vv`` is passed on the command-line.
142
143
144 Managing loading of plugins
145 -------------------------------
146
147 Early loading plugins
148 ~~~~~~~~~~~~~~~~~~~~~~~
149
150 You can early-load plugins (internal and external) explicitly in the command-line with the ``-p`` option::
151
152 pytest -p mypluginmodule
153
154 The option receives a ``name`` parameter, which can be:
155
156 * A full module dotted name, for example ``myproject.plugins``. This dotted name must be importable.
157 * The entry-point name of a plugin. This is the name passed to ``setuptools`` when the plugin is
158 registered. For example to early-load the :pypi:`pytest-cov` plugin you can use::
159
160 pytest -p pytest_cov
161
162
163 Disabling plugins
164 ~~~~~~~~~~~~~~~~~~
165
166 To disable loading specific plugins at invocation time, use the ``-p`` option
167 together with the prefix ``no:``.
168
169 Example: to disable loading the plugin ``doctest``, which is responsible for
170 executing doctest tests from text files, invoke pytest like this:
171
172 .. code-block:: bash
173
174 pytest -p no:doctest
175
176
177 .. _invoke-other:
178
179 Other ways of calling pytest
180 -----------------------------------------------------
181
182 .. _invoke-python:
183
184 Calling pytest through ``python -m pytest``
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186
187 You can invoke testing through the Python interpreter from the command line:
188
189 .. code-block:: text
190
191 python -m pytest [...]
192
193 This is almost equivalent to invoking the command line script ``pytest [...]``
194 directly, except that calling via ``python`` will also add the current directory to ``sys.path``.
195
196
197 .. _`pytest.main-usage`:
198
199 Calling pytest from Python code
200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
202 You can invoke ``pytest`` from Python code directly:
203
204 .. code-block:: python
205
206 retcode = pytest.main()
207
208 this acts as if you would call "pytest" from the command line.
209 It will not raise :class:`SystemExit` but return the :ref:`exit code <exit-codes>` instead.
210 If you don't pass it any arguments, ``main`` reads the arguments from the command line arguments of the process (:data:`sys.argv`), which may be undesirable.
211 You can pass in options and arguments explicitly:
212
213 .. code-block:: python
214
215 retcode = pytest.main(["-x", "mytestdir"])
216
217 You can specify additional plugins to ``pytest.main``:
218
219 .. code-block:: python
220
221 # content of myinvoke.py
222 import sys
223
224 import pytest
225
226
227 class MyPlugin:
228 def pytest_sessionfinish(self):
229 print("*** test run reporting finishing")
230
231
232 if __name__ == "__main__":
233 sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()]))
234
235 Running it will show that ``MyPlugin`` was added and its
236 hook was invoked:
237
238 .. code-block:: pytest
239
240 $ python myinvoke.py
241 *** test run reporting finishing
242
243
244 .. note::
245
246 Calling ``pytest.main()`` will result in importing your tests and any modules
247 that they import. Due to the caching mechanism of python's import system,
248 making subsequent calls to ``pytest.main()`` from the same process will not
249 reflect changes to those files between the calls. For this reason, making
250 multiple calls to ``pytest.main()`` from the same process (in order to re-run
251 tests, for example) is not recommended.