Skip to content

Commit 0b285f5

Browse files
committed
QA: Migrate tests to use fixtures.
1 parent eeaba1b commit 0b285f5

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

tests/conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
import sys
3+
4+
import mock
5+
6+
from .tools import CALIBRATED_VALUES, SMBusFakeAS7262
7+
8+
import pytest
9+
10+
11+
@pytest.fixture
12+
def smbus():
13+
smbus = mock.Mock()
14+
smbus.SMBus = SMBusFakeAS7262
15+
sys.modules['smbus2'] = smbus
16+
yield smbus
17+
18+
19+
@pytest.fixture
20+
def AS7262():
21+
from as7262 import AS7262
22+
yield AS7262
23+
del sys.modules['as7262']

tests/test_features.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
# noqa D100
2-
import sys
2+
from .tools import CALIBRATED_VALUES
33

4-
import mock
54

6-
from .tools import CALIBRATED_VALUES, SMBusFakeAS7262
7-
8-
9-
def _setup():
10-
global as7262
11-
smbus = mock.Mock()
12-
smbus.SMBus = SMBusFakeAS7262
13-
sys.modules['smbus2'] = smbus
14-
from as7262 import AS7262
15-
as7262 = AS7262()
16-
17-
18-
def test_set_integration_time():
5+
def test_set_integration_time(smbus, AS7262):
196
"""Test the set_integration_time method against various values."""
20-
_setup()
7+
as7262 = AS7262()
218

229
# Integration time is stored as 2.8ms per lsb
2310
# so returned values experience quantization
@@ -39,9 +26,9 @@ def test_set_integration_time():
3926
assert as7262._as7262.INTEGRATION_TIME.get_ms() == (int(99999 * 2.8) & 0xFF) / 2.8
4027

4128

42-
def test_set_gain():
29+
def test_set_gain(smbus, AS7262):
4330
"""Test the set_gain method against various values."""
44-
_setup()
31+
as7262 = AS7262()
4532

4633
as7262.set_gain(1)
4734
assert as7262._as7262.CONTROL.get_gain_x() == 1
@@ -55,17 +42,17 @@ def test_set_gain():
5542
assert as7262._as7262.CONTROL.get_gain_x() == 1
5643

5744

58-
def test_set_measurement_mode():
45+
def test_set_measurement_mode(smbus, AS7262):
5946
"""Test the set_measurement_mode method."""
60-
_setup()
47+
as7262 = AS7262()
6148

6249
as7262.set_measurement_mode(2)
6350
assert as7262._as7262.CONTROL.get_measurement_mode() == 2
6451

6552

66-
def test_set_illumination_led_current():
53+
def test_set_illumination_led_current(smbus, AS7262):
6754
"""Test the set_illumination_led_current method."""
68-
_setup()
55+
as7262 = AS7262()
6956

7057
as7262.set_illumination_led_current(12.5)
7158
assert as7262._as7262.LED_CONTROL.get_illumination_current_limit_ma() == 12.5
@@ -77,9 +64,9 @@ def test_set_illumination_led_current():
7764
assert as7262._as7262.LED_CONTROL.get_illumination_current_limit_ma() == 100
7865

7966

80-
def test_set_indicator_led_current():
67+
def test_set_indicator_led_current(smbus, AS7262):
8168
"""Test the set_indicator_led_current method."""
82-
_setup()
69+
as7262 = AS7262()
8370

8471
as7262.set_indicator_led_current(4)
8572
assert as7262._as7262.LED_CONTROL.get_indicator_current_limit_ma() == 4
@@ -91,33 +78,33 @@ def test_set_indicator_led_current():
9178
assert as7262._as7262.LED_CONTROL.get_indicator_current_limit_ma() == 1
9279

9380

94-
def test_indicator_led():
81+
def test_indicator_led(smbus, AS7262):
9582
"""Test the indicator_led method."""
96-
_setup()
83+
as7262 = AS7262()
9784

9885
as7262.set_indicator_led(1)
9986
assert as7262._as7262.LED_CONTROL.get_indicator_enable() == 1
10087

10188

102-
def test_illumination_led():
89+
def test_illumination_led(smbus, AS7262):
10390
"""Test the illumination_led method."""
104-
_setup()
91+
as7262 = AS7262()
10592

10693
as7262.set_illumination_led(1)
10794
assert as7262._as7262.LED_CONTROL.get_illumination_enable() == 1
10895

10996

110-
def test_soft_reset():
97+
def test_soft_reset(smbus, AS7262):
11198
"""Test the soft_reset method."""
112-
_setup()
99+
as7262 = AS7262()
113100

114101
as7262.soft_reset()
115102
assert as7262._as7262.CONTROL.get_reset() == 1
116103

117104

118-
def test_get_calibrated_values():
105+
def test_get_calibrated_values(smbus, AS7262):
119106
"""Test against fake calibrated values stored in hardware mock."""
120-
_setup()
107+
as7262 = AS7262()
121108

122109
values = as7262.get_calibrated_values()
123110

tests/test_setup.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
# noqa D100
2-
import sys
32

4-
import mock # noqa: E402
53

6-
from .tools import SMBusFakeAS7262 # noqa: E402
7-
8-
9-
def test_fw_info():
4+
def test_fw_info(smbus, AS7262):
105
"""Test against fake device information stored in hardware mock."""
11-
smbus = mock.Mock()
12-
smbus.SMBus = SMBusFakeAS7262
13-
sys.modules['smbus2'] = smbus
14-
from as7262 import AS7262
15-
166
as7262 = AS7262()
177

188
hw_type, hw_version, fw_version = as7262.get_version()

0 commit comments

Comments
 (0)