Challenge - 5 Problems
Capsys Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
capsys.readouterr() captures output up to the point it is called.
✗ Incorrect
capsys.readouterr() captures all output printed before it is called. The first two print statements produce 'Hello\n' and 'World!' without a newline after 'World!'. So captured.out is 'Hello\nWorld!'. The last print("Again") happens after capture, so it is not included.
❓ assertion
intermediate2: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()
Attempts:
2 left
💡 Hint
stderr output is captured in captured.err, not captured.out.
✗ Incorrect
Output printed to sys.stderr is captured in captured.err. So to check for 'Error occurred' in stderr, the assertion must check captured.err.
🔧 Debug
advanced2: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'
Attempts:
2 left
💡 Hint
Think about when capsys.readouterr() captures output.
✗ Incorrect
capsys.readouterr() captures output printed up to the moment it is called. Output printed after that is not included in the captured object.
❓ framework
advanced2: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?
Attempts:
2 left
💡 Hint
Call capsys.readouterr() after each print to capture output separately.
✗ Incorrect
Calling capsys.readouterr() clears the captured output buffer. So capturing after each print separately allows checking outputs independently.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about pytest's default behavior with output capture.
✗ Incorrect
Pytest captures stdout and stderr by default during tests. If capsys.readouterr() is not called, output is captured silently and only shown on failure or with -s option.