2

(This is meant as a much more specific version of How can I make py.test tests accept interactive input?).

I'm using py.test, and I'm trying to make stdin and stdout work as normally -- i.e. print() should immediately print to the terminal, and input() (this is Python 3.5) should accept a line of input interactively.

(I am trying to make my test cases do this, I know this isn't normal for unit testing, I am not trying to have my testcases test something else that uses stdin and stdout).

Running pytest with the -s flag does pretty much what I want, but I would rather have this as an option set within my test modules or individual test functions. Especially as I only want to use it on some tests. capsys.disabled() as context manager doesn't cut it -- it only deals with stdout and stderr, not stdin.

Is there a way to do this, either a pytest option to call via marker/decorator/context manager, or a way to "re-connect" sys.stdin/stdout within my module?

Thank you.

1
  • 1
    I've communicated with the developers. The inability to do this may actually be a bug in py.test. Commented Jan 24, 2017 at 22:01

1 Answer 1

-2

http://doc.pytest.org/en/latest/capture.html provides how to disable the stdout capturing:

def test_disabling_capturing(capsys):
    print('this output is captured')
    with capsys.disabled():
        print('output not captured, going directly to sys.stdout')
    print('this output is also captured')
Sign up to request clarification or add additional context in comments.

4 Comments

This handles stdout just fine. However, it doesn't help with stdin. So print() works, but input() doesn't.
You a right, somehow I missed the input part. I actually doubt there is a solution, as to me this seems to contradict the concept of unittests. They should ensure a standard which is tested. Maybe you just want to mock the input function, and provide a fixed input?
Not in this case. But maybe I might be able to somehow mock the input function, or stdin. back into being actual stdin.
@DerWeh when mocking input, pytest raises OSError anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.