| 1 |
|
| 2 |
.. _usage: |
| 3 |
|
| 4 |
How to invoke pytest |
| 5 |
========================================== |
| 6 |
|
| 7 |
.. seealso:: :ref:`Complete pytest command-line flags 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 |
To run all tests which are decorated with the ``@pytest.mark.slow`` decorator: |
| 80 |
|
| 81 |
.. code-block:: bash |
| 82 |
|
| 83 |
pytest -m slow |
| 84 |
|
| 85 |
|
| 86 |
To run all tests which are decorated with the annotated ``@pytest.mark.slow(phase=1)`` decorator, |
| 87 |
with the ``phase`` keyword argument set to ``1``: |
| 88 |
|
| 89 |
.. code-block:: bash |
| 90 |
|
| 91 |
pytest -m "slow(phase=1)" |
| 92 |
|
| 93 |
For more information see :ref:`marks <mark>`. |
| 94 |
|
| 95 |
**Run tests from packages** |
| 96 |
|
| 97 |
.. code-block:: bash |
| 98 |
|
| 99 |
pytest --pyargs pkg.testing |
| 100 |
|
| 101 |
This will import ``pkg.testing`` and use its filesystem location to find and run tests from. |
| 102 |
|
| 103 |
.. _args-from-file: |
| 104 |
|
| 105 |
**Read arguments from file** |
| 106 |
|
| 107 |
.. versionadded:: 8.2 |
| 108 |
|
| 109 |
All of the above can be read from a file using the ``@`` prefix: |
| 110 |
|
| 111 |
.. code-block:: bash |
| 112 |
|
| 113 |
pytest @tests_to_run.txt |
| 114 |
|
| 115 |
where ``tests_to_run.txt`` contains an entry per line, e.g.: |
| 116 |
|
| 117 |
.. code-block:: text |
| 118 |
|
| 119 |
tests/test_file.py |
| 120 |
tests/test_mod.py::test_func[x1,y2] |
| 121 |
tests/test_mod.py::TestClass |
| 122 |
-m slow |
| 123 |
|
| 124 |
This file can also be generated using ``pytest --collect-only -q`` and modified as needed. |
| 125 |
|
| 126 |
Getting help on version, option names, environment variables |
| 127 |
-------------------------------------------------------------- |
| 128 |
|
| 129 |
.. code-block:: bash |
| 130 |
|
| 131 |
pytest --version # shows where pytest was imported from |
| 132 |
pytest --fixtures # show available builtin function arguments |
| 133 |
pytest -h | --help # show help on command line and config file options |
| 134 |
|
| 135 |
|
| 136 |
.. _durations: |
| 137 |
|
| 138 |
Profiling test execution duration |
| 139 |
------------------------------------- |
| 140 |
|
| 141 |
.. versionchanged:: 6.0 |
| 142 |
|
| 143 |
To get a list of the slowest 10 test durations over 1.0s long: |
| 144 |
|
| 145 |
.. code-block:: bash |
| 146 |
|
| 147 |
pytest --durations=10 --durations-min=1.0 |
| 148 |
|
| 149 |
By default, pytest will not show test durations that are too small (<0.005s) unless ``-vv`` is passed on the command-line. |
| 150 |
|
| 151 |
|
| 152 |
Managing loading of plugins |
| 153 |
------------------------------- |
| 154 |
|
| 155 |
Early loading plugins |
| 156 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 157 |
|
| 158 |
You can early-load plugins (internal and external) explicitly in the command-line with the :option:`-p` option:: |
| 159 |
|
| 160 |
pytest -p mypluginmodule |
| 161 |
|
| 162 |
The option receives a ``name`` parameter, which can be: |
| 163 |
|
| 164 |
* A full module dotted name, for example ``myproject.plugins``. This dotted name must be importable. |
| 165 |
* The entry-point name of a plugin. This is the name passed to ``importlib`` when the plugin is |
| 166 |
registered. For example to early-load the :pypi:`pytest-cov` plugin you can use:: |
| 167 |
|
| 168 |
pytest -p pytest_cov |
| 169 |
|
| 170 |
|
| 171 |
Disabling plugins |
| 172 |
~~~~~~~~~~~~~~~~~~ |
| 173 |
|
| 174 |
To disable loading specific plugins at invocation time, use the :option:`-p` option |
| 175 |
together with the prefix ``no:``. |
| 176 |
|
| 177 |
Example: to disable loading the plugin ``doctest``, which is responsible for |
| 178 |
executing doctest tests from text files, invoke pytest like this: |
| 179 |
|
| 180 |
.. code-block:: bash |
| 181 |
|
| 182 |
pytest -p no:doctest |
| 183 |
|
| 184 |
|
| 185 |
.. _invoke-other: |
| 186 |
|
| 187 |
Other ways of calling pytest |
| 188 |
----------------------------------------------------- |
| 189 |
|
| 190 |
.. _invoke-python: |
| 191 |
|
| 192 |
Calling pytest through ``python -m pytest`` |
| 193 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 194 |
|
| 195 |
You can invoke testing through the Python interpreter from the command line: |
| 196 |
|
| 197 |
.. code-block:: text |
| 198 |
|
| 199 |
python -m pytest [...] |
| 200 |
|
| 201 |
This is almost equivalent to invoking the command line script ``pytest [...]`` |
| 202 |
directly, except that calling via ``python`` will also add the current directory to ``sys.path``. |
| 203 |
|
| 204 |
|
| 205 |
.. _`pytest.main-usage`: |
| 206 |
|
| 207 |
Calling pytest from Python code |
| 208 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 209 |
|
| 210 |
You can invoke ``pytest`` from Python code directly: |
| 211 |
|
| 212 |
.. code-block:: python |
| 213 |
|
| 214 |
retcode = pytest.main() |
| 215 |
|
| 216 |
this acts as if you would call "pytest" from the command line. |
| 217 |
It will not raise :class:`SystemExit` but return the :ref:`exit code <exit-codes>` instead. |
| 218 |
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. |
| 219 |
You can pass in options and arguments explicitly: |
| 220 |
|
| 221 |
.. code-block:: python |
| 222 |
|
| 223 |
retcode = pytest.main(["-x", "mytestdir"]) |
| 224 |
|
| 225 |
You can specify additional plugins to ``pytest.main``: |
| 226 |
|
| 227 |
.. code-block:: python |
| 228 |
|
| 229 |
# content of myinvoke.py |
| 230 |
import sys |
| 231 |
|
| 232 |
import pytest |
| 233 |
|
| 234 |
|
| 235 |
class MyPlugin: |
| 236 |
def pytest_sessionfinish(self): |
| 237 |
print("*** test run reporting finishing") |
| 238 |
|
| 239 |
|
| 240 |
if __name__ == "__main__": |
| 241 |
sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()])) |
| 242 |
|
| 243 |
Running it will show that ``MyPlugin`` was added and its |
| 244 |
hook was invoked: |
| 245 |
|
| 246 |
.. code-block:: pytest |
| 247 |
|
| 248 |
$ python myinvoke.py |
| 249 |
*** test run reporting finishing |
| 250 |
|
| 251 |
|
| 252 |
.. note:: |
| 253 |
|
| 254 |
Calling ``pytest.main()`` will result in importing your tests and any modules |
| 255 |
that they import. Due to the caching mechanism of python's import system, |
| 256 |
making subsequent calls to ``pytest.main()`` from the same process will not |
| 257 |
reflect changes to those files between the calls. For this reason, making |
| 258 |
multiple calls to ``pytest.main()`` from the same process (in order to re-run |
| 259 |
tests, for example) is not recommended. |