Bird
Raised Fist0
PyTesttesting~10 mins

capsys for stdout/stderr in PyTest - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to capture standard output using capsys in pytest.

PyTest
def test_print_output(capsys):
    print("Hello, world!")
    captured = capsys.[1]()
    assert captured.out == "Hello, world!\n"
Drag options to blanks, or click blank then click option'
Acapture
Breadouterr
Cgetoutput
Dcapture_output
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent method like capture() or getoutput()
Trying to access capsys.out directly without calling a method
2fill in blank
medium

Complete the code to capture standard error output using capsys in pytest.

PyTest
def test_error_output(capsys):
    import sys
    print("Error message", file=sys.stderr)
    captured = capsys.[1]()
    assert captured.err == "Error message\n"
Drag options to blanks, or click blank then click option'
Areadouterr
Bcapture
Ccapture_err
Dgeterr
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use a method that only captures stdout
Using a method name that does not exist in capsys
3fill in blank
hard

Fix the error in the code to correctly capture and assert printed output using capsys.

PyTest
def test_output(capsys):
    print("Test output")
    captured = capsys.[1]()
    assert captured.out == "Test output\n"
Drag options to blanks, or click blank then click option'
Agetoutput
Bcapture
Creadouterr
Dcapture_output
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the newline character in the assertion
Using a wrong method to capture output
4fill in blank
hard

Fill both blanks to capture output and check that stderr is empty.

PyTest
def test_stdout_only(capsys):
    print("Only stdout")
    captured = capsys.[1]()
    assert captured.out == "Only stdout\n"
    assert captured.[2] == ""
Drag options to blanks, or click blank then click option'
Areadouterr
Bcapture
Cerr
Dout
Attempts:
3 left
💡 Hint
Common Mistakes
Checking captured.out instead of captured.err for stderr
Using a non-existent method to capture output
5fill in blank
hard

Fill all three blanks to capture output, check stdout, and check stderr after printing to both.

PyTest
def test_both_outputs(capsys):
    import sys
    print("Output message")
    print("Error message", file=sys.stderr)
    captured = capsys.[1]()
    assert captured.[2] == "Output message\n"
    assert captured.[3] == "Error message\n"
Drag options to blanks, or click blank then click option'
Areadouterr
Bout
Cerr
Dcapture
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up captured.out and captured.err
Using a wrong method name to capture output

Practice

(1/5)
1. What is the main purpose of using capsys in pytest tests?
easy
A. To capture and check printed output sent to stdout and stderr
B. To speed up test execution by skipping print statements
C. To automatically fix syntax errors in print statements
D. To generate random test data for print output

Solution

  1. Step 1: Understand what capsys does

    capsys is a pytest feature that captures output sent to the console, including both stdout and stderr.
  2. Step 2: Identify its main use in tests

    It allows tests to check what was printed, so you can verify messages or errors your code produces.
  3. Final Answer:

    To capture and check printed output sent to stdout and stderr -> Option A
  4. Quick Check:

    capsys captures output = C [OK]
Hint: capsys catches print output for verification [OK]
Common Mistakes:
  • Thinking capsys speeds up tests
  • Believing capsys fixes code errors
  • Confusing capsys with test data generation
2. Which of the following is the correct way to use capsys to capture printed output in a pytest test function?
easy
A. def test_func(capsys): captured = capsys.capture(); print('Hello')
B. def test_func(): capsys = print('Hello'); captured = capsys.readouterr()
C. def test_func(capsys): print('Hello'); captured = capsys.readouterr()
D. def test_func(): captured = capsys.readouterr(); print('Hello')

Solution

  1. Step 1: Check how capsys is passed and used

    In pytest, capsys is passed as a function argument and used after printing to capture output.
  2. Step 2: Verify correct method to read output

    The method to get output is capsys.readouterr(), which returns captured stdout and stderr.
  3. Final Answer:

    def test_func(capsys): print('Hello'); captured = capsys.readouterr() -> Option C
  4. Quick Check:

    capsys as argument + readouterr() = A [OK]
Hint: Pass capsys as argument and call readouterr() after print [OK]
Common Mistakes:
  • Not passing capsys as a test function argument
  • Calling non-existent capture() method
  • Calling readouterr() before printing
3. What will be the output of the following pytest test code snippet?
def test_output(capsys):
    print('Test message')
    captured = capsys.readouterr()
    assert captured.out == 'Test message\n'
medium
A. Test passes because printed output matches assertion
B. Test fails because captured.out does not include newline
C. Test fails due to syntax error in print statement
D. Test fails because capsys.readouterr() is called before print

Solution

  1. Step 1: Understand output capture timing

    The print statement runs first, sending 'Test message' plus a newline to stdout.
  2. Step 2: Check captured output and assertion

    capsys.readouterr() captures the output including the newline. The assertion checks for 'Test message\n', which matches exactly.
  3. Final Answer:

    Test passes because printed output matches assertion -> Option A
  4. Quick Check:

    Printed output includes newline = B [OK]
Hint: Remember print adds newline; assert with '\n' [OK]
Common Mistakes:
  • Forgetting newline in assertion string
  • Calling readouterr() before print
  • Assuming captured.out excludes newlines
4. Identify the error in this pytest test using capsys:
def test_error(capsys):
    captured = capsys.readouterr()
    print('Hello')
    assert captured.out == 'Hello\n'
medium
A. Missing parentheses in print statement
B. Calling capsys.readouterr() before printing captures nothing
C. Assertion string missing newline character
D. capsys must be imported before use

Solution

  1. Step 1: Check order of output capture and print

    capsys.readouterr() is called before the print statement, so it captures output up to that point, which is empty.
  2. Step 2: Understand assertion failure reason

    The assertion expects 'Hello\n' but captured.out is empty, causing test failure.
  3. Final Answer:

    Calling capsys.readouterr() before printing captures nothing -> Option B
  4. Quick Check:

    Read output after print, not before = A [OK]
Hint: Always call readouterr() after print to capture output [OK]
Common Mistakes:
  • Calling readouterr() before print
  • Forgetting newline in assertion
  • Assuming capsys needs import
5. You want to test a function that prints error messages to stderr using pytest and capsys. Which code snippet correctly captures and asserts the stderr output?
hard
A. def test_error_message(capsys): print('Error occurred', file=sys.stdout) captured = capsys.readouterr() assert captured.err == 'Error occurred\n'
B. def test_error_message(capsys): print('Error occurred') captured = capsys.readouterr() assert captured.err == 'Error occurred\n'
C. def test_error_message(): print('Error occurred', file=sys.stderr) captured = capsys.readouterr() assert captured.err == 'Error occurred\n'
D. def test_error_message(capsys): print('Error occurred', file=sys.stderr) captured = capsys.readouterr() assert captured.err == 'Error occurred\n'

Solution

  1. Step 1: Identify how to print to stderr

    Use print(..., file=sys.stderr) to send output to stderr stream.
  2. Step 2: Check correct use of capsys and assertion

    capsys.readouterr() captures both stdout and stderr. To check stderr, assert captured.err matches expected string including newline.
  3. Final Answer:

    def test_error_message(capsys): print('Error occurred', file=sys.stderr) captured = capsys.readouterr() assert captured.err == 'Error occurred\n' -> Option D
  4. Quick Check:

    Print to stderr + assert captured.err = D [OK]
Hint: Print with file=sys.stderr and assert captured.err [OK]
Common Mistakes:
  • Not passing capsys as argument
  • Printing to stdout instead of stderr
  • Asserting captured.out instead of captured.err
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard