4

I am aware that one can disable stdout and stderr capturing in pytest by using the -s / --capture=no switch. Alternatively, one can use the capsys fixture and its disable method when testing a particular function:

def test_disabling_capturing(capsys):
    print("this output is captured")
    with capsys.disabled():
        foo():
    print("this output is also captured")

def foo():
    print("output not captured, going directly to sys.stdout")

However, the capturing is disabled for the entire function foo(). What if I wanted to disable capturing for only particular lines of foo() (for the sake of readability of the output stream when the test is running)?

I know it's possible to pass the capsys fixture as an argument through the function being tested (and also through any nested functions), but I would like to know if there is a better way that doesn't modify the structure of the function(s).

Florent H
  • 323
  • 1
  • 8

0 Answers0