While running a particular unittest
with pytest
, it occasionally fails with this error (mentioned in the title) and from the stack trace it happens on the line
choice = input().lower()
when the control reaches this statement, the entire function is:
def prompt_to_activate(bear, printer):
PROMPT_TO_ACTIVATE_STR = ('program has found {} to be useful '
'based of dependencies discovered from your '
'project files. \n Would you like to activate '
'it? (y/n)')
printer.print(PROMPT_TO_ACTIVATE_STR)
choice = input().lower()
if choice.startswith('y'):
return True
elif choice.startswith('n'):
return False
else:
return prompt_to_activate(bear, printer)
for i in range(0, 3):
a = i
print(a)
I tried adding some time.sleep(x)
before that statement but that wouldn't fix it. Can somebody tell me the exact reason why this is happening and how to fix it?
There are three ways to read data from stdin in Python. 1. Using sys.stdin to read from standard input Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input () function. The input string is appended with a newline character ( ) in the end. So, you can use the rstrip () function to remove it.
There are three ways in which pytest can perform capturing: fd (file descriptor) level capturing (default): All writes going to the operating system file descriptors 1 and 2 will be captured. sys level capturing: Only writes to Python files sys.stdout and sys.stderr will be captured. No capturing of writes to filedescriptors is performed.
OSError is a built-in exception in Python and serves as the error class for the os module, which is raised when an os specific system function returns a system-related error, including I/O failures such as “file not found” or “disk full”. Below is an example of OSError: Attention geek!
Automated tests shouldn't require user input that interrupts the test run; use command line arguments or environment variables for passing user input. Show activity on this post. try to extract the functions into a separate file, call them in the file and run the pytest with the -s flag like this pytest FILENAME -s
In case someone else stumbles upon this, this error will also be raised if you forgot a pdb breakpoint (import ipdb; ipdb.set_trace()
) in your code.
Since input()
is an interactive function, you'll want to mock out the return value in your automated tests. Something like this:
def test_prompt(capsys, monkeypatch):
monkeypatch.setattr('path.to.yourmodule.input', lambda: 'no')
val = prompt_to_activate(bear=..., printer=...)
assert not val
You might also receive this if you set a breakpoint, but didn't use the -s
flag with pytest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With