22import sys
33
44
5- def force_reimport (module ):
6- """Force the module under test to be re-imported.
7-
8- Because pytest runs all tests within the same scope (this makes me cry)
9- we have to do some manual housekeeping to avoid tests polluting each other.
10-
11- In this case, the first `from fanshim import FanShim` would import both plasma
12- and RPi.GPIO from the first test run fixtures. Since we want *clean* fixtures
13- for each test this is not only no good but the results are outright weird-
14-
15- IE: functions we expect to be called will have no calls because FanShim
16- receives an entirely different mock object to the one we're validating against.
17-
18- Since conftest.py already does some sys.modules mangling I see no reason not to
19- do the same thing here.
20- """
21- try :
22- del sys .modules [module ]
23- except KeyError :
24- pass
25-
26-
27- def test_setup (GPIO , plasma ):
28- force_reimport ('fanshim' )
29-
30- from fanshim import FanShim
5+ def test_setup (GPIO , apa102 , FanShim ):
316 fanshim = FanShim ()
327
338 GPIO .setwarnings .assert_called_once_with (False )
@@ -37,26 +12,18 @@ def test_setup(GPIO, plasma):
3712 mock .call (fanshim ._pin_button , GPIO .IN , pull_up_down = GPIO .PUD_UP )
3813 ])
3914
40- plasma .legacy .set_clear_on_exit .assert_called_once_with (True )
41- plasma .legacy .set_light_count .assert_called_once_with (1 )
42- plasma .legacy .set_light .assert_called_once_with (0 , 0 , 0 , 0 )
15+ apa102 .APA102 .assert_called_once_with (1 , 15 , 14 , None , brightness = 0.05 )
4316
4417
45- def test_button_disable (GPIO , plasma ):
46- force_reimport ('fanshim' )
47-
48- from fanshim import FanShim
18+ def test_button_disable (GPIO , apa102 , FanShim ):
4919 fanshim = FanShim (disable_button = True )
5020
5121 GPIO .setwarnings .assert_called_once_with (False )
5222 GPIO .setmode .assert_called_once_with (GPIO .BCM )
5323 GPIO .setup .assert_called_once_with (fanshim ._pin_fancontrol , GPIO .OUT )
5424
5525
56- def test_led_disable (GPIO , plasma ):
57- force_reimport ('fanshim' )
58-
59- from fanshim import FanShim
26+ def test_led_disable (GPIO , apa102 , FanShim ):
6027 fanshim = FanShim (disable_led = True )
6128
6229 GPIO .setwarnings .assert_called_once_with (False )
@@ -66,10 +33,6 @@ def test_led_disable(GPIO, plasma):
6633 mock .call (fanshim ._pin_button , GPIO .IN , pull_up_down = GPIO .PUD_UP )
6734 ])
6835
69- plasma .legacy .set_clear_on_exit .assert_not_called ()
70- plasma .legacy .set_light_count .assert_not_called ()
71- plasma .legacy .set_light .assert_not_called ()
36+ assert not apa102 .APA102 .called
7237
7338 fanshim .set_light (0 , 0 , 0 )
74- plasma .legacy .set_light .assert_not_called ()
75- plasma .legacy .show .assert_not_called ()
0 commit comments