diff options
Diffstat (limited to 'tests/__init__.py')
| -rw-r--r-- | tests/__init__.py | 221 |
1 files changed, 41 insertions, 180 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index 7fc393e..7640296 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,40 +1,22 @@ -import functools import glob import logging import os -import re -import shutil -import sys -import tempfile import unittest import pytest -import six from chirp import directory -from tests import run_tests - +from tests import test_banks +from tests import test_brute_force +from tests import test_clone +from tests import test_copy_all +from tests import test_detect +from tests import test_edges +from tests import test_features +from tests import test_settings LOG = logging.getLogger('testadapter') -PY3_XFAIL = [] - - -def if_enabled(fn): - name = fn.__name__.replace('test_', '') - if 'CHIRP_TESTS' in os.environ: - tests_enabled = os.environ['CHIRP_TESTS'].split(',') - enabled = name in tests_enabled - else: - enabled = True - - def wrapper(*a, **k): - if enabled: - return fn(*a, **k) - else: - raise unittest.SkipTest('%s not enabled' % name) - - return wrapper class TestAdapterMeta(type): @@ -42,101 +24,10 @@ class TestAdapterMeta(type): return super(TestAdapterMeta, cls).__new__(cls, name, parents, dct) -class TestAdapter(unittest.TestCase): - RADIO_CLASS = None - SOURCE_IMAGE = None - RADIO_INST = None - testwrapper = None - XFAIL = False - - def shortDescription(self): - test = self.id().split('.')[-1].replace('test_', '').replace('_', ' ') - return 'Testing %s %s' % (self.RADIO_CLASS.get_name(), test) - - @classmethod - def setUpClass(cls): - if not cls.testwrapper: - # Initialize the radio once per class invocation to save - # bitwise parse time - # Do this for things like Generic_CSV, that demand it - _base, ext = os.path.splitext(cls.SOURCE_IMAGE) - cls.testimage = tempfile.mktemp(ext) - shutil.copy(cls.SOURCE_IMAGE, cls.testimage) - cls.testwrapper = run_tests.TestWrapper(cls.RADIO_CLASS, - cls.testimage) - - @classmethod - def tearDownClass(cls): - os.remove(cls.testimage) - - def setUp(self): - self._out = run_tests.TestOutputANSI() - rid = "%s_%s_" % (self.RADIO_CLASS.VENDOR, self.RADIO_CLASS.MODEL) - rid = rid.replace("/", "_") - basefn, ext = os.path.splitext(self.testimage) - self.testimage = tempfile.mktemp('.%s' % ext, rid) - shutil.copy(self.SOURCE_IMAGE, self.testimage) - - def tearDown(self): - os.remove(self.testimage) - - def _runtest(self, test): - tw = run_tests.TestWrapper(self.RADIO_CLASS, - self.testimage, - dst=self.RADIO_INST) - testcase = test(tw) - testcase.prepare() - try: - failures = testcase.run() - if failures: - raise failures[0] - except run_tests.TestCrashError as e: - raise e.get_original_exception() - except run_tests.TestSkippedError as e: - raise unittest.SkipTest(str(e)) - finally: - testcase.cleanup() - - @if_enabled - def test_copy_all(self): - self._runtest(run_tests.TestCaseCopyAll) - - @if_enabled - def test_brute_force(self): - self._runtest(run_tests.TestCaseBruteForce) - - @if_enabled - def test_edges(self): - self._runtest(run_tests.TestCaseEdges) - - @if_enabled - def test_settings(self): - self._runtest(run_tests.TestCaseSettings) - - @if_enabled - def test_banks(self): - self._runtest(run_tests.TestCaseBanks) - - @if_enabled - def test_detect(self): - self._runtest(run_tests.TestCaseDetect) - - @if_enabled - def test_clone(self): - self._runtest(run_tests.TestCaseClone) - - def test_py3_expected(self): - assert self.SOURCE_IMAGE not in PY3_XFAIL - - def _get_sub_devices(rclass, testimage): try: - tw = run_tests.TestWrapper(rclass, None) - except Exception as e: - tw = run_tests.TestWrapper(rclass, testimage) - - try: - rf = tw.do("get_features") + radio = rclass(None) + rf = radio.get_features() except Exception as e: print('Failed to get features for %s: %s' % (rclass, e)) # FIXME: If the driver fails to run get_features with no memobj @@ -145,23 +36,14 @@ def _get_sub_devices(rclass, testimage): # out, but until then, assume crash means "no sub devices". return [rclass] if rf.has_sub_devices: - return tw.do("get_sub_devices") + # Radios with sub-devices may need to look at the image to determine + # what those are. That's slow, so only do it for these. + radio = rclass(testimage) + return radio.get_sub_devices() else: return [rclass] -class TestAdapterXFAIL(TestAdapter): - @pytest.mark.xfail(reason=('Driver not expected to run in python3. ' - 'Remove from py3_remaining.txt'), strict=True) - def test_py3_expected(self): - pass - - -class RadioSkipper(unittest.TestCase): - def test_is_supported_by_environment(self): - raise unittest.SkipTest('Running in py3 and driver is not supported') - - def _load_tests(loader, tests, pattern, suite=None): if not suite: suite = unittest.TestSuite() @@ -181,57 +63,36 @@ def _load_tests(loader, tests, pattern, suite=None): # This default is meaningless for us pattern = None - py3_remaining = [x.strip() for x in open(os.path.join( - base, '..', 'py3_remaining.txt')).readlines() - if not x.startswith('#')] + driver_test_cases = (test_edges.TestCaseEdges, + test_brute_force.TestCaseBruteForce, + test_banks.TestCaseBanks, + test_detect.TestCaseDetect, + test_clone.TestCaseClone, + test_settings.TestCaseSettings, + test_features.TestCaseFeatures, + test_copy_all.TestCaseCopyAll) for image, test in tests.items(): - try: - rclass = directory.get_radio(test) - if os.path.basename(image) in py3_remaining: - PY3_XFAIL.append(image) - - except Exception: - if not six.PY3: - raise - - if 'CHIRP_DEBUG' in os.environ: - LOG.error('Failed to load %s' % test) - - if os.path.basename(image) in py3_remaining: - # We expect this to fail to import in py3, so do not - # abort the test run. - continue - print('%s not in py3 exclusion list %s...' % ( - os.path.basename(image), ','.join(py3_remaining))) - raise - for device in _get_sub_devices(rclass, image): - class_name = 'TestCase_%s' % ( - ''.join(filter(lambda c: c.isalnum(), - device.get_name()))) - if isinstance(device, type): - dst = None - else: - dst = device + rclass = directory.get_radio(test) + if hasattr(rclass, '_orig_rclass'): + rclass = rclass._orig_rclass + module = rclass.__module__.split('.')[-1] + subdevs = _get_sub_devices(rclass, image) + has_subdevs = subdevs != [rclass] + for index, device in enumerate(subdevs): + if not isinstance(device, type): device = device.__class__ - - if image in PY3_XFAIL: - cls = TestAdapterXFAIL - else: - cls = TestAdapter - - tc = TestAdapterMeta( - class_name, (cls,), dict(RADIO_CLASS=device, - SOURCE_IMAGE=image, - RADIO_INST=dst)) - tests = loader.loadTestsFromTestCase(tc) - - if pattern: - tests = [t for t in tests - if re.search(pattern, '%s.%s' % (class_name, - t._testMethodName))] - - suite.addTests(tests) + for case in driver_test_cases: + tc = TestAdapterMeta( + "%s_%s" % (case.__name__, + directory.radio_class_id(device)), + (case,), + {'RADIO_CLASS': rclass, + 'SUB_DEVICE': index if has_subdevs else None, + 'TEST_IMAGE': image}) + + tc = getattr(pytest.mark, module)(tc) + suite.addTests(loader.loadTestsFromTestCase(tc)) return suite |
