.\" Man page generated from reStructuredText. . .TH "PY.TEST" "1" "October 21, 2014" "2.6" "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 THROUGH PYTHON -M PY.TEST .sp New in version 2.0. .sp If you use Python\-2.5 or later 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 \fBpy.test [...]\fP directly. .SH GETTING HELP ON VERSION, OPTION NAMES, ENVIRONMENT VARIABLES .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C py.test \-\-version # shows where pytest was imported from py.test \-\-fixtures # show available builtin function arguments py.test \-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 py.test \-x # stop after first failure py.test \-\-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 py.test test_mod.py # run tests in module 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 py.test 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 .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 py.test \-\-pyargs pkg # run all tests found below directory of pypkg .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 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 .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 py.test \-\-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 py.test \-x \-\-pdb # drop to PDB on first failure, then end test session py.test \-\-pdb \-\-maxfail=3 # drop to PDB for first three failures .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 \fBpy.test \-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 occuring 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 \fBpy.test \-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 py.test \-\-durations=10 .ft P .fi .UNINDENT .UNINDENT .SH CREATING JUNITXML FORMAT FILES .sp To create result files which can be read by \fI\%Hudson\fP or other Continuous integration servers, use this invocation: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C py.test \-\-junitxml=path .ft P .fi .UNINDENT .UNINDENT .sp to create an XML file at \fBpath\fP\&. .SH CREATING RESULTLOG FORMAT FILES .sp To create plain\-text machine\-readable result files you can issue: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C py.test \-\-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 py.test \-\-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 py.test \-\-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 py.test like this: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C py.test \-p no:doctest .ft P .fi .UNINDENT .UNINDENT .SH CALLING PY.TEST 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 "py.test" 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 2014, holger krekel .\" Generated by docutils manpage writer. .