Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move DriverTestCase and mock Instruments out of tests module #5486

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changes/newsfragments/5486.new
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.
4 changes: 2 additions & 2 deletions src/qcodes/dist/tests/station/example.station.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ instruments:
add_parameters:
Bx: {source: ch01.fine_volt, label: Bx, unit: T, scale: 28, limits: [-1, 1], monitor: true}
mock_dac:
type: qcodes.tests.instrument_mocks.DummyInstrument
type: qcodes.instrument_drivers.mock_instruments.DummyInstrument
enable_forced_reconnect: true
init:
gates: {"ch1", "ch2"}
add_parameters:
Bx: {source: ch1, label: Bx, unit: T, scale: 28, limits: [-1, 1], monitor: true}
mock_dac2:
type: qcodes.tests.instrument_mocks.DummyInstrument
type: qcodes.instrument_drivers.mock_instruments.DummyInstrument
enable_forced_reconnect: true
7 changes: 6 additions & 1 deletion src/qcodes/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
The extensions module contains smaller modules that extend the functionality of QCoDeS.
These modules may import from all of QCoDeS but do not themselves get imported into QCoDeS.
"""
from ._driver_test_case import DriverTestCase
from ._log_export_info import log_dataset_export_info
from .installation import register_station_schema_with_vscode

__all__ = ["register_station_schema_with_vscode", "log_dataset_export_info"]
__all__ = [
"register_station_schema_with_vscode",
"log_dataset_export_info",
"DriverTestCase",
]
58 changes: 58 additions & 0 deletions src/qcodes/extensions/_driver_test_case.py
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

"""

Check warning on line 7 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/qcodes/extensions/_driver_test_case.py#L7

String statement has no effect
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

Check warning on line 34 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L34

Added line #L34 was not covered by tests

if cls.driver is None:
raise TypeError("you must set a driver for " + cls.__name__)

Check warning on line 37 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L37

Added line #L37 was not covered by tests

instances = cls.driver.instances()
name = cls.driver.__name__

if not instances:
msg = f"no instances of {name} found"
if getattr(cls, "noskip", False):

Check notice on line 44 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/qcodes/extensions/_driver_test_case.py#L44

Unnecessary "else" after "raise", remove the "else" and de-indent the code inside it
# just to test this class, we need to disallow skipping
raise ValueError(msg)

Check warning on line 46 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L46

Added line #L46 was not covered by tests
else:
raise unittest.SkipTest(msg)

if len(instances) == 1:
print(f"***** found one {name}, testing *****")

Check warning on line 51 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L50-L51

Added lines #L50 - L51 were not covered by tests
else:
print(

Check warning on line 53 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L53

Added line #L53 was not covered by tests
f"***** found {len(instances)} instances of {name}; "
"testing the last one *****"
)

cls.instrument = instances[-1]

Check warning on line 58 in src/qcodes/extensions/_driver_test_case.py

View check run for this annotation

Codecov / codecov/patch

src/qcodes/extensions/_driver_test_case.py#L58

Added line #L58 was not covered by tests