0
0
PyTesttesting~20 mins

capsys for capturing output 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 this pytest test function using capsys to capture printed output. What will be the value of captured.out after the test runs?
PyTest
def test_print_output(capsys):
    print('Hello, world!')
    captured = capsys.readouterr()
    assert captured.out == 'Hello, world!\n'
A'Hello, world!\n'
B'Hello, world!'
C''
D'hello, world!\n'
Attempts:
2 left
💡 Hint
Remember that print adds a newline character at the end.
assertion
intermediate
2:00remaining
Which assertion correctly checks captured stderr output?
Given this test code snippet, which assertion correctly verifies that the error message 'Error occurred' was printed to standard error?
PyTest
def test_error_output(capsys):
    import sys
    print('Error occurred', file=sys.stderr)
    captured = capsys.readouterr()
Aassert captured.err == 'Error occurred\n'
Bassert captured.out == 'Error occurred\n'
Cassert 'Error occurred' in captured.out
Dassert captured.err == 'error occurred\n'
Attempts:
2 left
💡 Hint
Standard error output is captured in captured.err, not captured.out.
🔧 Debug
advanced
2:00remaining
Why does this test fail to capture output?
This test tries to capture printed output but fails. What is the reason?
PyTest
def test_no_capture(capsys):
    captured = capsys.readouterr()
    print('Test output')
    assert captured.out == 'Test output\n'
Acapsys.readouterr() clears output after reading, so print is lost
Bprint does not output anything to capture
Ccaptured is read before print, so it captures nothing
Dassertion syntax is incorrect
Attempts:
2 left
💡 Hint
Think about when capsys.readouterr() is called relative to print.
framework
advanced
2:00remaining
How to capture output from a function called multiple times?
You want to test a function that prints output multiple times. How do you capture all output using capsys?
PyTest
def greet():
    print('Hello')
    print('World')

def test_greet(capsys):
    greet()
    captured = capsys.readouterr()
ACall capsys.readouterr() before greet() to capture output
BUse capsys.readouterr() only once before test starts
CCall capsys.readouterr() after each print inside greet()
DCall capsys.readouterr() after all prints to capture combined output
Attempts:
2 left
💡 Hint
capsys accumulates output until readouterr() is called.
🧠 Conceptual
expert
2:00remaining
What happens if you call capsys.readouterr() multiple times?
In a pytest test, what is the effect of calling capsys.readouterr() multiple times during the test?
AOnly the first call returns output; others return empty strings
BEach call returns output since last call and clears the buffer
CCalling multiple times causes an error
DEach call returns all output from test start without clearing
Attempts:
2 left
💡 Hint
Think of capsys.readouterr() like reading and emptying a mailbox.