2010-10-14 01:30:00 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
.. _usage:
|
|
|
|
|
|
|
|
Usage and Invocations
|
|
|
|
==========================================
|
2010-10-14 07:25:09 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
.. _cmdline:
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Calling pytest through ``python -m pytest``
|
2010-11-13 18:10:45 +08:00
|
|
|
-----------------------------------------------------
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
.. versionadded:: 2.0
|
2010-11-13 18:10:45 +08:00
|
|
|
|
2015-12-17 02:14:36 +08:00
|
|
|
You can invoke testing through the Python interpreter from the command line::
|
2010-11-13 18:10:45 +08:00
|
|
|
|
|
|
|
python -m pytest [...]
|
|
|
|
|
2016-12-11 02:55:04 +08:00
|
|
|
This is almost equivalent to invoking the command line script ``pytest [...]``
|
2017-10-19 03:30:56 +08:00
|
|
|
directly, except that calling via ``python`` will also add the current directory to ``sys.path``.
|
2010-11-13 18:10:45 +08:00
|
|
|
|
2017-03-20 02:34:43 +08:00
|
|
|
Possible exit codes
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
Running ``pytest`` can result in six different exit codes:
|
|
|
|
|
|
|
|
:Exit code 0: All tests were collected and passed successfully
|
|
|
|
:Exit code 1: Tests were collected and run but some of the tests failed
|
|
|
|
:Exit code 2: Test execution was interrupted by the user
|
|
|
|
:Exit code 3: Internal error happened while executing tests
|
|
|
|
:Exit code 4: pytest command line usage error
|
|
|
|
:Exit code 5: No tests were collected
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
Getting help on version, option names, environment variables
|
|
|
|
--------------------------------------------------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
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
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
.. _maxfail:
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
Stopping after the first (or N) failures
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
To stop the testing process after the first (N) failures::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest -x # stop after first failure
|
|
|
|
pytest --maxfail=2 # stop after two failures
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
.. _select-tests:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Specifying tests / selecting tests
|
2010-11-07 05:17:33 +08:00
|
|
|
---------------------------------------------------
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2017-07-19 04:04:37 +08:00
|
|
|
Pytest supports several ways to run and select tests from the command-line.
|
|
|
|
|
|
|
|
**Run tests in a module**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
pytest test_mod.py
|
|
|
|
|
|
|
|
**Run tests in a directory**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
pytest testing/
|
|
|
|
|
|
|
|
**Run tests by keyword expressions**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
pytest -k "MyClass and not method"
|
|
|
|
|
|
|
|
This will run tests which contain names that match the given *string expression*, which can
|
|
|
|
include Python operators that use filenames, class names and function names as variables.
|
|
|
|
The example above will run ``TestMyClass.test_something`` but not ``TestMyClass.test_method_simple``.
|
|
|
|
|
|
|
|
.. _nodeids:
|
|
|
|
|
|
|
|
**Run tests by node ids**
|
|
|
|
|
|
|
|
Each collected test is assigned a unique ``nodeid`` which consist of the module filename followed
|
|
|
|
by specifiers like class names, function names and parameters from parametrization, separated by ``::`` characters.
|
|
|
|
|
|
|
|
To run a specific test within a module::
|
|
|
|
|
|
|
|
pytest test_mod.py::test_func
|
|
|
|
|
|
|
|
|
|
|
|
Another example specifying a test method in the command line::
|
|
|
|
|
|
|
|
pytest test_mod.py::TestClass::test_method
|
|
|
|
|
|
|
|
**Run tests by marker expressions**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
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**
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
pytest --pyargs pkg.testing
|
|
|
|
|
|
|
|
This will import ``pkg.testing`` and use its filesystem location to find and run tests from.
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
Modifying Python traceback printing
|
|
|
|
----------------------------------------------
|
|
|
|
|
|
|
|
Examples for modifying traceback printing::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --showlocals # show local variables in tracebacks
|
|
|
|
pytest -l # show local variables (shortcut)
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --tb=auto # (default) 'long' tracebacks for the first and last
|
2016-02-28 04:54:09 +08:00
|
|
|
# entry, but 'short' style for the other entries
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --tb=long # exhaustive, informative traceback formatting
|
|
|
|
pytest --tb=short # shorter traceback format
|
|
|
|
pytest --tb=line # only one line per failure
|
|
|
|
pytest --tb=native # Python standard library formatting
|
|
|
|
pytest --tb=no # no traceback at all
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2016-03-21 02:53:02 +08:00
|
|
|
The ``--full-trace`` causes very long traces to be printed on error (longer
|
|
|
|
than ``--tb=long``). It also ensures that a stack trace is printed on
|
2017-01-01 01:54:47 +08:00
|
|
|
**KeyboardInterrupt** (Ctrl+C).
|
2016-03-21 02:53:02 +08:00
|
|
|
This is very useful if the tests are taking too long and you interrupt them
|
|
|
|
with Ctrl+C to find out where the tests are *hanging*. By default no output
|
2016-06-27 20:41:40 +08:00
|
|
|
will be shown (because KeyboardInterrupt is caught by pytest). By using this
|
2016-03-21 02:53:02 +08:00
|
|
|
option you make sure a trace is shown.
|
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
|
|
|
|
.. _pdb-option:
|
|
|
|
|
2014-02-01 17:11:42 +08:00
|
|
|
Dropping to PDB_ (Python Debugger) on failures
|
|
|
|
-----------------------------------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
.. _PDB: http://docs.python.org/library/pdb.html
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Python comes with a builtin Python debugger called PDB_. ``pytest``
|
2014-02-01 17:11:42 +08:00
|
|
|
allows one to drop into the PDB_ prompt via a command line option::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --pdb
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
This will invoke the Python debugger on every failure. Often you might
|
|
|
|
only want to do this for the first failing test to understand a certain
|
|
|
|
failure situation::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest -x --pdb # drop to PDB on first failure, then end test session
|
|
|
|
pytest --pdb --maxfail=3 # drop to PDB for first three failures
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2015-09-05 03:11:57 +08:00
|
|
|
Note that on any failure the exception information is stored on
|
2015-03-22 00:26:23 +08:00
|
|
|
``sys.last_value``, ``sys.last_type`` and ``sys.last_traceback``. In
|
|
|
|
interactive use, this allows one to drop into postmortem debugging with
|
|
|
|
any debug tool. One can also manually access the exception information,
|
|
|
|
for example::
|
|
|
|
|
2015-07-10 08:50:38 +08:00
|
|
|
>>> import sys
|
|
|
|
>>> sys.last_traceback.tb_lineno
|
2015-03-22 00:26:23 +08:00
|
|
|
42
|
2015-07-10 08:50:38 +08:00
|
|
|
>>> sys.last_value
|
2015-03-22 00:26:23 +08:00
|
|
|
AssertionError('assert result == "ok"',)
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
.. _breakpoints:
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
Setting breakpoints
|
|
|
|
-------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
.. versionadded: 2.4.0
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
To set a breakpoint in your code use the native Python ``import pdb;pdb.set_trace()`` call
|
|
|
|
in your code and pytest automatically disables its output capture for that test:
|
2014-02-01 17:19:09 +08:00
|
|
|
|
|
|
|
* Output capture in other tests is not affected.
|
|
|
|
* Any prior test output that has already been captured and will be processed as
|
|
|
|
such.
|
|
|
|
* Any later output produced within the same test will not be captured and will
|
|
|
|
instead get sent directly to ``sys.stdout``. Note that this holds true even
|
2015-11-28 14:46:45 +08:00
|
|
|
for test output occurring after you exit the interactive PDB_ tracing session
|
2014-02-01 17:19:09 +08:00
|
|
|
and continue with the regular test run.
|
|
|
|
|
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
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
.. versionadded: 2.2
|
|
|
|
|
|
|
|
To get a list of the slowest 10 test durations::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --durations=10
|
2011-11-09 01:20:56 +08:00
|
|
|
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Creating JUnitXML format files
|
2010-10-14 01:30:00 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2016-04-05 20:08:30 +08:00
|
|
|
To create result files which can be read by Jenkins_ or other Continuous
|
2010-10-14 01:30:00 +08:00
|
|
|
integration servers, use this invocation::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --junitxml=path
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
to create an XML file at ``path``.
|
|
|
|
|
2017-05-12 09:54:15 +08:00
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
|
|
|
To set the name of the root test suite xml item, you can configure the ``junit_suite_name`` option in your config file:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[pytest]
|
|
|
|
junit_suite_name = my_suite
|
|
|
|
|
2018-02-28 04:58:51 +08:00
|
|
|
.. _record_xml_property example:
|
|
|
|
|
2015-08-23 22:45:39 +08:00
|
|
|
record_xml_property
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2015-08-20 05:36:42 +08:00
|
|
|
|
2015-08-23 22:45:39 +08:00
|
|
|
.. versionadded:: 2.8
|
|
|
|
|
|
|
|
If you want to log additional information for a test, you can use the
|
|
|
|
``record_xml_property`` fixture:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def test_function(record_xml_property):
|
|
|
|
record_xml_property("example_key", 1)
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
This will add an extra property ``example_key="1"`` to the generated
|
|
|
|
``testcase`` tag:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
2015-09-16 19:02:54 +08:00
|
|
|
<testcase classname="test_function" file="test_function.py" line="0" name="test_function" time="0.0009">
|
|
|
|
<properties>
|
|
|
|
<property name="example_key" value="1" />
|
|
|
|
</properties>
|
|
|
|
</testcase>
|
2015-08-23 22:45:39 +08:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2017-03-14 10:04:44 +08:00
|
|
|
``record_xml_property`` is an experimental feature, and its interface might be replaced
|
2015-08-23 22:45:39 +08:00
|
|
|
by something more powerful and general in future versions. The
|
|
|
|
functionality per-se will be kept, however.
|
2015-08-20 05:36:42 +08:00
|
|
|
|
2015-09-26 14:24:07 +08:00
|
|
|
Currently it does not work when used with the ``pytest-xdist`` plugin.
|
|
|
|
|
2015-08-23 22:45:39 +08:00
|
|
|
Also please note that using this feature will break any schema verification.
|
|
|
|
This might be a problem when used with some CI servers.
|
2015-08-22 04:31:20 +08:00
|
|
|
|
2018-01-19 08:06:42 +08:00
|
|
|
record_xml_attribute
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
|
|
|
|
|
|
|
To add an additional xml attribute to a testcase element, you can use
|
|
|
|
``record_xml_attribute`` fixture. This can also be used to override existing values:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def test_function(record_xml_attribute):
|
|
|
|
record_xml_attribute("assertions", "REQ-1234")
|
|
|
|
record_xml_attribute("classname", "custom_classname")
|
|
|
|
print('hello world')
|
|
|
|
assert True
|
|
|
|
|
|
|
|
Unlike ``record_xml_property``, this will not add a new child element.
|
|
|
|
Instead, this will add an attribute ``assertions="REQ-1234"`` inside the generated
|
|
|
|
``testcase`` tag and override the default ``classname`` with ``"classname=custom_classname"``:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<testcase classname="custom_classname" file="test_function.py" line="0" name="test_function" time="0.003" assertions="REQ-1234">
|
|
|
|
<system-out>
|
|
|
|
hello world
|
|
|
|
</system-out>
|
|
|
|
</testcase>
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
``record_xml_attribute`` is an experimental feature, and its interface might be replaced
|
|
|
|
by something more powerful and general in future versions. The
|
|
|
|
functionality per-se will be kept, however.
|
|
|
|
|
|
|
|
Using this over ``record_xml_property`` can help when using ci tools to parse the xml report.
|
|
|
|
However, some parsers are quite strict about the elements and attributes that are allowed.
|
|
|
|
Many tools use an xsd schema (like the example below) to validate incoming xml.
|
|
|
|
Make sure you are using attribute names that are allowed by your parser.
|
|
|
|
|
|
|
|
Below is the Scheme used by Jenkins to validate the XML report:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<xs:element name="testcase">
|
|
|
|
<xs:complexType>
|
|
|
|
<xs:sequence>
|
|
|
|
<xs:element ref="skipped" minOccurs="0" maxOccurs="1"/>
|
|
|
|
<xs:element ref="error" minOccurs="0" maxOccurs="unbounded"/>
|
|
|
|
<xs:element ref="failure" minOccurs="0" maxOccurs="unbounded"/>
|
|
|
|
<xs:element ref="system-out" minOccurs="0" maxOccurs="unbounded"/>
|
|
|
|
<xs:element ref="system-err" minOccurs="0" maxOccurs="unbounded"/>
|
|
|
|
</xs:sequence>
|
|
|
|
<xs:attribute name="name" type="xs:string" use="required"/>
|
|
|
|
<xs:attribute name="assertions" type="xs:string" use="optional"/>
|
|
|
|
<xs:attribute name="time" type="xs:string" use="optional"/>
|
|
|
|
<xs:attribute name="classname" type="xs:string" use="optional"/>
|
|
|
|
<xs:attribute name="status" type="xs:string" use="optional"/>
|
|
|
|
</xs:complexType>
|
|
|
|
</xs:element>
|
|
|
|
|
2016-02-29 22:14:23 +08:00
|
|
|
LogXML: add_global_property
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
2016-07-13 08:02:40 +08:00
|
|
|
.. versionadded:: 3.0
|
2016-02-29 22:14:23 +08:00
|
|
|
|
|
|
|
If you want to add a properties node in the testsuite level, which may contains properties that are relevant
|
|
|
|
to all testcases you can use ``LogXML.add_global_properties``
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def log_global_env_facts(f):
|
|
|
|
|
|
|
|
if pytest.config.pluginmanager.hasplugin('junitxml'):
|
|
|
|
my_junit = getattr(pytest.config, '_xml', None)
|
|
|
|
|
|
|
|
my_junit.add_global_property('ARCH', 'PPC')
|
|
|
|
my_junit.add_global_property('STORAGE_TYPE', 'CEPH')
|
|
|
|
|
|
|
|
@pytest.mark.usefixtures(log_global_env_facts)
|
|
|
|
def start_and_prepare_env():
|
|
|
|
pass
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMe(object):
|
2016-02-29 22:14:23 +08:00
|
|
|
def test_foo(self):
|
|
|
|
assert True
|
|
|
|
|
|
|
|
This will add a property node below the testsuite node to the generated xml:
|
|
|
|
|
|
|
|
.. code-block:: xml
|
|
|
|
|
|
|
|
<testsuite errors="0" failures="0" name="pytest" skips="0" tests="1" time="0.006">
|
|
|
|
<properties>
|
|
|
|
<property name="ARCH" value="PPC"/>
|
|
|
|
<property name="STORAGE_TYPE" value="CEPH"/>
|
|
|
|
</properties>
|
|
|
|
<testcase classname="test_me.TestMe" file="test_me.py" line="16" name="test_foo" time="0.000243663787842"/>
|
|
|
|
</testsuite>
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This is an experimental feature, and its interface might be replaced
|
|
|
|
by something more powerful and general in future versions. The
|
|
|
|
functionality per-se will be kept.
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Creating resultlog format files
|
2010-10-14 01:30:00 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2016-08-17 07:22:15 +08:00
|
|
|
.. deprecated:: 3.0
|
|
|
|
|
|
|
|
This option is rarely used and is scheduled for removal in 4.0.
|
|
|
|
|
2017-09-01 06:11:20 +08:00
|
|
|
An alternative for users which still need similar functionality is to use the
|
|
|
|
`pytest-tap <https://pypi.python.org/pypi/pytest-tap>`_ plugin which provides
|
|
|
|
a stream of test data.
|
|
|
|
|
|
|
|
If you have any concerns, please don't hesitate to
|
|
|
|
`open an issue <https://github.com/pytest-dev/pytest/issues>`_.
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
To create plain-text machine-readable result files you can issue::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --resultlog=path
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
and look at the content at the ``path`` location. Such files are used e.g.
|
|
|
|
by the `PyPy-test`_ web page to show test results over several revisions.
|
|
|
|
|
2012-07-02 19:13:48 +08:00
|
|
|
.. _`PyPy-test`: http://buildbot.pypy.org/summary
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
|
2012-12-12 03:04:12 +08:00
|
|
|
Sending test report to online pastebin service
|
2010-10-14 01:30:00 +08:00
|
|
|
-----------------------------------------------------
|
|
|
|
|
|
|
|
**Creating a URL for each test failure**::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --pastebin=failed
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
This will submit test run information to a remote Paste service and
|
|
|
|
provide a URL for each failure. You may select tests as usual or add
|
|
|
|
for example ``-x`` if you only want to send one particular failure.
|
|
|
|
|
|
|
|
**Creating a URL for a whole test session log**::
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --pastebin=all
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2012-07-02 19:13:48 +08:00
|
|
|
Currently only pasting to the http://bpaste.net service is implemented.
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2014-01-20 05:05:14 +08:00
|
|
|
Disabling plugins
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
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
|
2016-06-21 22:16:57 +08:00
|
|
|
executing doctest tests from text files, invoke pytest like this::
|
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
|
|
|
.. _`pytest.main-usage`:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Calling pytest from Python code
|
2010-11-07 05:17:33 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
.. versionadded:: 2.0
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
You can invoke ``pytest`` from Python code directly::
|
2010-11-07 05:17:33 +08:00
|
|
|
|
|
|
|
pytest.main()
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
this acts as if you would call "pytest" from the command line.
|
2010-11-07 05:17:33 +08:00
|
|
|
It will not raise ``SystemExit`` but return the exitcode instead.
|
|
|
|
You can pass in options and arguments::
|
|
|
|
|
2013-04-04 03:51:06 +08:00
|
|
|
pytest.main(['-x', 'mytestdir'])
|
2010-11-07 05:17:33 +08:00
|
|
|
|
|
|
|
You can specify additional plugins to ``pytest.main``::
|
|
|
|
|
|
|
|
# content of myinvoke.py
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class MyPlugin(object):
|
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
|
|
|
|
2016-08-02 06:36:49 +08:00
|
|
|
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
|
|
|
|
hook was invoked::
|
2010-11-07 05:17:33 +08:00
|
|
|
|
|
|
|
$ python myinvoke.py
|
2012-05-23 00:30:34 +08:00
|
|
|
*** test run reporting finishing
|
2018-01-31 03:47:56 +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.
|
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
.. include:: links.inc
|