The capfd fixture captures output sent to the system file descriptors stdout and stderr. This means it captures output from print statements and error messages during test execution.
Click to reveal answer
beginner
How do you retrieve the captured output using capfd in a pytest test?
You call out, err = capfd.readouterr() inside your test. out contains the captured standard output, and err contains the captured standard error.
Click to reveal answer
intermediate
Why is capfd useful compared to capturing output with capsys?
capfd captures output at the file descriptor level, so it can capture output from C extensions or subprocesses, while capsys captures output at the Python sys module level only.
Click to reveal answer
intermediate
What happens if you call capfd.readouterr() multiple times in the same test?
Each call to <code>readouterr()</code> returns the output captured since the last call and clears the buffer. So multiple calls let you check output in stages.
Click to reveal answer
intermediate
Can capfd capture output from subprocesses spawned during a test?
Yes, because capfd captures output at the file descriptor level, it can capture output from subprocesses, unlike capsys.
Click to reveal answer
What does capfd capture in pytest?
AOnly logging messages
BOnly Python print statements
COutput sent to system file descriptors stdout and stderr
DOnly exceptions
✗ Incorrect
capfd captures output sent to the system file descriptors stdout and stderr, including print statements and errors.
How do you get the captured output from capfd?
AUsing <code>capfd.readouterr()</code>
BUsing <code>capfd.get_output()</code>
CUsing <code>capfd.capture()</code>
DUsing <code>capfd.output()</code>
✗ Incorrect
The method readouterr() returns a tuple with captured stdout and stderr.
Which is true about capfd compared to capsys?
A<code>capsys</code> captures output at the file descriptor level
B<code>capfd</code> only captures Python sys.stdout
C<code>capsys</code> captures subprocess output
D<code>capfd</code> captures output at the file descriptor level
✗ Incorrect
capfd captures output at the OS file descriptor level, so it can capture subprocess output.
What happens when you call capfd.readouterr() multiple times?
AIt returns output since last call and clears buffer
BIt returns the same output every time
CIt raises an error
DIt disables output capture
✗ Incorrect
Each call returns new output since last call and clears the captured output buffer.
Can capfd capture output from subprocesses?
ANo, it only captures Python output
BYes, because it captures at file descriptor level
COnly if subprocess uses Python print
DOnly if subprocess is run with special flags
✗ Incorrect
capfd captures output at the OS file descriptor level, so it captures subprocess output.
Explain how the pytest fixture capfd works and when you would use it.
Think about capturing output beyond just Python print statements.
You got /4 concepts.
Describe the difference between capfd and capsys in pytest.
Consider the level at which output is captured.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of capfd in pytest?
easy
A. To capture output printed to the console during tests
B. To mock database connections in tests
C. To measure test execution time
D. To generate test reports automatically
Solution
Step 1: Understand capfd functionality
capfd is a pytest fixture used to capture output sent to standard output (stdout) and standard error (stderr) during test execution.
Step 2: Identify correct purpose
Since capfd captures printed output, it helps verify what the code prints without changing the code itself.
Final Answer:
To capture output printed to the console during tests -> Option A
Quick Check:
capfd captures printed output = B [OK]
Hint: Remember capfd captures print output and errors [OK]
Common Mistakes:
Confusing capfd with mocking tools
Thinking capfd measures time
Assuming capfd generates reports
2. Which of the following is the correct way to use capfd in a pytest test function?
easy
A. def test_func(capsys): output = capfd.readouterr()
B. def test_func(capfd): output = capfd.readouterr()
C. def test_func(): output = capfd.readouterr()
D. def test_func(capfd): output = capfd.capture()
Solution
Step 1: Check pytest fixture usage
To use capfd, it must be a parameter in the test function so pytest injects it.
Step 2: Verify method call
The correct method to get captured output is readouterr(), not capture().
Final Answer:
def test_func(capfd): output = capfd.readouterr() -> Option B
Quick Check:
Use capfd parameter and readouterr() method = D [OK]
Hint: Use capfd as function argument and call readouterr() [OK]
Common Mistakes:
Using capsys instead of capfd
Calling non-existent capture() method
Not passing capfd as parameter
3. Given the following test code, what will be the value of captured.out after capfd.readouterr() is called?