Why is Pytest in an infinite loop when mocking `builtins.input`?

Viewed 121

I want to test a function which calls builtins.input, so I have monkeypatched it. However, this seems to give an error which seems to repeat forever. What is going on?

Here is a test which reproduces the error:

import pytest

class TestA:

    def test_a(self, monkeypatch):
        monkeypatch.setattr('builtins.input', lambda *a, **k: 'no')
        assert 0

And I want to run pytest like this: pytest --pdb tests/tmp.py

The error is this:

...
*** NameError: name 'no' is not defined
*** NameError: name 'no' is not defined
*** NameError: name 'no' is not defined
*** NameError: name 'no' is not defined
...

Continuing forever.

Note that if I don't use the --pdb flag, it seems to work normally. But I want to use the --pdb flag as I find it useful for debugging.

0 Answers
Related