diff options
author | Dylan Baker <baker.dylan.c@gmail.com> | 2015-10-08 17:43:12 -0700 |
---|---|---|
committer | Dylan Baker <baker.dylan.c@gmail.com> | 2016-01-21 14:13:33 -0800 |
commit | 1541948cd02aaa4c65be808a09739ca97231d2a6 (patch) | |
tree | 4d3b8921d18137511f14811105ffaac722ce746c /unittests | |
parent | ed5e91d572e7b2561d88f6fadd38efa73cffac49 (diff) |
framework/tests: add helper for checking for 3rd party modules
This little helper skips a test if a module isn't available.
Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/junit_backends_tests.py | 3 | ||||
-rw-r--r-- | unittests/utils.py | 9 |
2 files changed, 10 insertions, 2 deletions
diff --git a/unittests/junit_backends_tests.py b/unittests/junit_backends_tests.py index 5b4f80132..1e71f3ffd 100644 --- a/unittests/junit_backends_tests.py +++ b/unittests/junit_backends_tests.py @@ -113,8 +113,7 @@ class TestJUnitSingleTest(TestJunitNoTests): def test_xml_valid(self): """backends.junit.JUnitBackend.write_test(): (once) produces valid xml""" - if etree.__name__ != 'lxml.etree': - raise SkipTest('Test requires lxml features') + utils.module_check('lxml') schema = etree.XMLSchema(file=JUNIT_SCHEMA) with open(self.test_file, 'r') as f: nt.ok_(schema.validate(etree.parse(f)), msg='xml is not valid') diff --git a/unittests/utils.py b/unittests/utils.py index 2a9370c91..aaf782c1f 100644 --- a/unittests/utils.py +++ b/unittests/utils.py @@ -34,6 +34,7 @@ import tempfile as tempfile_ import functools import subprocess import errno +import importlib from contextlib import contextmanager try: @@ -314,6 +315,14 @@ def binary_check(bin_, errno_=None): e.returncode, errno_)) +def module_check(module): + """Check that an external module is available or skip.""" + try: + importlib.import_module(module) + except ImportError: + raise SkipTest('Required module {} not available.'.format(module)) + + def platform_check(plat): """If the platform is not in the list specified skip the test.""" if not sys.platform.startswith(plat): |