0
0
PyTesttesting~20 mins

capfd for file descriptors in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Capfd 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 capfd?
Consider this pytest test function using capfd to capture output from print statements. What will be the value of captured output after running this test?
PyTest
def test_capture_output(capfd):
    print('Hello')
    print('World')
    out, err = capfd.readouterr()
    assert out == 'Hello\nWorld\n'
    assert err == ''
A''
B'Hello World\n'
C'HelloWorld'
D'Hello\nWorld\n'
Attempts:
2 left
💡 Hint
Remember that print adds a newline after each call.
assertion
intermediate
2:00remaining
Which assertion correctly checks stderr captured by capfd?
Given a function that writes an error message to stderr, which assertion correctly verifies the captured error output using capfd?
PyTest
def test_error_message(capfd):
    import sys
    print('Error occurred', file=sys.stderr)
    out, err = capfd.readouterr()
Aassert err == 'Error occurred'
Bassert err == 'Error occurred\n'
Cassert out == 'Error occurred\n'
Dassert out == ''
Attempts:
2 left
💡 Hint
Error messages printed to stderr are captured in err.
🔧 Debug
advanced
2:00remaining
Why does capfd.readouterr() return empty strings after first call?
In this test, capfd.readouterr() is called twice. Why does the second call return empty strings?
PyTest
def test_double_read(capfd):
    print('Test')
    out1, err1 = capfd.readouterr()
    out2, err2 = capfd.readouterr()
    assert out1 == 'Test\n'
    assert out2 == ''
ABecause readouterr() clears the captured output after reading
BBecause print only outputs once and capfd caches it
CBecause capfd only captures stderr, not stdout
DBecause the second print statement is missing
Attempts:
2 left
💡 Hint
Think about what happens to the buffer after reading.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between capfd and capsys in pytest?
Choose the correct statement about the difference between capfd and capsys fixtures in pytest.
Acapfd captures output only from Python print, capsys captures all system output
Bcapfd captures only stdout, capsys captures both stdout and stderr
Ccapfd captures output at the file descriptor level, capsys captures at the Python sys module level
Dcapfd is slower than capsys because it uses subprocesses
Attempts:
2 left
💡 Hint
Think about how low-level the capture happens.
framework
expert
3:00remaining
How to test a function that writes to both stdout and stderr using capfd?
You have a function that prints 'Info' to stdout and 'Warning' to stderr. Which test code correctly asserts both outputs using capfd?
PyTest
def log_messages():
    import sys
    print('Info')
    print('Warning', file=sys.stderr)
A
def test_log(capfd):
    log_messages()
    out, err = capfd.readouterr()
    assert out == 'Info\n'
    assert err == 'Warning\n'
B
def test_log(capfd):
    log_messages()
    out, err = capfd.readouterr()
    assert out == 'Warning\n'
    assert err == 'Info\n'
C
def test_log(capfd):
    log_messages()
    out, err = capfd.readouterr()
    assert out == 'InfoWarning\n'
    assert err == ''
D
def test_log(capfd):
    log_messages()
    out, err = capfd.readouterr()
    assert out == ''
    assert err == 'Info\nWarning\n'
Attempts:
2 left
💡 Hint
Remember which stream each print writes to.