.\" Man page generated from reStructuredText. . .TH "PY.TEST-3" "1" "October 24, 2016" "3.0.3" "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 PY.TEST-3 THROUGH PYTHON -M PY.TEST-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 equivalent to invoking the command line script \fBpytest [...]\fP directly. .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 Several test run options: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest test_mod.py # run tests in module pytest somepath # run all tests below somepath pytest \-k stringexpr # only run tests with names that match the # "string expression", e.g. "MyClass and not method" # will select TestMyClass.test_something # but not TestMyClass.test_method_simple pytest test_mod.py::test_func # only run tests that match the "node ID", # e.g "test_mod.py::test_func" will select # only test_func in test_mod.py pytest test_mod.py::TestClass::test_method # run a single method in # a single class .ft P .fi .UNINDENT .UNINDENT .sp Import \(aqpkg\(aq and use its filesystem location to find and run tests: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest \-\-pyargs pkg # run all tests found below directory of pkg .ft P .fi .UNINDENT .UNINDENT .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 \fBKeyboardInterrrupt\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 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. 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 SETTING A BREAKPOINT / AKA SET_TRACE() .sp If you want to set a breakpoint and enter the \fBpdb.set_trace()\fP you can use a helper: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C import pytest def test_function(): ... pytest.set_trace() # invoke PDB debugger and tracing .ft P .fi .UNINDENT .UNINDENT .sp Prior to pytest version 2.0.0 you could only enter \fI\%PDB\fP tracing if you disabled capturing on the command line via \fBpytest \-s\fP\&. In later versions, pytest automatically disables its output capture when you enter \fI\%PDB\fP tracing: .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 .sp Since pytest version 2.4.0 you can also use the native Python \fBimport pdb;pdb.set_trace()\fP call to enter \fI\%PDB\fP tracing without having to use the \fBpytest.set_trace()\fP wrapper or explicitly disable pytest\(aqs output capturing via \fBpytest \-s\fP\&. .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 .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\&. .SS record_xml_property .sp New in version 2.8. .sp If you want to log additional information for a test, you can use the \fBrecord_xml_property\fP fixture: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C def test_function(record_xml_property): record_xml_property("example_key", 1) assert 0 .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 \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, however. .sp Currently it does not work when used with the \fBpytest\-xdist\fP plugin. .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 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(\(aqjunitxml\(aq): my_junit = getattr(pytest.config, \(aq_xml\(aq, None) my_junit.add_global_property(\(aqARCH\(aq, \(aqPPC\(aq) my_junit.add_global_property(\(aqSTORAGE_TYPE\(aq, \(aqCEPH\(aq) @pytest.mark.usefixtures(log_global_env_facts) def start_and_prepare_env(): pass class TestMe: 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 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 PY.TEST-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 or pass in a string: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C pytest.main("\-x mytestdir") .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: 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 *** test run reporting finishing .ft P .fi .UNINDENT .UNINDENT .SH AUTHOR holger krekel at merlinux eu .SH COPYRIGHT 2015, holger krekel and pytest-dev team .\" Generated by docutils manpage writer. .