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
Adding an autouse fixture to close plots after each test #1628
Conversation
Can we have a |
fde1c58
to
79198a2
Compare
Good suggestion, I just moved everything over to the mpl-specific directory so that it only gets called there and not in every test. What other testing reset do you think we need? Do you want to run each test in an |
79198a2
to
e2af009
Compare
Just realized the actual MPL fixture is not private. I was looking at |
I'm not sure how I feel about this:
@QuLogic ? |
That is one of the downsides of force-pushing over things with multiple implementations. Sorry about that, Ryan. The previous iteration I had in here was simply: @pytest.fixture(autouse=True)
def close_plots(request):
try:
yield
finally:
plt.close("all") Actually, it looks like using MPLs version doesn't work on Python 3.5 for us either, so maybe the previous iteration is the better route anyways. |
e2af009
to
cc846d4
Compare
I just undid the "simple" version of grabbing MPLs test fixture. I put the |
This takes care of all the cleanup of figures and state for each test.
cc846d4
to
d2f3567
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works for me. It's straightforward. The Agg switching is something I've done on MetPy as well (in a different kind of global fixture.)
yield | ||
finally: | ||
# Closes all open figures and switches backend back to original | ||
plt.switch_backend(orig_backend) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no idea switch_backend
closed all the figures, but that makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me either until I went through MPLs cleanup decorator. Everyones learning something today :)
try: | ||
# Run each test in a context manager so that state does not leak out | ||
orig_backend = plt.get_backend() | ||
plt.switch_backend('Agg') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me.
Automatically closing figures after each test. Implemented the suggestion from #1625 (comment)
Rationale
Will automatically close figures after each test to not reuse the same figures.
Implications