Skip to content

Commit

Permalink
TST: encaplsulate test skipping in a unified function that should wor…
Browse files Browse the repository at this point in the history
…k on any test runner with any Python version
  • Loading branch information
neutrinoceros committed Dec 6, 2021
1 parent 6b5e748 commit 4cfbb50
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
31 changes: 19 additions & 12 deletions yt/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import matplotlib
import numpy as np
import pytest
from more_itertools import always_iterable
from numpy.random import RandomState
from unyt.exceptions import UnitOperationError
Expand Down Expand Up @@ -856,6 +857,21 @@ def expand_keywords(keywords, full=False):
return list_of_kwarg_dicts


def skip_case(*, reason: str):
"""
An adapter test skipping function that should work with both test runners
(nosetest or pytest) and with any Python version.
"""
if sys.version_info >= (3, 10):
# nose isn't compatible with Python 3.10 so we can't import it here
pytest.skip(reason)
else:
# pytest.skip() isn't recognized by nosetest (but nose.SkipTest works in pytest !)
from nose import SkipTest

raise SkipTest(reason)


def requires_module(module):
"""
Decorator that takes a module name as an argument and tries to import it.
Expand All @@ -864,12 +880,11 @@ def requires_module(module):
being imported will not fail if the module is not installed on the testing
platform.
"""
from nose import SkipTest

def ffalse(func):
@functools.wraps(func)
def false_wrapper(*args, **kwargs):
raise SkipTest
skip_case(reason=f"Missing required module {module}")

return false_wrapper

Expand Down Expand Up @@ -901,8 +916,6 @@ def requires_module_pytest(*module_names):
So that it can be later renamed to `requires_module`.
"""
import pytest

from yt.utilities import on_demand_imports as odi

def deco(func):
Expand Down Expand Up @@ -930,16 +943,14 @@ def inner_func(*args, **kwargs):


def requires_file(req_file):
from nose import SkipTest

path = ytcfg.get("yt", "test_data_dir")

def ffalse(func):
@functools.wraps(func)
def false_wrapper(*args, **kwargs):
if ytcfg.get("yt", "internals", "strict_requires"):
raise FileNotFoundError(req_file)
raise SkipTest
skip_case(reason=f"Missing required file {req_file}")

return false_wrapper

Expand Down Expand Up @@ -1339,7 +1350,6 @@ def requires_backend(backend):
Decorated function or null function
"""
import pytest

def ffalse(func):
# returning a lambda : None causes an error when using pytest. Having
Expand All @@ -1350,8 +1360,7 @@ def ffalse(func):
# exception in the xfail case for that test
def skip(*args, **kwargs):
msg = f"`{backend}` backend not in use, skipping: `{func.__name__}`"
print(msg, file=sys.stderr)
pytest.skip(msg)
skip_case(reason=msg)

if ytcfg.get("yt", "internals", "within_pytest"):
return skip
Expand All @@ -1367,8 +1376,6 @@ def ftrue(func):


def requires_external_executable(*names):
import pytest

def deco(func):
missing = []
for name in names:
Expand Down
8 changes: 0 additions & 8 deletions yt/visualization/tests/test_geo_projections.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import unittest

import pytest
from nose.plugins.attrib import attr
from packaging.version import Version

import yt
from yt.testing import ANSWER_TEST_TAG, fake_amr_ds, requires_module
Expand Down Expand Up @@ -134,12 +132,6 @@ def test_projection_object(self):
def test_nondefault_transform(self):
from yt.utilities.on_demand_imports import _cartopy as cartopy

if Version(cartopy.__version__) >= Version("0.19"):
# see https://github.com/yt-project/yt/issues/3705
pytest.skip(
"SlicePlot fails with 'ValueError: Invalid vmin or vmax' (frb is full of nans or infs)"
)

axis = "altitude"
self.ds.coordinates.data_transform[axis] = "Miller"
self.slc = yt.SlicePlot(self.ds, axis, ("stream", "Density"), origin="native")
Expand Down

0 comments on commit 4cfbb50

Please sign in to comment.