Source code for qcodes.extensions._driver_test_case
from__future__importannotationsimportunittestfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:fromqcodes.instrumentimportInstrument"""This module defines:- `DriverTestCase`: a `TestCase` subclass meant for testing instrument driversUsing `DriverTestCase` is pretty easy:- Inherit from this class instead of from the base `unittest.TestCase`- Provide a driver class variable that points to the Instrument class- In your tests, `self.instrument` is the latest instance of this class.- If your test case includes a `setUpClass` method, make sure to call `super().setUpClass()`, because that's where we find the latest instance of this `Instrument`, or skip the test case if no instances are found."""
[docs]classDriverTestCase(unittest.TestCase):# override this in a subclassdriver:type[Instrument]|None=Noneinstrument:Instrument
[docs]@classmethoddefsetUpClass(cls)->None:ifclsisDriverTestCase:returnifcls.driverisNone:raiseTypeError("you must set a driver for "+cls.__name__)instances=cls.driver.instances()name=cls.driver.__name__ifnotinstances:msg=f"no instances of {name} found"ifgetattr(cls,"noskip",False):# just to test this class, we need to disallow skippingraiseValueError(msg)else:raiseunittest.SkipTest(msg)iflen(instances)==1:print(f"***** found one {name}, testing *****")else:print(f"***** found {len(instances)} instances of {name}; ""testing the last one *****")cls.instrument=instances[-1]