capsys helps you catch what your program prints so you can check if it shows the right messages.
capsys for capturing output in PyTest
Introduction
Syntax
PyTest
def test_function(capsys): print("Hello") captured = capsys.readouterr() assert captured.out == "Hello\n"
capsys is a special pytest argument that captures print output.
Use capsys.readouterr() to get the captured output as an object with out and err strings.
Examples
PyTest
def test_print_message(capsys): print("Test message") captured = capsys.readouterr() assert captured.out == "Test message\n"
PyTest
def test_multiple_prints(capsys): print("Line 1") print("Line 2") captured = capsys.readouterr() assert captured.out == "Line 1\nLine 2\n"
PyTest
def test_error_output(capsys): import sys print("Error happened", file=sys.stderr) captured = capsys.readouterr() assert captured.err == "Error happened\n"
Sample Program
This test calls greet which prints a message. capsys captures the output and the test checks if it is correct.
PyTest
def greet(name): print(f"Hello, {name}!") def test_greet(capsys): greet("Alice") captured = capsys.readouterr() assert captured.out == "Hello, Alice!\n" # Running test_greet will pass if output matches
Important Notes
capsys captures output only during the test function where it is used.
After calling capsys.readouterr(), the captured output is cleared.
Use captured.out for standard output and captured.err for error output.
Summary
capsys lets you catch and check printed messages in tests.
Use capsys.readouterr() to get output and error streams.
This helps verify your program talks to users correctly.
Practice
1. What is the main purpose of using
capsys in pytest tests?easy
Solution
Step 1: Understand capsys role in pytest
capsys is a pytest feature that captures output sent to stdout and stderr during tests.Step 2: Identify what capsys helps verify
It helps verify printed messages, ensuring the program outputs expected text.Final Answer:
To capture and check printed output during test execution -> Option DQuick Check:
capsys captures output = A [OK]
Hint: Remember: capsys catches print output for checking [OK]
Common Mistakes:
- Thinking capsys speeds up tests
- Believing capsys fixes code errors
- Confusing capsys with report generation
2. Which of the following is the correct way to capture output using capsys in a pytest test function?
easy
Solution
Step 1: Recall capsys method to get output
The correct method to retrieve captured output iscapsys.readouterr().Step 2: Check other options for correctness
Other methods likecapture(),getoutput(), orprint()do not exist on capsys.Final Answer:
output = capsys.readouterr() -> Option AQuick Check:
capsys.readouterr() reads output = B [OK]
Hint: Use readouterr() to get captured stdout and stderr [OK]
Common Mistakes:
- Using non-existent capsys methods
- Trying to capture output without readouterr()
- Confusing capsys with other pytest fixtures
3. What will be the output of the following pytest test using capsys?
def greet():
print('Hello, Tester!')
def test_greet(capsys):
greet()
captured = capsys.readouterr()
assert captured.out == 'Hello, Tester!\n'medium
Solution
Step 1: Understand what greet() prints
The function prints 'Hello, Tester!' followed by a newline.Step 2: Check assertion against captured output
capsys.readouterr() captures stdout including newline, so captured.out equals 'Hello, Tester!\n'.Final Answer:
Test passes because output matches exactly -> Option AQuick Check:
captured.out == 'Hello, Tester!\n' = C [OK]
Hint: Remember print adds newline, so include \n in assertion [OK]
Common Mistakes:
- Forgetting newline in expected output
- Assuming captured.out excludes newlines
- Misusing capsys.readouterr() causing errors
4. Identify the error in this pytest test using capsys:
def test_error(capsys):
print('Start')
captured = capsys.readouterr
assert captured.out == 'Start\n'medium
Solution
Step 1: Check how capsys.readouterr is used
capsys.readouterr is a method and must be called with parentheses: capsys.readouterr()Step 2: Verify assertion and other code parts
The assertion correctly expects 'Start\n', print is inside the test function, and capsys is passed as argument.Final Answer:
Missing parentheses when calling capsys.readouterr -> Option CQuick Check:
Method call needs () = D [OK]
Hint: Always call readouterr() with parentheses [OK]
Common Mistakes:
- Forgetting () after readouterr
- Ignoring newline in output assertion
- Not passing capsys fixture to test
5. You want to test a function that prints multiple lines and errors. How do you use capsys to verify both stdout and stderr outputs correctly?
hard
Solution
Step 1: Understand capsys.readouterr() output
It returns a namedtuple with two attributes: out (stdout) and err (stderr).Step 2: Verify how to check both outputs
Calling readouterr() once captures both streams; check captured.out and captured.err separately.Final Answer:
Call capsys.readouterr() once and check both captured.out and captured.err -> Option BQuick Check:
One readouterr() call captures both streams = A [OK]
Hint: One readouterr() call captures stdout and stderr together [OK]
Common Mistakes:
- Calling readouterr() multiple times expecting separate streams
- Ignoring stderr output in tests
- Using print instead of capsys for output verification
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
