-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5486 from jenshnielsen/move_test_utils
Move DriverTestCase and mock Instruments out of tests module
- Loading branch information
Showing
39 changed files
with
1,405 additions
and
1,309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Mock instruments have been moved from `qcodes.tests.instrument_mocks` to `qcodes.instrument_drivers.mock_instruments` and `DriverTestCase` from | ||
`qcodes.tests.driver_test_case` to `qcodes.extensions`. This is in preparation for no longer shipping `qcodes.tests` as part of the | ||
public api. The previous locations remain functional but will be deprecated after one release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import annotations | ||
|
||
import unittest | ||
|
||
from qcodes.instrument import Instrument | ||
|
||
""" | ||
This module defines: | ||
- `DriverTestCase`: a `TestCase` subclass meant for testing instrument drivers | ||
Using `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. | ||
""" | ||
|
||
|
||
class DriverTestCase(unittest.TestCase): | ||
# override this in a subclass | ||
driver: type[Instrument] | None = None | ||
instrument: Instrument | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
if cls is DriverTestCase: | ||
return | ||
|
||
if cls.driver is None: | ||
raise TypeError("you must set a driver for " + cls.__name__) | ||
|
||
instances = cls.driver.instances() | ||
name = cls.driver.__name__ | ||
|
||
if not instances: | ||
msg = f"no instances of {name} found" | ||
if getattr(cls, "noskip", False): | ||
# just to test this class, we need to disallow skipping | ||
raise ValueError(msg) | ||
else: | ||
raise unittest.SkipTest(msg) | ||
|
||
if len(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] |
Oops, something went wrong.