Package: pytest-pyvista
Plugin to test PyVista plot outputs.Thispytestplugin was generated withCookiecutteralong with@hackebrot’scookiecutter-pytest-plugintemplate.FeaturesThis plugin facilitates the comparison of the images produced byPyVista. It
generates a cache of images from the tests, using thePyVistaplotting
function in its first execution. Then, further executions will compare its
results against this cache, so if there are any changes in the code that break
the image generation, the comparison against the cache will notice it. Note
that there is an error tolerance in the comparison, so minor differences won’t
fail.RequirementsYou must have a Python version >= 3.8, as well as PyVista installed
in your environment.pyvista version >=0.37.0 and vtk version >=9.0.0 required.InstallationYou can install “pytest-pyvista” viapipfromPyPI:$ pip install pytest-pyvistaAlternatively, you can also install viacondaormambafromconda-forge:$ mamba install -c conda-forge pytest-pyvistaUsageOnce installed, you only need to use the commandpl.show()in your test. The
plugin will automatically manage the cache generation if it does not exist, and
the image comparison itself. Make sure you enablepv.OFF_SCREENwhen loading
PyVista, so thepl.show()doesn’t pop up any window while testing.  By default,
the verify_image_cache fixture should be used for each test for image comparison:import pyvista as pv
pv.OFF_SCREEN = True
def test_succeeds(verify_image_cache):
    pl = pyvista.Plotter()
    pl.add_mesh(pyvista.Sphere(), show_edges=True)
    pl.show()If most tests utilize this functionality, possibly restricted to a module,
a wrapped version could be used:@pytest.fixture(autouse=True)
def wrapped_verify_image_cache(verify_image_cache):
    return verify_image_cacheIf you need to use any flag inside the tests, you can modify theverify_image_cacheobject in the test:import pyvista as pv
pv.OFF_SCREEN = True
def test_succeeds(verify_image_cache):
    verify_image_cache.windows_skip_image_cache = True
    pl = pyvista.Plotter()
    pl.add_mesh(pyvista.Sphere(), show_edges=True)
    pl.show()Global flagsThese are the flags you can use when callingpytestin the command line:--reset_image_cachecreates a new image for each test intests/plotting/test_plotting.pyand is not recommended except for
testing or for potentially a major or minor release.You can use--ignore_image_cacheif you want to
temporarily ignore regression testing, e.g. on a particular CI action.When using--fail_extra_image_cacheif there is an extra image in the
cache, it will report as an error.--generated_image_dir<DIR>dumps all generated test images into <DIR>.--add_missing_imagesadds any missing images from the test run to the cache.--image_cache_dir<DIR>sets the image cache dir.  This will override any
configuration, see below.--reset_only_failedreset the image cache of the failed tests only.Test specific flagsThese are attributes ofverify_image_cache. You can set them asTrueif needed
in the beginning of your test function.high_variance_test: If necessary, the threshold for determining if a test
will pass or not is incremented to another predetermined threshold. This is
currently done due to the use of an unstable version of VTK, in stable
versions this shouldn’t be necessary.windows_skip_image_cache: For test where the plotting in Windows is different
from MacOS/Linux.macos_skip_image_cache: For test where the plotting in MacOS is different
from Windows/Linux.skip: If you have a test that plots a figure, but you don’t want to compare
its output against the cache, you can skip it with this flag.ConfigurationIf usingpyproject.tomlor any otherpytest configurationsection, consider configuring your test directory location to
avoid passing command line arguments when callingpytest, for example inpyproject.toml[tool.pytest.ini_options]
image_cache_dir = "tests/plotting/image_cache"ContributingContributions are always welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.LicenseDistributed under the terms of theMITlicense,pytest-pyvistais free
and open source software.IssuesIf you encounter any problems, pleasefile an issuealong with a detailed
description.