Skip to content

Commit

Permalink
Add generated_image_dir ini configuration (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjlittle authored Jan 1, 2024
1 parent af19768 commit 00c510b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
15 changes: 12 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ These are the flags you can use when calling ``pytest`` in the command line:
* When using ``--fail_extra_image_cache`` if 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>.
* ``--generated_image_dir <DIR>`` dumps all generated test images into the provided
directory. This will override any configuration, see below.

* ``--add_missing_images`` adds any missing images from the test run to the cache.

* ``--image_cache_dir <DIR>`` sets the image cache dir. This will override any
* ``--image_cache_dir <DIR>`` sets the image cache directory. This will override any
configuration, see below.

* ``--reset_only_failed`` reset the image cache of the failed tests only.
Expand Down Expand Up @@ -138,13 +139,21 @@ 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``
``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
Expand Down
7 changes: 7 additions & 0 deletions pytest_pyvista/pytest_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def pytest_addoption(parser):
action="store",
help="Path to dump test images from the current run.",
)
parser.addini(
"generated_image_dir",
default="generated_image_dir",
help="Path to dump test images from the current run.",
)
group.addoption(
"--add_missing_images",
action="store_true",
Expand Down Expand Up @@ -271,6 +276,8 @@ def verify_image_cache(request, pytestconfig):
cache_dir = pytestconfig.getini("image_cache_dir")

gen_dir = pytestconfig.getoption("generated_image_dir")
if gen_dir is None:
gen_dir = pytestconfig.getini("generated_image_dir")

verify_image_cache = VerifyImageCache(
request.node.name, cache_dir, generated_image_dir=gen_dir
Expand Down
26 changes: 26 additions & 0 deletions tests/test_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,32 @@ def test_imcache(verify_image_cache):
result.stdout.fnmatch_lines("*[Pp]assed*")


def test_generated_image_dir_ini(testdir):
"""Test setting generated_image_dir via config."""
make_cached_images(testdir.tmpdir)
testdir.makepyfile(
"""
import pyvista as pv
pv.OFF_SCREEN = True
def test_imcache(verify_image_cache):
sphere = pv.Sphere()
plotter = pv.Plotter()
plotter.add_mesh(sphere, color="red")
plotter.show()
"""
)
testdir.makepyprojecttoml(
"""
[tool.pytest.ini_options]
generated_image_dir = "gen_dir"
"""
)
result = testdir.runpytest("--fail_extra_image_cache")
assert os.path.isdir(os.path.join(testdir.tmpdir, "gen_dir"))
assert os.path.isfile(os.path.join(testdir.tmpdir, "gen_dir", "imcache.png"))
result.stdout.fnmatch_lines("*[Pp]assed*")


def test_add_missing_images_commandline(testdir):
"""Test setting add_missing_images via CLI option."""
testdir.makepyfile(
Expand Down

0 comments on commit 00c510b

Please sign in to comment.