Capturing Output and Logs
The pytest captures the stdout and the stderr by default (the -s the off), and the capsys/capfd fixtures read the captured text (the capsys the Python-level, the capfd the file-descriptor the C-level), while the caplog reads the logging output (the caplog.text, the caplog.records). The capture ini option is the fd (the default), the sys, or the no - the fd the capfd, the sys the capsys. This chapter covers the capsys/capfd (the readouterr, the out/err), the caplog (the text, the records, the propagate), the -s (the live, the no capture), and the capture ini option.
The capsys and the capfd
# the capsys: the the PYTHON-level (the
# the
# the)
import pytest
def test_print_captured(capsys):
print("the hello to stdout")
import sys
print("the hello to stderr", file=sys.stderr)
out, err = capsys.readouterr()
assert out == "the hello to stdout\n"
assert err == "the hello to stderr\n"
# the
# the
The capsys is the Python-level capture (the sys.stdout/sys.stderr the replaced, the capsys.readouterr() the (out, err) tuple, the capsys.out/capsys.err the direct) - the print the test inspects, and the capfd is the file-descriptor level (the dup2 the /dev/null, the C-level write the captured, the capfd.readouterr() the same API) - the capfd the C-extension the print the bypass (the sys the no-catch). The capture ini option is the fd (the default, the capfd), the sys (the capsys), or the no (the no capture, the -s the equivalent) - the fd the thorough (the C-level), the sys the Python-only. The -s is the live (the no capture, the print the terminal, the debug) and the --capture=no the ini's - the next section's caplog is the logging half, and the rule is the one: the capsys/capfd the print, the caplog the logging, the -s the debug's live.
The caplog: The logging Capture
# the caplog: the the LOGGING capture (the
# the
# the)
import logging
import pytest
def test_log_captured(caplog):
log = logging.getLogger("texttools")
log.setLevel(logging.INFO)
log.info("the word_count ran")
assert "the word_count ran" in caplog.text
# the
# the
rec = caplog.records[-1]
assert rec.levelno == logging.INFO
assert rec.name == "texttools"
# the
# the
The caplog is the logging capture: the caplog.text the formatted text (the the word_count ran), the caplog.records the LogRecord list (the rec.levelno, the rec.name, the rec.args), and the caplog.set_level(logging.DEBUG, logger="texttools") the level the test controls (the no handler the test's, the caplog the handler). The propagate is the logger's (the caplog the root the reach, the propagate=False the no-root the caplog the no-catch) - the caplog the handler the logger the attached, and the logging the test inspects (the no file, the no console - the caplog the memory). The rule is the one: the capsys/capfd the print, the caplog the logging, the -s the debug's live (the no capture, the terminal) - the three are the output's three surfaces, and the next chapter's coverage is the line the test executes, the pytest-cov the plugin.
The Working Rules
- The
capsysthe Python-level, thecapfdthe C-level. Thereadouterr()the(out, err), the.out/.errthe direct - thecapsysthesys(theprint), thecapfdthefd(the C-extension the bypass), thecaptureini thefd/sys/no. - The
caplogis theloggingcapture. Thecaplog.textthe formatted, thecaplog.recordstheLogRecord(thelevelno, thename), theset_levelthe test's level, thepropagatethe root's reach - thecaplogthe handler the memory. - The
-sis the live, the--capture=nothe ini's. The no capture, theprintthe terminal (the debug), the-sthe per-run, thecapture=nothe ini's the per-project - the two are the live's two forms. - The
printthecapsys, theloggingthecaplog. Theprintthecapsys/capfd, theloggingthecaplog- the surface the output goes to, the fixture the read, the-sthe debug's escape.
Summary
The pytest captures the stdout/stderr by default (the -s the off), and the capsys (the Python-level, the sys) and the capfd (the file-descriptor, the C-level the print the bypass) read the captured text (the readouterr() the (out, err), the .out/.err the direct). The caplog is the logging capture (the caplog.text the formatted, the caplog.records the LogRecord the levelno/name, the set_level the test's level, the propagate the root's reach), and the capture ini option is the fd (the default), the sys, or the no (the -s the equivalent). The rule is the split: the print the capsys/capfd, the logging the caplog, the -s the debug's live - the next chapter's pytest-cov is the coverage, the line the test executes, the --cov the flag, and the --cov-fail-under the threshold the CI enforces.