0
0
PyTesttesting~20 mins

capsys for stdout/stderr in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Capsys Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output captured by capsys?
Consider the following pytest test function using capsys to capture output. What will be the value of captured.out after the print statements?
PyTest
def test_capture_output(capsys):
    print("Hello")
    print("World", end="!")
    captured = capsys.readouterr()
    print("Again")
    return captured.out
A"Hello\nWorld!"
B"Hello\nWorld!\nAgain\n"
C"HelloWorld!\n"
D"Hello\nWorld!\n"
Attempts:
2 left
💡 Hint
capsys.readouterr() captures output up to the point it is called.
assertion
intermediate
2:00remaining
Which assertion correctly checks stderr output captured by capsys?
Given the following test code, which assertion will correctly verify that the error message 'Error occurred' was printed to stderr?
PyTest
def test_error_message(capsys):
    import sys
    print('Error occurred', file=sys.stderr)
    captured = capsys.readouterr()
Aassert 'Error occurred' in captured.out
Bassert captured.out == 'Error occurred\n'
Cassert captured.err == ''
Dassert 'Error occurred' in captured.err
Attempts:
2 left
💡 Hint
stderr output is captured in captured.err, not captured.out.
🔧 Debug
advanced
2:00remaining
Why does capsys.readouterr() not capture output after the test ends?
In the following test, the last print statement is not captured by capsys. Why does this happen?
PyTest
def test_late_print(capsys):
    print('Start')
    captured = capsys.readouterr()
    print('End')
    assert captured.out == 'Start\n'
Acapsys only captures output printed before readouterr() is called
Bcapsys captures output only from stderr
Cprint statements after assertions are ignored by pytest
Dcapsys captures all output during the entire test function
Attempts:
2 left
💡 Hint
Think about when capsys.readouterr() captures output.
framework
advanced
2:00remaining
How to capture output multiple times in one test using capsys?
You want to capture output twice in a single test function using capsys. Which code snippet correctly captures and asserts both outputs separately?
A
captured1 = capsys.readouterr()
print('First')
captured2 = capsys.readouterr()
print('Second')
assert captured1.out == ''
assert captured2.out == 'First\n'
B
print('First')
print('Second')
captured1 = capsys.readouterr()
captured2 = capsys.readouterr()
assert captured1.out == 'First\nSecond\n'
assert captured2.out == ''
C
print('First')
captured1 = capsys.readouterr()
print('Second')
captured2 = capsys.readouterr()
assert captured1.out == 'First\n'
assert captured2.out == 'Second\n'
D
print('First')
captured1 = capsys.readouterr()
print('Second')
captured2 = capsys.readouterr()
assert captured1.out == 'First'
assert captured2.out == 'Second'
Attempts:
2 left
💡 Hint
Call capsys.readouterr() after each print to capture output separately.
🧠 Conceptual
expert
2:00remaining
What happens if you do not call capsys.readouterr() in a pytest test?
In a pytest test function using capsys, if you print output but never call capsys.readouterr(), what is the effect on the test output and captured output?
AThe test raises an error because capsys.readouterr() was not called
BThe printed output is captured silently and not shown unless the test fails or -s is used
CThe printed output goes to the console normally and is not captured by pytest
DThe printed output is lost and not shown anywhere
Attempts:
2 left
💡 Hint
Think about pytest's default behavior with output capture.