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
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
If you use Python-2.5 or later you can invoke testing through the
|
2010-11-13 18:10:45 +08:00
|
|
|
Python interpreter from the command line::
|
|
|
|
|
|
|
|
python -m pytest [...]
|
|
|
|
|
|
|
|
This is equivalent to invoking the command line script ``py.test [...]``
|
|
|
|
directly.
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
Getting help on version, option names, environment variables
|
|
|
|
--------------------------------------------------------------
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
py.test --version # shows where pytest was imported from
|
2012-10-05 20:24:44 +08:00
|
|
|
py.test --fixtures # show available builtin function arguments
|
2010-11-02 07:53:53 +08:00
|
|
|
py.test -h | --help # show help on command line and config file options
|
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::
|
|
|
|
|
|
|
|
py.test -x # stop after first failure
|
2012-05-22 19:24:53 +08:00
|
|
|
py.test --maxfail=2 # stop after two failures
|
2010-10-14 01:30:00 +08:00
|
|
|
|
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
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
Several test run options::
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
py.test test_mod.py # run tests in module
|
2013-10-01 03:39:32 +08:00
|
|
|
py.test somepath # run all tests below somepath
|
|
|
|
py.test -k stringexpr # only run tests with names that match the
|
|
|
|
# the "string expression", e.g. "MyClass and not method"
|
|
|
|
# will select TestMyClass.test_something
|
|
|
|
# but not TestMyClass.test_method_simple
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
Import 'pkg' and use its filesystem location to find and run tests::
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2010-11-07 07:22:16 +08:00
|
|
|
py.test --pyargs pkg # run all tests found below directory of pypkg
|
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::
|
|
|
|
|
|
|
|
py.test --showlocals # show local variables in tracebacks
|
|
|
|
py.test -l # show local variables (shortcut)
|
|
|
|
|
|
|
|
py.test --tb=long # the default informative traceback formatting
|
|
|
|
py.test --tb=native # the Python standard library formatting
|
|
|
|
py.test --tb=short # a shorter traceback format
|
|
|
|
py.test --tb=line # only one line per failure
|
|
|
|
|
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
|
|
|
|
|
|
|
py.test --pdb
|
|
|
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
py.test -x --pdb # drop to PDB on first failure, then end test session
|
2014-02-01 17:07:54 +08:00
|
|
|
py.test --pdb --maxfail=3 # drop to PDB for first three failures
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
Setting a breakpoint / aka ``set_trace()``
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
If you want to set a breakpoint and enter the ``pdb.set_trace()`` you
|
|
|
|
can use a helper::
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-14 01:30:00 +08:00
|
|
|
def test_function():
|
|
|
|
...
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.set_trace() # invoke PDB debugger and tracing
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
.. versionadded: 2.0.0
|
|
|
|
|
2014-02-01 17:11:42 +08:00
|
|
|
In previous versions you could only enter PDB_ tracing if
|
2011-12-05 18:10:48 +08:00
|
|
|
you disabled capturing on the command line via ``py.test -s``.
|
2010-10-14 01:30:00 +08:00
|
|
|
|
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::
|
|
|
|
|
|
|
|
py.test --durations=10
|
|
|
|
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Creating JUnitXML format files
|
2010-10-14 01:30:00 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2011-02-17 21:46:40 +08:00
|
|
|
To create result files which can be read by Hudson_ or other Continuous
|
2010-10-14 01:30:00 +08:00
|
|
|
integration servers, use this invocation::
|
|
|
|
|
|
|
|
py.test --junitxml=path
|
|
|
|
|
|
|
|
to create an XML file at ``path``.
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Creating resultlog format files
|
2010-10-14 01:30:00 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
To create plain-text machine-readable result files you can issue::
|
|
|
|
|
|
|
|
py.test --resultlog=path
|
|
|
|
|
|
|
|
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**::
|
|
|
|
|
|
|
|
py.test --pastebin=failed
|
|
|
|
|
|
|
|
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**::
|
|
|
|
|
|
|
|
py.test --pastebin=all
|
|
|
|
|
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
|
|
|
|
executing doctest tests from text files, invoke py.test like this::
|
|
|
|
|
|
|
|
py.test -p no:doctest
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
this acts as if you would call "py.test" from the command line.
|
|
|
|
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
|
|
|
|
|
|
|
or pass in a string::
|
|
|
|
|
|
|
|
pytest.main("-x mytestdir")
|
|
|
|
|
|
|
|
You can specify additional plugins to ``pytest.main``::
|
|
|
|
|
|
|
|
# content of myinvoke.py
|
|
|
|
import pytest
|
|
|
|
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
|
|
|
|
2012-05-23 00:30:34 +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
|
2013-10-04 01:09:18 +08:00
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
.. include:: links.inc
|