Pytest supports several ways to run and select tests from the command-line.

Run tests in a module

pytesttest_mod.py

Run tests in a directory

pytesttesting/

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.

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:

pytesttest_mod.py::test_func

Another example specifying a test method in the command line:

pytesttest_mod.py::TestClass::test_method

Run tests by marker expressions

pytest-mslow

Will run all tests which are decorated with the @pytest.mark.slow decorator.

pytest--showlocals# show local variables in tracebackspytest-l# show local variables (shortcut)pytest--tb=auto# (default) 'long' tracebacks for the first and last# entry, but 'short' style for the other entriespytest--tb=long# exhaustive, informative traceback formattingpytest--tb=short# shorter traceback formatpytest--tb=line# only one line per failurepytest--tb=native# Python standard library formattingpytest--tb=no# no traceback at all

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
KeyboardInterrupt (Ctrl+C).
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
will be shown (because KeyboardInterrupt is caught by pytest). By using this
option you make sure a trace is shown.

Python comes with a builtin Python debugger called PDB. pytest
allows one to drop into the PDB prompt via a command line option:

pytest--pdb

This will invoke the Python debugger on every failure (or KeyboardInterrupt).
Often you might only want to do this for the first failing test to understand
a certain failure situation:

pytest-x--pdb# drop to PDB on first failure, then end test sessionpytest--pdb--maxfail=3# drop to PDB for first three failures

Note that on any failure the exception information is stored on
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:

To set a breakpoint in your code use the native Python importpdb;pdb.set_trace() call
in your code and pytest automatically disables its output capture for that test:

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
for test output occurring after you exit the interactive PDB tracing session
and continue with the regular test run.

Unlike record_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":

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.

Running it will show that MyPlugin was added and its
hook was invoked:

$ python myinvoke.py
. [100%]*** test run reporting finishing

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.