-
Notifications
You must be signed in to change notification settings - Fork 47
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
Saving multiple figures in one test #133
Comments
This plugin can only compare one figure per test. However, you should be able to use Your solution is another common way to solve this, as you can define a function such as |
Thanks for the reply. I am trying something like the following `
` And, I see that savefig call saves all the images. But, when I run
there is no image captured. I can see that the 3 tests are picked by pytest ` platform win32 -- Python 3.10.2, pytest-7.0.0, pluggy-1.0.0
` If I move the method test_(first/second/third)_plot outside the class the baseline images are captured. Sorry, to deviate from the topic of the issue but I am trying to get this working for the first time. |
The figures may not be available in the tests due to this: pytest-mpl/pytest_mpl/plugin.py Lines 617 to 622 in e1bf8dd
Not 100% sure as I don't often use test classes. Does seem like a bug though. Instead of the |
Thanks for the reply. This is a bug indeed. For my immediate need, I am using matplotlib.test now. There is another bug(or maybe intended feature) that all the figures are closed that does not allow me to use cached figures across tests. Matplotlib.tests allows specifying multiple images to compare against hence using that for now. |
You're welcome. The core Matplotlib decorator is probably easiest if a single method/function produces multiple figures. This plugin is designed for one figure returned per test function, which makes reporting the results and generating a HTML report easier. The closing of figures is intended for convenient memory management, although in theory there could be an option to disable this if needed. For future reference, I just had a quick look to see what the issue was. Looks like it's caused by how figure numbers are scoped in Matplotlib. It may be because the active figure manager when class TestPCA:
@classmethod
def setup_class(cls):
pca.run_pca_on_csv("./myfile.csv")
cls.figs = [plt.figure(i) for i in (1, 2, 3)]
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images')
def test_first_plot(self):
return self.figs[0]
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images')
def test_second_plot(self):
return self.figs[1]
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images')
def test_third_plot(self):
return self.figs[2] |
I need to test multiple figures that are generated by a method for correctness. Using @pytest.mark.mpl_image_compare does not enable me to do that as it does not save multiple figures. Any way to do that?
Right now my thought is having dummy tests that return these figures individually from a shared variable and validate it one by one in different tests
The text was updated successfully, but these errors were encountered: