pyvista / pytest-pyvista

Plugin to test PyVista plot outputs
https://pytest.pyvista.org
MIT License
13 stars 5 forks source link
image-regression pytest pyvista

============== pytest-pyvista

.. image:: https://img.shields.io/pypi/v/pytest-pyvista.svg?color=orange&label=pypi&logo=python&logoColor=white :target: https://pypi.org/project/pytest-pyvista :alt: PyPI version

.. image:: https://img.shields.io/conda/vn/conda-forge/pytest-pyvista?color=orange&label=conda-forge&logo=conda-forge&logoColor=white :target: https://anaconda.org/conda-forge/pytest-pyvista :alt: conda-forge version

.. image:: https://img.shields.io/pypi/pyversions/pytest-pyvista.svg?color=orange&logo=python&label=python&logoColor=white :target: https://pypi.org/project/pytest-pyvista :alt: Python versions

.. image:: https://github.com/pyvista/pytest-pyvista/actions/workflows/ci_cd.yml/badge.svg :target: https://github.com/pyvista/pytest-pyvista/actions/workflows/ci_cd.yml :alt: GitHub Actions: Unit Testing and Deployment

Plugin to test PyVista plot outputs.


This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Features

This plugin facilitates the comparison of the images produced by PyVista. It generates a cache of images from the tests, using the PyVista plotting 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.

Requirements

You 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.

Installation

You can install "pytest-pyvista" via pip from PyPI::

$ pip install pytest-pyvista

Alternatively, you can also install via conda or mamba from conda-forge_::

$ mamba install -c conda-forge pytest-pyvista

Usage

Once installed, you only need to use the command pl.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 enable pv.OFF_SCREEN when loading PyVista, so the pl.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_cache

If you need to use any flag inside the tests, you can modify the verify_image_cache object 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 flags

These are the flags you can use when calling pytest in the command line:

Test specific flags

These are attributes of verify_image_cache. You can set them as True if needed in the beginning of your test function.

Configuration

If using pyproject.toml or any other pytest configuration <https://docs.pytest.org/en/latest/reference/customize.html>_ section, consider configuring your test directory location to avoid passing command line arguments when calling pytest, for example in pyproject.toml:

.. code::

[tool.pytest.ini_options] image_cache_dir = "tests/plotting/image_cache"

Additionally, to configure the directory that will contain the generated test images:

.. code::

[tool.pytest.ini_options] generated_image_dir = "generated_images"

Contributing

Contributions are always welcome. Tests can be run with tox_, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT_ license, pytest-pyvista is free and open source software.

Issues

If you encounter any problems, please file an issue_ along with a detailed description.

.. Cookiecutter: https://github.com/audreyr/cookiecutter .. @hackebrot: https://github.com/hackebrot .. MIT: http://opensource.org/licenses/MIT .. BSD-3: http://opensource.org/licenses/BSD-3-Clause .. GNU GPL v3.0: http://www.gnu.org/licenses/gpl-3.0.txt .. Apache Software License 2.0: http://www.apache.org/licenses/LICENSE-2.0 .. cookiecutter-pytest-plugin: https://github.com/pytest-dev/cookiecutter-pytest-plugin .. file an issue: https://github.com/pyvista/pytest-pyvista/issues .. pytest: https://github.com/pytest-dev/pytest .. tox: https://tox.readthedocs.io/en/latest/ .. pip: https://pypi.org/project/pip/ .. PyPI: https://pypi.org/project .. conda: https://github.com/conda/conda .. mamba: https://github.com/mamba-org/mamba .. _conda-forge: https://anaconda.org/conda-forge/pytest-pyvista