Complete the code to capture printed output in a pytest test function.
def test_print_output(capsys): print('Hello, world!') captured = capsys.[1]() assert captured.out == 'Hello, world!\n'
The capsys.readouterr() method captures the output printed to stdout and stderr during the test.
Complete the assertion to check that the captured output contains the expected substring.
def test_partial_output(capsys): print('Test output for pytest') captured = capsys.readouterr() assert [1] in captured.out
We check that the string 'pytest' is part of the captured output.
Fix the error in the test code to correctly capture and assert printed output.
def test_error_output(capsys): print('Error occurred') captured = capsys.[1]() assert captured.err == ''
The correct method to capture output is readouterr(). It returns both stdout and stderr.
Fill both blanks to capture output and assert it matches the expected string.
def test_output_match(capsys): print('Success!') captured = capsys.[1]() assert captured.[2] == 'Success!\n'
captured.err instead of captured.outUse readouterr() to capture output and check captured.out for stdout content.
Fill all three blanks to capture output, strip whitespace, and assert the exact message.
def test_strip_output(capsys): print(' Trim me ') captured = capsys.[1]() output = captured.[2].[3]() assert output == 'Trim me'
rstrip() which only removes trailing spacesWe capture output with readouterr(), access out, then use strip() to remove spaces.