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

Using Patcher with py.test #135

Closed
mirosval opened this issue Nov 17, 2016 · 4 comments
Closed

Using Patcher with py.test #135

mirosval opened this issue Nov 17, 2016 · 4 comments

Comments

@mirosval
Copy link

I have run into an issue using Patcher with py.test.

The issue is that when the code under test throws, the exception bubbles up to py.test, which will then fail processing it, because os.path is patched.

This then results into a horrible mess of tracebacks and exceptions during handling of other exceptions and the original exception is lost, so it makes debugging the underlying problem difficult.

Example:

patcher = Patcher()
patcher.setUp()
foo()  # throws
patcher.tearDown()

I fixed this by wrapping Patcher in a context manager like so:

class ManagedPatcher(Patcher):
        def __enter__(self):
            self.setUp()

        def __exit__(self, exc_type, exc_val, exc_tb):
            self.tearDown()

with ManagedPatcher():
    foo()

This ensures that when an exception is thrown, the patch is unapplied on exit from the context block and the exception is allowed to correctly bubble up and be caught by py.test.

I would suggest adding the two context methods to the original Patcher. I could make a PR for this if you like.

@mrbean-bremen
Copy link
Member

Thanks - this sounds good! A context manager for Patcher certainly makes sense in this context. I may add this tomorrow - as it's a small addition, I think a PR is not really needed.

@mirosval
Copy link
Author

Oh fantastic, I was hoping you'd say that. ;)

On Thu, Nov 17, 2016 at 9:53 PM mrbean-bremen [email protected]
wrote:

Thanks - this sounds good! A context manager for Patcher certainly makes
sense in this context. I may add this tomorrow - as it's a small addition,
I think a PR is not really needed.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#135 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABQSWXgSgaROs_5RN0DFpzmrZvCgonO6ks5q_L6-gaJpZM4K1imx
.

Best Regards,
Miroslav Zoricak

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Nov 18, 2016
mrbean-bremen added a commit that referenced this issue Nov 18, 2016
@mrbean-bremen
Copy link
Member

Neither I nor @jmcgeheeiv have much experience with pytest, so we are always glad to get input from pytest users. Thanks for the contribution!
Please check if this is ok for you and close the issue in this case.

@mirosval
Copy link
Author

Yes, this is exactly what I did on my side. Perfect! Thank you so much!

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

No branches or pull requests

2 participants