Skip to content

Commit

Permalink
Use instance attributes (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewFlamm authored Dec 27, 2022
1 parent 8c46ad5 commit cadb9e1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
10 changes: 5 additions & 5 deletions pytest_pyvista/pytest_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ class VerifyImageCache:
ignore_image_cache = False
fail_extra_image_cache = False

high_variance_tests = False
windows_skip_image_cache = False
macos_skip_image_cache = False

def __init__(
self,
test_name,
Expand All @@ -98,6 +94,10 @@ def __init__(
self.var_error_value = var_error_value
self.var_warning_value = var_warning_value

self.high_variance_test = False
self.windows_skip_image_cache = False
self.macos_skip_image_cache = False

self.skip = False
self.n_calls = 0

Expand Down Expand Up @@ -125,7 +125,7 @@ def __call__(self, plotter):
test_name = self.test_name
self.n_calls += 1

if self.high_variance_tests:
if self.high_variance_test:
allowed_error = self.var_error_value
allowed_warning = self.var_warning_value
else:
Expand Down
49 changes: 46 additions & 3 deletions tests/test_pyvista.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ def test_args(verify_image_cache):
result.stdout.fnmatch_lines("*[Pp]assed*")


def make_cached_images(test_path, path="image_cache_dir"):
def make_cached_images(test_path, path="image_cache_dir", name="imcache.png"):
"""Makes image cache in `test_path\path`."""
d = os.path.join(test_path, path)
os.mkdir(d)
if not os.path.isdir(d):
os.mkdir(d)
sphere = pv.Sphere()
plotter = pv.Plotter()
plotter.add_mesh(sphere, color="red")
plotter.screenshot(os.path.join(d, "imcache.png"))
plotter.screenshot(os.path.join(d, name))


def test_verify_image_cache(testdir):
Expand Down Expand Up @@ -135,3 +136,45 @@ def test_imcache(verify_image_cache):
)
result = testdir.runpytest("--fail_extra_image_cache")
result.stdout.fnmatch_lines("*[Pp]assed*")


def test_high_variance_test(testdir):
"""Test `skip` flag of `verify_image_cache`"""
make_cached_images(testdir.tmpdir)
make_cached_images(testdir.tmpdir, name="imcache_var.png")

# First make sure test fails with image regression error
testdir.makepyfile(test_file1=
"""
import pytest
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=[255, 5, 5])
plotter.show()
"""
)
# Next mark as a high_variance_test and check that it passes
testdir.makepyfile(test_file2=
"""
import pytest
import pyvista as pv
pv.OFF_SCREEN = True
def test_imcache_var(verify_image_cache):
verify_image_cache.high_variance_test = True
sphere = pv.Sphere()
plotter = pv.Plotter()
plotter.add_mesh(sphere, color=[255, 5, 5])
plotter.show()
"""
)
result = testdir.runpytest("--fail_extra_image_cache", "test_file1.py")
result.stdout.fnmatch_lines("*[Ff]ailed*")
result.stdout.fnmatch_lines("*Exceeded image regression error*")

result = testdir.runpytest("--fail_extra_image_cache", "test_file2.py")
result.stdout.fnmatch_lines("*[Pp]assed*")

0 comments on commit cadb9e1

Please sign in to comment.