Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with testdata.patch() context manager #50

Open
Jaymon opened this issue Jun 26, 2019 · 2 comments
Open

with testdata.patch() context manager #50

Jaymon opened this issue Jun 26, 2019 · 2 comments

Comments

@Jaymon
Copy link
Owner

Jaymon commented Jun 26, 2019

it would be cool to allow a context manager through patch so you can easily patch something and then restore the patched code to the original at the end

Likewise, it would be cool if there was an easy way for patch to handle the builtins, like if you specify something for the module and it isn't there then you check builtins

@Jaymon
Copy link
Owner Author

Jaymon commented Jun 26, 2019

I just had to do something like this:

input_name = "raw_input" if is_py2 else "input"
orig_input = getattr(builtins, input_name)

class MockInput(object):
    def __init__(self, v):
        self.v = v
    def __call__(self, *args, **kwargs):
        return self.v


 setattr(builtins, input_name, MockInput("Yes"))

# restore patched
setattr(builtins, input_name, orig_input)

because testdata.patch isn't flexible enough

@Jaymon
Copy link
Owner Author

Jaymon commented Jul 1, 2019

python mocking raw input in unittests

search:
python test prompt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant