Package: pytest-plt
pytest-pltpytest-pltprovides fixtures for
quickly creating Matplotlib plots in your tests.Create PDF plots in one line with thepltfixture.deftest_rectification(plt):values=list(range(-10,11))rectified=[vifv>0else0forvinvalues]assertall(v>=0forvinrectified)plt.plot(values,label="Original")plt.plot(rectified,label="Rectified")plt.legend()To use these fixtures, install withpipinstallpytest-pltAnd pass the--plotsoptionpytest--plotsUsageThepltfixture allows you to create PDF plots with as little as one line.
It exposes thematplotlib.pyplotinterface.When running your tests,
pass the--plotsoption (with optional directory name)
to generate the plots
(in this case, we save them to the “my_plots” directory):pytest--plotsmy_plotsIf no directory name is provided,
plots will be saved to the “plots” directory:pytest--plotsIf you do not pass the--plotsoption,
no Matplotlib commands will be executed,
speeding up test execution.Custom filenames and extensionspytest-pltattempts to give each plot
a readable name without being too long.
Sometimes the default name is not good enough,
sopltallows you to change it usingplt.saveas:deftest_rectification(plt):values=list(range(-10,11))rectified=[vifv>0else0forvinvalues]assertall(v>=0forvinrectified)plt.plot(values,label="Original")plt.plot(rectified,label="Rectified")plt.legend()plt.saveas="test_rec.png"Theplt.saveasattribute contains the
filename that will appear in the plots directory.
You can modify this attribute within your test
to change the filename that will be used
to save the plot for a given test function.
In the above example, running pytest withpytest--plotsmy_plotswill result in
amy_plots/test_rec.pngfile.It should be noted that the file extension
inplt.saveaswill be used when saving the plot.
That is, in the example above,
the resulting plot will be a true PNG file,
not a PDF with an incorrect.pngextension.
You can use the following snippet to change
the file extension in a specific test
if the PDF format is unsuitable.plt.saveas="%s.png"%(plt.saveas[:-4],)Using plt.showIf you want to inspect a figure using
the GUI that pops up withplt.show(),
you can achieve this by saving a plot with the.pklor.pickleextension.
pytest-plt will pickle the current figure object
for later inspection.Building on the previous example,
you can change thesaveasattribute like so:deftest_rectification(plt):values=list(range(-10,11))rectified=[vifv>0else0forvinvalues]assertall(v>=0forvinrectified)plt.plot(values,label="Original")plt.plot(rectified,label="Rectified")plt.legend()plt.saveas="test_rec.pkl"Then use the following snippet to inspect the figure.>>>importpickle>>>importmatplotlib.pyplotasplt>>>withopen("path/to/test_rec.pkl","rb")asfh:...fig=pickle.load(fh)>>>plt.show()ConfigurationThe following configuration options exist.plt_filename_dropplt_filename_dropaccepts a list of regular expressions
for parts of the filename to drop.By default, plot filenames contain the fullnodeidfor the test in question,
with directory separators (/) replaced with dots (.).
If all tests reside in the same project directory,
that name will appear at the start of all plot filenames,
making them unnecessarily long.In this case, we use the carat^to ensure that
our regex matches the start of the filename only,
and we remove the trailing dot as well (\.):plt_filename_drop=^project\.If your tests always reside in a directory with a particular name
(e.g. “tests”),
you can safely remove this name wherever it occurs.
In this case, we do not use the carat to allow the regex to match anywhere
in the filename.
Be careful, as this will match any directory
that ends with “tests” (e.g. “other_tests”),
and will remove the ends of these directory names.plt_filename_drop=^project\.tests\.When usingplt_filename_drop, take care to avoid collisions
(situations where plots from two different tests
will end up with the same name).
In this case, the plots of later tests
will override those of earlier tests with the same name.plt_dirnameplt_dirnamechanges the default directory name for output plots.The defaultplt_dirnameis"plots".
To change it to"test_plots", for example, add the following
to yourpytest.ini.plt_dirname=test_plotsA directory provided at the command line with the--plotsflag
takes priority overplt_dirname.See the fulldocumentationfor more details and configuration options.Release History1.1.1 (January 15, 2024)ChangedPython 3.5, which is past its end of life, is no longer supported. (#26)FixedConfig options are now properly added to Pytest, eliminating a warning
that was raised if a config option was set. (#26)Handled multiple outputs when mocking outplt.subplots. (#29)1.1.0 (August 17, 2020)AddedAdded the ability to save plots as pickle files using the.pklor.pickleextensions withplt.saveas. (#23)1.0.1 (October 28, 2019)FixedWe now use Windows-compatible plot filenames by default.
Colons in plot filenames are replaced with hyphens.
Filenames specified throughplt.saveasare not modified.
(#17,#21)1.0.0 (August 9, 2019)Initial release ofpytest-plt!
Thanks to all of the contributors for making this possible!