Skip to content

Commit

Permalink
tests: don't present untestable base classes via pytest
Browse files Browse the repository at this point in the history
pytest collects tests in classes that either derive from
`unittest.TestCase` or are named with a `Test` prefix, where functions
have a `test` prefix. More details on this logic are given at
https://docs.pytest.org/en/7.4.x/explanation/goodpractices.html#test-discovery.

pyftdi's `eeprom_mock` test has two base classes, `EepromMirrorTestCase`
and `NonMirroredEepromTestCase`, that derive from `unittest.TestCase`
and contain `test`-prefixed functions. These tests cannot be run from
within the base classes, because they depend on a variable,
`TEST_CONFIG_FILENAME`, which is expected to be set in their subclasses.
The tests can only run from within the subclasses, and will produce an
error result when attempting to run them from the base classes.

To prevent pytest from collecting the untestable base class tests, the
base classes are changed to not derive from `unittest.TestCase`,
preferring instead to use multiple inheritance to allow the subclasses
to derive from that class. This makes it possible to easily run the
`eeprom_mock` test via pytest without spurious errors related to the
untestable base classes.
  • Loading branch information
markmentovai authored and eblot committed Apr 5, 2024
1 parent 8e1f4db commit e2b4ac4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pyftdi/tests/eeprom_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
VirtLoader = None


class FtdiTestCase(TestCase):
class FtdiTestCase:
"""Common features for all tests.
"""

Expand Down Expand Up @@ -338,39 +338,39 @@ def _check_for_non_mirrored_eeprom_contents(self, eeprom: FtdiEeprom):
self.assertNotEqual(normal_mirror_s1, normal_mirror_s2)


class EepromMirrorFt232hTestCase(EepromMirrorTestCase):
class EepromMirrorFt232hTestCase(EepromMirrorTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft232h.yaml'


class EepromMirrorFt2232hTestCase(EepromMirrorTestCase):
class EepromMirrorFt2232hTestCase(EepromMirrorTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft2232h.yaml'


class EepromMirrorFt4232hTestCase(EepromMirrorTestCase):
class EepromMirrorFt4232hTestCase(EepromMirrorTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft4232h.yaml'


class EepromMirrorFt232rTestCase(NonMirroredEepromTestCase):
class EepromMirrorFt232rTestCase(NonMirroredEepromTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft232r.yaml'
DEVICE_CAN_MIRROR = False


class EepromMirrorFt230xTestCase(NonMirroredEepromTestCase):
class EepromMirrorFt230xTestCase(NonMirroredEepromTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft230x.yaml'
DEVICE_CAN_MIRROR = False


class EepromNonMirroredFt232hTestCase(NonMirroredEepromTestCase):
class EepromNonMirroredFt232hTestCase(NonMirroredEepromTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft232h.yaml'
DEVICE_CAN_MIRROR = True


class EepromNonMirroredFt2232hTestCase(NonMirroredEepromTestCase):
class EepromNonMirroredFt2232hTestCase(NonMirroredEepromTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft2232h.yaml'
DEVICE_CAN_MIRROR = True


class EepromNonMirroredFt4232hTestCase(NonMirroredEepromTestCase):
class EepromNonMirroredFt4232hTestCase(NonMirroredEepromTestCase, TestCase):
TEST_CONFIG_FILENAME = 'pyftdi/tests/resources/ft4232h.yaml'
DEVICE_CAN_MIRROR = True

Expand Down

0 comments on commit e2b4ac4

Please sign in to comment.