0

I'm trying to monkeypatch in pytest the input function to simulate user input but I'm getting an attribute error.

I receive the same error when I use the mock.patch.object as well. But I'm able to readily monkeypatch the input when I'm in a regular Python environment, I only get this error in testing.enter image description here

What could be causing this issue?

Edit Adding additional screenshot trying same thing using unittest.mock Unittestmockerror

1
  • Please do not post screenshots but your actual code. Commented Jul 14, 2016 at 19:26

1 Answer 1

3

__builtins__ is an implementation detail. You shouldn't touch it. What you're looking for is either the __builtin__ (no s) or builtins module, depending on whether you're on Python 2 or 3.

Judging by the details of the error you got, you're on Python 3, so you want builtins.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm on Python3 but I need to replace the input() function with one that returns a fake input. Trying to this without using a decorator or mock module stackoverflow.com/questions/18161330/…
@canyon289 I'm confused. You provided a link that has two answers using a decorator and uses mock. And if you are looking for input to return a fake input, you are mocking input's functionality. So, I'm not sure what it is you are trying to do exactly.
One parts of my program takes a user input and returns a value. I'm trying to test using pytest and part of that test is having the input function return a value. To automate that test I'm trying to monkeypatch/mock the regular input function with one that will return a string set in the test. I get this same error whether I use the mock module or the pytest monkeypatch function

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.