.\" Man page generated from reStructuredText. . .TH "PYTEST-3" "1" "Dec 21, 2018" "3.10.1" "pytest" .SH NAME pytest \- pytest usage . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH CALLING PYTEST-3 THROUGH PYTHON -M PYTEST-3 .sp New in version 2.0. .sp You can invoke testing through the Python interpreter from the command line: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C python \-m pytest [...] .ft P .fi .UNINDENT .UNINDENT .sp This is almost equivalent to invoking the command line script \fBpytest [...]\fP directly, except that calling via \fBpython\fP will also add the current directory to \fBsys.path\fP\&. .SH POSSIBLE EXIT CODES .sp Running \fBpytest\fP can result in six different exit codes: .INDENT 0.0 .TP .B Exit code 0 All tests were collected and passed successfully .TP .B Exit code 1 Tests were collected and run but some of the tests failed .TP .B Exit code 2 Test execution was interrupted by the user .TP .B Exit code 3 Internal error happened while executing tests .TP .B Exit code 4 pytest command line usage error .TP .B Exit code 5 No tests were collected .UNINDENT .SH GETTING HELP ON VERSION, OPTION NAMES, ENVIRONMENT VARIABLES .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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 .ft P .fi .UNINDENT .UNINDENT .SH STOPPING AFTER THE FIRST (OR N) FAILURES .sp To stop the testing process after the first (N) failures: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-x # stop after first failure pytest \-\-maxfail=2 # stop after two failures .ft P .fi .UNINDENT .UNINDENT .SH SPECIFYING TESTS / SELECTING TESTS .sp Pytest supports several ways to run and select tests from the command\-line. .sp \fBRun tests in a module\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest test_mod.py .ft P .fi .UNINDENT .UNINDENT .sp \fBRun tests in a directory\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest testing/ .ft P .fi .UNINDENT .UNINDENT .sp \fBRun tests by keyword expressions\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-k "MyClass and not method" .ft P .fi .UNINDENT .UNINDENT .sp This will run tests which contain names that match the given \fIstring expression\fP, which can include Python operators that use filenames, class names and function names as variables. The example above will run \fBTestMyClass.test_something\fP but not \fBTestMyClass.test_method_simple\fP\&. .sp \fBRun tests by node ids\fP .sp Each collected test is assigned a unique \fBnodeid\fP which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by \fB::\fP characters. .sp To run a specific test within a module: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest test_mod.py::test_func .ft P .fi .UNINDENT .UNINDENT .sp Another example specifying a test method in the command line: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest test_mod.py::TestClass::test_method .ft P .fi .UNINDENT .UNINDENT .sp \fBRun tests by marker expressions\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-m slow .ft P .fi .UNINDENT .UNINDENT .sp Will run all tests which are decorated with the \fB@pytest.mark.slow\fP decorator. .sp For more information see marks\&. .sp \fBRun tests from packages\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-pyargs pkg.testing .ft P .fi .UNINDENT .UNINDENT .sp This will import \fBpkg.testing\fP and use its filesystem location to find and run tests from. .SH MODIFYING PYTHON TRACEBACK PRINTING .sp Examples for modifying traceback printing: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-showlocals # show local variables in tracebacks pytest \-l # show local variables (shortcut) pytest \-\-tb=auto # (default) \(aqlong\(aq tracebacks for the first and last # entry, but \(aqshort\(aq style for the other entries 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 .ft P .fi .UNINDENT .UNINDENT .sp The \fB\-\-full\-trace\fP causes very long traces to be printed on error (longer than \fB\-\-tb=long\fP). It also ensures that a stack trace is printed on \fBKeyboardInterrupt\fP (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 \fIhanging\fP\&. By default no output will be shown (because KeyboardInterrupt is caught by pytest). By using this option you make sure a trace is shown. .SH DETAILED SUMMARY REPORT .sp New in version 2.9. .sp The \fB\-r\fP flag can be used to display test results summary at the end of the test session, making it easy in large test suites to get a clear picture of all failures, skips, xfails, etc. .sp Example: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ pytest \-ra =========================== test session starts ============================ platform linux \-\- Python 3.x.y, pytest\-3.x.y, py\-1.x.y, pluggy\-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 0 items ======================= no tests ran in 0.12 seconds ======================= .ft P .fi .UNINDENT .UNINDENT .sp The \fB\-r\fP options accepts a number of characters after it, with \fBa\fP used above meaning "all except passes". .sp Here is the full list of available characters that can be used: .INDENT 0.0 .INDENT 3.5 .INDENT 0.0 .IP \(bu 2 \fBf\fP \- failed .IP \(bu 2 \fBE\fP \- error .IP \(bu 2 \fBs\fP \- skipped .IP \(bu 2 \fBx\fP \- xfailed .IP \(bu 2 \fBX\fP \- xpassed .IP \(bu 2 \fBp\fP \- passed .IP \(bu 2 \fBP\fP \- passed with output .IP \(bu 2 \fBa\fP \- all except \fBpP\fP .UNINDENT .UNINDENT .UNINDENT .sp More than one character can be used, so for example to only see failed and skipped tests, you can execute: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ pytest \-rfs =========================== test session starts ============================ platform linux \-\- Python 3.x.y, pytest\-3.x.y, py\-1.x.y, pluggy\-0.x.y rootdir: $REGENDOC_TMPDIR, inifile: collected 0 items ======================= no tests ran in 0.12 seconds ======================= .ft P .fi .UNINDENT .UNINDENT .SH DROPPING TO PDB (PYTHON DEBUGGER) ON FAILURES .sp Python comes with a builtin Python debugger called \fI\%PDB\fP\&. \fBpytest\fP allows one to drop into the \fI\%PDB\fP prompt via a command line option: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-pdb .ft P .fi .UNINDENT .UNINDENT .sp 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: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-x \-\-pdb # drop to PDB on first failure, then end test session pytest \-\-pdb \-\-maxfail=3 # drop to PDB for first three failures .ft P .fi .UNINDENT .UNINDENT .sp Note that on any failure the exception information is stored on \fBsys.last_value\fP, \fBsys.last_type\fP and \fBsys.last_traceback\fP\&. 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: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C >>> import sys >>> sys.last_traceback.tb_lineno 42 >>> sys.last_value AssertionError(\(aqassert result == "ok"\(aq,) .ft P .fi .UNINDENT .UNINDENT .SH DROPPING TO PDB (PYTHON DEBUGGER) AT THE START OF A TEST .sp \fBpytest\fP allows one to drop into the \fI\%PDB\fP prompt immediately at the start of each test via a command line option: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-trace .ft P .fi .UNINDENT .UNINDENT .sp This will invoke the Python debugger at the start of every test. .SH SETTING BREAKPOINTS .sp To set a breakpoint in your code use the native Python \fBimport pdb;pdb.set_trace()\fP call in your code and pytest automatically disables its output capture for that test: .INDENT 0.0 .IP \(bu 2 Output capture in other tests is not affected. .IP \(bu 2 Any prior test output that has already been captured and will be processed as such. .IP \(bu 2 Any later output produced within the same test will not be captured and will instead get sent directly to \fBsys.stdout\fP\&. Note that this holds true even for test output occurring after you exit the interactive \fI\%PDB\fP tracing session and continue with the regular test run. .UNINDENT .SH USING THE BUILTIN BREAKPOINT FUNCTION .sp Python 3.7 introduces a builtin \fBbreakpoint()\fP function. Pytest supports the use of \fBbreakpoint()\fP with the following behaviours: .INDENT 0.0 .INDENT 3.5 .INDENT 0.0 .IP \(bu 2 When \fBbreakpoint()\fP is called and \fBPYTHONBREAKPOINT\fP is set to the default value, pytest will use the custom internal PDB trace UI instead of the system default \fBPdb\fP\&. .IP \(bu 2 When tests are complete, the system will default back to the system \fBPdb\fP trace UI. .IP \(bu 2 With \fB\-\-pdb\fP passed to pytest, the custom internal Pdb trace UI is used with both \fBbreakpoint()\fP and failed tests/unhandled exceptions. .IP \(bu 2 \fB\-\-pdbcls\fP can be used to specify a custom debugger class. .UNINDENT .UNINDENT .UNINDENT .SH PROFILING TEST EXECUTION DURATION .sp To get a list of the slowest 10 test durations: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-durations=10 .ft P .fi .UNINDENT .UNINDENT .sp By default, pytest will not show test durations that are too small (<0.01s) unless \fB\-vv\fP is passed on the command\-line. .SH CREATING JUNITXML FORMAT FILES .sp To create result files which can be read by \fI\%Jenkins\fP or other Continuous integration servers, use this invocation: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-junitxml=path .ft P .fi .UNINDENT .UNINDENT .sp to create an XML file at \fBpath\fP\&. .sp New in version 3.1. .sp To set the name of the root test suite xml item, you can configure the \fBjunit_suite_name\fP option in your config file: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C [pytest] junit_suite_name = my_suite .ft P .fi .UNINDENT .UNINDENT .SS record_property .sp New in version 2.8. .sp Changed in version 3.5: Fixture renamed from \fBrecord_xml_property\fP to \fBrecord_property\fP as user properties are now available to all reporters. \fBrecord_xml_property\fP is now deprecated. .sp If you want to log additional information for a test, you can use the \fBrecord_property\fP fixture: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C def test_function(record_property): record_property("example_key", 1) assert True .ft P .fi .UNINDENT .UNINDENT .sp This will add an extra property \fBexample_key="1"\fP to the generated \fBtestcase\fP tag: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .sp Alternatively, you can integrate this functionality with custom markers: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # content of conftest.py def pytest_collection_modifyitems(session, config, items): for item in items: for marker in item.iter_markers(name="test_id"): test_id = marker.args[0] item.user_properties.append(("test_id", test_id)) .ft P .fi .UNINDENT .UNINDENT .sp And in your tests: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # content of test_function.py import pytest @pytest.mark.test_id(1501) def test_function(): assert True .ft P .fi .UNINDENT .UNINDENT .sp Will result in: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 \fBrecord_property\fP is an experimental feature and may change in the future. .sp Also please note that using this feature will break any schema verification. This might be a problem when used with some CI servers. .UNINDENT .UNINDENT .SS record_xml_attribute .sp New in version 3.4. .sp To add an additional xml attribute to a testcase element, you can use \fBrecord_xml_attribute\fP fixture. This can also be used to override existing values: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C def test_function(record_xml_attribute): record_xml_attribute("assertions", "REQ\-1234") record_xml_attribute("classname", "custom_classname") print("hello world") assert True .ft P .fi .UNINDENT .UNINDENT .sp Unlike \fBrecord_property\fP, this will not add a new child element. Instead, this will add an attribute \fBassertions="REQ\-1234"\fP inside the generated \fBtestcase\fP tag and override the default \fBclassname\fP with \fB"classname=custom_classname"\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C hello world .ft P .fi .UNINDENT .UNINDENT .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 \fBrecord_xml_attribute\fP 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. .sp Using this over \fBrecord_xml_property\fP 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. .sp Below is the Scheme used by Jenkins to validate the XML report: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .UNINDENT .UNINDENT .SS LogXML: add_global_property .sp New in version 3.0. .sp 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 \fBLogXML.add_global_properties\fP .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C 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.__name__) def start_and_prepare_env(): pass class TestMe(object): def test_foo(self): assert True .ft P .fi .UNINDENT .UNINDENT .sp This will add a property node below the testsuite node to the generated xml: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C .ft P .fi .UNINDENT .UNINDENT .sp \fBWARNING:\fP .INDENT 0.0 .INDENT 3.5 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. .UNINDENT .UNINDENT .SH CREATING RESULTLOG FORMAT FILES .sp Deprecated since version 3.0: This option is rarely used and is scheduled for removal in 4.0. .sp An alternative for users which still need similar functionality is to use the \fI\%pytest\-tap\fP plugin which provides a stream of test data. .sp If you have any concerns, please don\(aqt hesitate to \fI\%open an issue\fP\&. .sp To create plain\-text machine\-readable result files you can issue: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-resultlog=path .ft P .fi .UNINDENT .UNINDENT .sp and look at the content at the \fBpath\fP location. Such files are used e.g. by the \fI\%PyPy\-test\fP web page to show test results over several revisions. .SH SENDING TEST REPORT TO ONLINE PASTEBIN SERVICE .sp \fBCreating a URL for each test failure\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-pastebin=failed .ft P .fi .UNINDENT .UNINDENT .sp 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 \fB\-x\fP if you only want to send one particular failure. .sp \fBCreating a URL for a whole test session log\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-pastebin=all .ft P .fi .UNINDENT .UNINDENT .sp Currently only pasting to the \fI\%http://bpaste.net\fP service is implemented. .SH DISABLING PLUGINS .sp To disable loading specific plugins at invocation time, use the \fB\-p\fP option together with the prefix \fBno:\fP\&. .sp Example: to disable loading the plugin \fBdoctest\fP, which is responsible for executing doctest tests from text files, invoke pytest like this: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-p no:doctest .ft P .fi .UNINDENT .UNINDENT .SH CALLING PYTEST-3 FROM PYTHON CODE .sp New in version 2.0. .sp You can invoke \fBpytest\fP from Python code directly: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest.main() .ft P .fi .UNINDENT .UNINDENT .sp this acts as if you would call "pytest" from the command line. It will not raise \fBSystemExit\fP but return the exitcode instead. You can pass in options and arguments: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest.main([\(aq\-x\(aq, \(aqmytestdir\(aq]) .ft P .fi .UNINDENT .UNINDENT .sp You can specify additional plugins to \fBpytest.main\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C # content of myinvoke.py import pytest class MyPlugin(object): def pytest_sessionfinish(self): print("*** test run reporting finishing") pytest.main(["\-qq"], plugins=[MyPlugin()]) .ft P .fi .UNINDENT .UNINDENT .sp Running it will show that \fBMyPlugin\fP was added and its hook was invoked: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ python myinvoke.py \&. [100%]*** test run reporting finishing .ft P .fi .UNINDENT .UNINDENT .sp \fBNOTE:\fP .INDENT 0.0 .INDENT 3.5 Calling \fBpytest.main()\fP will result in importing your tests and any modules that they import. Due to the caching mechanism of python\(aqs import system, making subsequent calls to \fBpytest.main()\fP from the same process will not reflect changes to those files between the calls. For this reason, making multiple calls to \fBpytest.main()\fP from the same process (in order to re\-run tests, for example) is not recommended. .UNINDENT .UNINDENT .SH AUTHOR holger krekel at merlinux eu .SH COPYRIGHT 2015–2018 , holger krekel and pytest-dev team .\" Generated by docutils manpage writer. .