2010-10-14 01:30:00 +08:00
2010-11-06 06:37:31 +08:00
.. _usage:
2021-03-11 03:18:21 +08:00
How to invoke pytest
2010-11-06 06:37:31 +08:00
==========================================
2010-10-14 07:25:09 +08:00
2021-03-22 07:04:12 +08:00
.. seealso :: :ref: `Complete pytest command-line flag reference <command-line-flags>`
2010-11-06 06:37:31 +08:00
2021-04-09 08:19:30 +08:00
In general, pytest is invoked with the command `` pytest `` (see below for :ref:`other ways to invoke pytest
<invoke-other> `). This will execute all tests in all files whose names follow the form ` `test_*.py` ` or ` `\*_test.py` `
in the current directory and its subdirectories. More generally, pytest follows :ref:`standard test discovery rules
<test discovery> `.
2019-06-16 03:41:55 +08:00
2010-10-14 01:30:00 +08:00
2017-08-04 09:00:11 +08:00
.. _select-tests:
2021-03-22 07:04:12 +08:00
Specifying which tests to run
------------------------------
2010-11-06 06:37:31 +08:00
2024-03-09 14:51:52 +08:00
Pytest supports several ways to run and select tests from the command-line or from a file
(see below for :ref: `reading arguments from file <args-from-file>` ).
2017-07-19 04:04:37 +08:00
**Run tests in a module**
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
pytest test_mod.py
**Run tests in a directory**
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
pytest testing/
**Run tests by keyword expressions**
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
2023-05-11 09:28:52 +08:00
pytest -k 'MyClass and not method'
2017-07-19 04:04:37 +08:00
2019-12-05 21:13:22 +08:00
This will run tests which contain names that match the given *string expression* (case-insensitive),
which can include Python operators that use filenames, class names and function names as variables.
2017-07-19 04:04:37 +08:00
The example above will run `` TestMyClass.test_something `` but not `` TestMyClass.test_method_simple `` .
2023-05-11 09:28:52 +08:00
Use `` "" `` instead of `` '' `` in expression when running this on Windows
2017-07-19 04:04:37 +08:00
.. _nodeids:
2023-07-28 15:06:38 +08:00
**Run tests by collection arguments**
2017-07-19 04:04:37 +08:00
2023-07-28 15:06:38 +08:00
Pass the module filename relative to the working directory, followed by specifiers like the class name and function name
separated by `` :: `` characters, and parameters from parameterization enclosed in `` [] `` .
2017-07-19 04:04:37 +08:00
2019-02-15 21:10:37 +08:00
To run a specific test within a module:
.. code-block :: bash
2017-07-19 04:04:37 +08:00
2023-07-23 18:36:42 +08:00
pytest tests/test_mod.py::test_func
2017-07-19 04:04:37 +08:00
2023-07-23 18:36:42 +08:00
To run all tests in a class:
2017-07-19 04:04:37 +08:00
2023-07-23 18:36:42 +08:00
.. code-block :: bash
pytest tests/test_mod.py::TestClass
Specifying a specific test method:
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
2023-07-23 18:36:42 +08:00
pytest tests/test_mod.py::TestClass::test_method
2017-07-19 04:04:37 +08:00
2023-07-28 15:06:38 +08:00
Specifying a specific parametrization of a test:
.. code-block :: bash
pytest tests/test_mod.py::test_func[x1,y2]
2017-07-19 04:04:37 +08:00
**Run tests by marker expressions**
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
pytest -m slow
Will run all tests which are decorated with the `` @pytest.mark.slow `` decorator.
For more information see :ref: `marks <mark>` .
**Run tests from packages**
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2017-07-19 04:04:37 +08:00
pytest --pyargs pkg.testing
2018-05-18 16:19:46 +08:00
2017-07-19 04:04:37 +08:00
This will import `` pkg.testing `` and use its filesystem location to find and run tests from.
2018-05-18 16:19:46 +08:00
2024-03-09 14:51:52 +08:00
.. _args-from-file:
**Read arguments from file**
.. versionadded :: 8.2
All of the above can be read from a file using the `` @ `` prefix:
.. code-block :: bash
pytest @tests_to_run.txt
where `` tests_to_run.txt `` contains an entry per line, e.g.:
.. code-block :: text
tests/test_file.py
tests/test_mod.py::test_func[x1,y2]
tests/test_mod.py::TestClass
-m slow
This file can also be generated using `` pytest --collect-only -q `` and modified as needed.
2010-11-06 06:37:31 +08:00
2021-03-22 07:04:12 +08:00
Getting help on version, option names, environment variables
--------------------------------------------------------------
.. code-block :: bash
pytest --version # shows where pytest was imported from
pytest --fixtures # show available builtin function arguments
pytest -h | --help # show help on command line and config file options
2011-11-09 01:20:56 +08:00
.. _durations:
2011-12-05 18:10:48 +08:00
Profiling test execution duration
2011-11-09 01:20:56 +08:00
-------------------------------------
2020-09-05 20:18:29 +08:00
.. versionchanged :: 6.0
2011-11-09 01:20:56 +08:00
2020-09-05 20:18:29 +08:00
To get a list of the slowest 10 test durations over 1.0s long:
2019-02-15 21:10:37 +08:00
.. code-block :: bash
2011-11-09 01:20:56 +08:00
2020-09-05 20:18:29 +08:00
pytest --durations=10 --durations-min=1.0
2011-11-09 01:20:56 +08:00
2020-09-05 20:18:29 +08:00
By default, pytest will not show test durations that are too small (<0.005s) unless `` -vv `` is passed on the command-line.
2011-11-09 01:20:56 +08:00
2019-06-13 05:49:51 +08:00
2021-03-22 07:04:12 +08:00
Managing loading of plugins
-------------------------------
2019-10-16 06:45:58 +08:00
2019-02-08 06:59:10 +08:00
Early loading plugins
2021-03-22 07:04:12 +08:00
~~~~~~~~~~~~~~~~~~~~~~~
2019-02-08 06:59:10 +08:00
You can early-load plugins (internal and external) explicitly in the command-line with the `` -p `` option::
pytest -p mypluginmodule
The option receives a `` name `` parameter, which can be:
* A full module dotted name, for example `` myproject.plugins `` . This dotted name must be importable.
* The entry-point name of a plugin. This is the name passed to `` setuptools `` when the plugin is
2021-10-22 20:47:57 +08:00
registered. For example to early-load the :pypi: `pytest-cov` plugin you can use::
2019-02-08 06:59:10 +08:00
pytest -p pytest_cov
2014-01-20 05:05:14 +08:00
Disabling plugins
2021-03-22 07:04:12 +08:00
~~~~~~~~~~~~~~~~~~
2014-01-20 05:05:14 +08:00
To disable loading specific plugins at invocation time, use the `` -p `` option
together with the prefix `` no: `` .
Example: to disable loading the plugin `` doctest `` , which is responsible for
2019-02-15 21:10:37 +08:00
executing doctest tests from text files, invoke pytest like this:
.. code-block :: bash
2014-01-20 05:05:14 +08:00
2016-06-21 22:16:57 +08:00
pytest -p no:doctest
2014-01-20 05:05:14 +08:00
2012-10-09 20:35:17 +08:00
2021-03-22 07:04:12 +08:00
.. _invoke-other:
Other ways of calling pytest
-----------------------------------------------------
2010-11-07 05:17:33 +08:00
2021-03-22 07:04:12 +08:00
.. _invoke-python:
2019-04-28 23:37:58 +08:00
2021-03-22 07:04:12 +08:00
Calling pytest through `` python -m pytest ``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can invoke testing through the Python interpreter from the command line:
.. code-block :: text
python -m pytest [...]
This is almost equivalent to invoking the command line script `` pytest [...] ``
directly, except that calling via `` python `` will also add the current directory to `` sys.path `` .
.. _`pytest.main-usage`:
Calling pytest from Python code
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-11-07 05:17:33 +08:00
2019-08-07 07:20:06 +08:00
You can invoke `` pytest `` from Python code directly:
2010-11-07 05:17:33 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2021-06-27 21:29:16 +08:00
retcode = pytest.main()
2010-11-07 05:17:33 +08:00
2016-06-21 22:16:57 +08:00
this acts as if you would call "pytest" from the command line.
2021-06-27 21:29:16 +08:00
It will not raise :class: `SystemExit` but return the :ref: `exit code <exit-codes>` instead.
2023-07-09 23:30:33 +08:00
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.
You can pass in options and arguments explicitly:
2010-11-07 05:17:33 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2021-06-27 21:29:16 +08:00
retcode = pytest.main(["-x", "mytestdir"])
2010-11-07 05:17:33 +08:00
2019-08-07 07:20:06 +08:00
You can specify additional plugins to `` pytest.main `` :
2010-11-07 05:17:33 +08:00
2019-08-07 04:25:54 +08:00
.. code-block :: python
2010-11-07 05:17:33 +08:00
# content of myinvoke.py
2021-06-30 14:11:20 +08:00
import sys
2019-08-07 04:34:58 +08:00
2022-04-28 22:30:16 +08:00
import pytest
2019-08-07 04:34:58 +08:00
2019-08-07 04:33:21 +08:00
class MyPlugin:
2012-05-23 00:30:34 +08:00
def pytest_sessionfinish(self):
print("*** test run reporting finishing")
2010-11-07 05:17:33 +08:00
2019-08-07 04:34:58 +08:00
2021-06-27 21:29:16 +08:00
if __name__ == "__main__":
sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()]))
2010-11-07 05:17:33 +08:00
2012-05-23 00:30:34 +08:00
Running it will show that `` MyPlugin `` was added and its
2019-02-15 21:10:37 +08:00
hook was invoked:
.. code-block :: pytest
2010-11-07 05:17:33 +08:00
$ python myinvoke.py
2021-10-04 14:56:26 +08:00
*** test run reporting finishing
2019-01-06 03:19:40 +08:00
2018-01-26 05:07:34 +08:00
.. note ::
Calling `` pytest.main() `` will result in importing your tests and any modules
that they import. Due to the caching mechanism of python's import system,
making subsequent calls to `` pytest.main() `` from the same process will not
reflect changes to those files between the calls. For this reason, making
multiple calls to `` pytest.main() `` from the same process (in order to re-run
tests, for example) is not recommended.