Skip to content

Commit

Permalink
Introduce TestCase.require_module
Browse files Browse the repository at this point in the history
This patch introduces the require_module method to testing.TestCase, and uses
the new capability to simplify the setUp method of DocTestCase.
  • Loading branch information
gertjanvanzwieten committed Jan 30, 2025
1 parent b92a3d8 commit a69a2cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
39 changes: 32 additions & 7 deletions nutils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,44 @@ def emit(self, record):
print(record.msg)


def _not_has_module(module):
def _has_module(module):
try:
importlib.import_module(module)
except ImportError:
return True
else:
return False
else:
return True


def requires(*modules):
missing = tuple(filter(_not_has_module, modules))
def _require(category, test, *items):
missing = [item for item in items if not test(item)]
if missing:
return unittest.skip('missing module{}: {}'.format('s' if len(missing) > 1 else '', ','.join(missing)))
if len(missing) > 1:
category += 's'
missing = ', '.join(missing)
raise unittest.SkipTest(f'missing {category}: {missing}')

Check warning on line 45 in nutils/testing.py

View workflow job for this annotation

GitHub Actions / Test coverage

Lines not covered

Lines 42–45 of `nutils/testing.py` are not covered by tests.


require_module = functools.partial(_require, 'module', _has_module)


def _test_decorator(test, *args):
try:
test(*args)
except Exception as outer_exc:
inner_exc = outer_exc
def wrapper(f):
@functools.wraps(f)
def wrapped(self):
raise inner_exc
return wrapped

Check warning on line 60 in nutils/testing.py

View workflow job for this annotation

GitHub Actions / Test coverage

Lines not covered

Lines 54–60 of `nutils/testing.py` are not covered by tests.
else:
return lambda func: func
def wrapper(f):
return f
return wrapper


requires = functools.partial(_test_decorator, require_module) # decorator version of require_module


class _ParametrizedCollection(type):
Expand Down Expand Up @@ -247,6 +270,8 @@ def assertAlmostEqual64(self, actual, desired, *, atol=2e-15, rtol=2e-3, dtype='
status.extend(s[i:i+80] for i in range(0, len(s), 80))
self.fail('\n'.join(status))

require_module = staticmethod(require_module)


ContextTestCase = TestCase

Expand Down
4 changes: 1 addition & 3 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def setUp(self):
if blank and line[indent:].startswith('.. requires:: '):
requires.extend(name.strip() for name in line[indent+13:].split(','))
blank = not line.strip()
missing = tuple(filter(nutils.testing._not_has_module, requires))
if missing:
self.skipTest('missing module{}: {}'.format('s' if len(missing) > 1 else '', ','.join(missing)))
self.require_module(*requires)

if 'matplotlib' in requires:
import matplotlib.testing
Expand Down

0 comments on commit a69a2cd

Please sign in to comment.