0
0
PyTesttesting~10 mins

capsys for capturing output in PyTest - Interactive Code Practice

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

Complete the code to capture printed output using capsys.

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

Complete the code to assert the captured error output using capsys.

PyTest
def test_error_output(capsys):
    import sys
    print("Error happened", file=sys.stderr)
    captured = capsys.[1]()
    assert captured.err == "Error happened\n"
Drag options to blanks, or click blank then click option'
Acapture_error
Bcapture
Cgeterr
Dreadouterr
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use a method that only captures stdout.
Using a non-existent method like 'geterr'.
3fill in blank
hard

Fix the error in the code to properly capture output with capsys.

PyTest
def test_fix(capsys):
    print("Test output")
    captured = capsys.[1]
    assert captured.out == "Test output\n"
Drag options to blanks, or click blank then click option'
Areadouterr()
Breadouterr
Ccapture()
Dcapture
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after readouterr.
Using a non-existent method.
4fill in blank
hard

Fill both blanks to capture output and assert it correctly.

PyTest
def test_multiple(capsys):
    print("Line 1")
    print("Line 2")
    captured = capsys.[1]()
    assert captured.[2] == "Line 1\nLine 2\n"
Drag options to blanks, or click blank then click option'
Areadouterr
Bout
Cerr
Dcapture
Attempts:
3 left
💡 Hint
Common Mistakes
Using captured.err when output is printed to stdout.
Not calling the capture method.
5fill in blank
hard

Fill all three blanks to capture output, split lines, and assert the first line.

PyTest
def test_split_lines(capsys):
    print("First line")
    print("Second line")
    captured = capsys.[1]()
    lines = captured.[2].split([3])
    assert lines[0] == "First line"
Drag options to blanks, or click blank then click option'
Areadouterr
Bout
C"\n"
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using captured.err instead of captured.out.
Forgetting to call the capture method.
Splitting lines with wrong delimiter.