August 2018
Beginner
160 pages
4h 8m
English
The capsys fixture captures all text written to sys.stdout and sys.stderr and makes it available during testing.
Suppose we have a small command-line script and want to check the usage instructions are correct when the script is invoked without arguments:
from textwrap import dedentdef script_main(args): if not args: show_usage() return 0 ...def show_usage(): print("Create/update webhooks.") print(" Usage: hooks REPO URL")
During testing, we can access the captured output, using the capsys fixture. This fixture has a capsys.readouterr() method that returns a namedtuple (https://docs.python.org/3/library/collections.html#collections.namedtuple) with out and err attributes, containing the captured text from sys.stdout and sys.stderr ...
Read now
Unlock full access