Complete the code to capture printed output using capsys.
def test_print(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 code to assert the captured error output using capsys.
def test_error_output(capsys): import sys print("Error happened", file=sys.stderr) captured = capsys.[1]() assert captured.err == "Error happened\n"
The readouterr() method captures both stdout and stderr output, accessible via captured.out and captured.err.
Fix the error in the code to properly capture output with capsys.
def test_fix(capsys): print("Test output") captured = capsys.[1] assert captured.out == "Test output\n"
readouterr.The readouterr() method must be called with parentheses to capture the output. Without parentheses, it is just a method reference.
Fill both blanks to capture output and assert it correctly.
def test_multiple(capsys): print("Line 1") print("Line 2") captured = capsys.[1]() assert captured.[2] == "Line 1\nLine 2\n"
captured.err when output is printed to stdout.Use readouterr() to capture output, then access captured.out for standard output.
Fill all three blanks to capture output, split lines, and assert the first line.
def test_split_lines(capsys): print("First line") print("Second line") captured = capsys.[1]() lines = captured.[2].split([3]) assert lines[0] == "First line"
captured.err instead of captured.out.Use readouterr() to capture output, access captured.out, then split by newline "\n" to get lines.