2010-10-11 05:45:45 +08:00
|
|
|
|
2010-10-12 16:59:04 +08:00
|
|
|
.. _`captures`:
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
Capturing of the stdout/stderr output
|
2010-10-11 05:45:45 +08:00
|
|
|
=========================================================
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
Default stdout/stderr/stdin capturing behaviour
|
|
|
|
---------------------------------------------------------
|
|
|
|
|
|
|
|
During test execution any output sent to ``stdout`` and ``stderr`` is
|
|
|
|
captured. If a test or a setup method fails its according captured
|
|
|
|
output will usually be shown along with the failure traceback.
|
|
|
|
|
|
|
|
In addition, ``stdin`` is set to a "null" object which will
|
|
|
|
fail on attempts to read from it because it is rarely desired
|
|
|
|
to wait for interactive input when running automated tests.
|
|
|
|
|
|
|
|
By default capturing is done by intercepting writes to low level
|
|
|
|
file descriptors. This allows to capture output from simple
|
|
|
|
print statements as well as output from a subprocess started by
|
|
|
|
a test.
|
|
|
|
|
|
|
|
Setting capturing methods or disabling capturing
|
|
|
|
-------------------------------------------------
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
There are two ways in which ``pytest`` can perform capturing:
|
2011-03-04 06:40:38 +08:00
|
|
|
|
|
|
|
* file descriptor (FD) level capturing (default): All writes going to the
|
|
|
|
operating system file descriptors 1 and 2 will be captured.
|
|
|
|
|
|
|
|
* ``sys`` level capturing: Only writes to Python files ``sys.stdout``
|
|
|
|
and ``sys.stderr`` will be captured. No capturing of writes to
|
|
|
|
filedescriptors is performed.
|
|
|
|
|
|
|
|
.. _`disable capturing`:
|
|
|
|
|
|
|
|
You can influence output capturing mechanisms from the command line::
|
|
|
|
|
|
|
|
py.test -s # disable all capturing
|
|
|
|
py.test --capture=sys # replace sys.stdout/stderr with in-mem files
|
|
|
|
py.test --capture=fd # also point filedescriptors 1 and 2 to temp file
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2010-12-06 17:41:20 +08:00
|
|
|
.. _printdebugging:
|
|
|
|
|
|
|
|
Using print statements for debugging
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
One primary benefit of the default capturing of stdout/stderr output
|
|
|
|
is that you can use print statements for debugging::
|
|
|
|
|
|
|
|
# content of test_module.py
|
2014-01-18 19:31:33 +08:00
|
|
|
|
2010-12-06 17:41:20 +08:00
|
|
|
def setup_function(function):
|
|
|
|
print ("setting up %s" % function)
|
|
|
|
|
|
|
|
def test_func1():
|
|
|
|
assert True
|
|
|
|
|
|
|
|
def test_func2():
|
|
|
|
assert False
|
|
|
|
|
|
|
|
and running this module will show you precisely the output
|
|
|
|
of the failing function and hide the other one::
|
|
|
|
|
|
|
|
$ py.test
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
2016-03-01 04:52:56 +08:00
|
|
|
platform linux -- Python 3.4.0, pytest-2.9.0, py-1.4.31, pluggy-0.3.1
|
2015-06-07 05:30:49 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile:
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 2 items
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2010-12-06 17:41:20 +08:00
|
|
|
test_module.py .F
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= FAILURES ========
|
|
|
|
_______ test_func2 ________
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2010-12-06 17:41:20 +08:00
|
|
|
def test_func2():
|
|
|
|
> assert False
|
|
|
|
E assert False
|
2014-01-29 20:47:11 +08:00
|
|
|
|
2010-12-06 17:41:20 +08:00
|
|
|
test_module.py:9: AssertionError
|
2015-09-22 20:02:11 +08:00
|
|
|
-------------------------- Captured stdout setup ---------------------------
|
2015-06-07 05:30:49 +08:00
|
|
|
setting up <function test_func2 at 0xdeadbeef>
|
|
|
|
======= 1 failed, 1 passed in 0.12 seconds ========
|
2010-12-06 17:41:20 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
Accessing captured output from a test function
|
|
|
|
---------------------------------------------------
|
|
|
|
|
2014-04-07 19:42:48 +08:00
|
|
|
The ``capsys`` and ``capfd`` fixtures allow to access stdout/stderr
|
|
|
|
output created during test execution. Here is an example test function
|
2015-07-10 08:50:38 +08:00
|
|
|
that performs some output related checks:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def test_myoutput(capsys): # or use "capfd" for fd-level
|
|
|
|
print ("hello")
|
|
|
|
sys.stderr.write("world\n")
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out == "hello\n"
|
|
|
|
assert err == "world\n"
|
|
|
|
print "next"
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out == "next\n"
|
|
|
|
|
|
|
|
The ``readouterr()`` call snapshots the output so far -
|
|
|
|
and capturing will be continued. After the test
|
|
|
|
function finishes the original streams will
|
|
|
|
be restored. Using ``capsys`` this way frees your
|
|
|
|
test from having to care about setting/resetting
|
2014-01-18 19:31:33 +08:00
|
|
|
output streams and also interacts well with pytest's
|
2010-10-11 05:45:45 +08:00
|
|
|
own per-test capturing.
|
|
|
|
|
2014-04-07 19:42:48 +08:00
|
|
|
If you want to capture on filedescriptor level you can use
|
2010-10-11 05:45:45 +08:00
|
|
|
the ``capfd`` function argument which offers the exact
|
2014-04-07 19:42:48 +08:00
|
|
|
same interface but allows to also capture output from
|
|
|
|
libraries or subprocesses that directly write to operating
|
|
|
|
system level output streams (FD1 and FD2).
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
.. include:: links.inc
|