1- import sys
2- import mock
31import pytest
42
53
6- def force_reimport (module ):
7- """Force the module under test to be re-imported.
8-
9- Because pytest runs all tests within the same scope (this makes me cry)
10- we have to do some manual housekeeping to avoid tests polluting each other.
11-
12- Since conftest.py already does some sys.modules mangling I see no reason not to
13- do the same thing here.
14- """
15- if "." in module :
16- steps = module .split ("." )
17- else :
18- steps = [module ]
19-
20- for i in range (len (steps )):
21- module = "." .join (steps [0 :i + 1 ])
22- try :
23- del sys .modules [module ]
24- except KeyError :
25- pass
26-
27-
284def test_noise_setup (sounddevice , numpy ):
29- force_reimport ('enviroplus.noise' )
305 from enviroplus .noise import Noise
316
327 noise = Noise (sample_rate = 16000 , duration = 0.1 )
338 del noise
349
3510
3611def test_noise_get_amplitudes_at_frequency_ranges (sounddevice , numpy ):
37- # Ippity zippidy what is this farce
38- # a curious function that makes my tests pass?
39- force_reimport ('enviroplus.noise' )
4012 from enviroplus .noise import Noise
4113
4214 noise = Noise (sample_rate = 16000 , duration = 0.1 )
@@ -49,11 +21,10 @@ def test_noise_get_amplitudes_at_frequency_ranges(sounddevice, numpy):
4921
5022
5123def test_noise_get_noise_profile (sounddevice , numpy ):
52- # Ippity zippidy what is this farce
53- # a curious function that makes my tests pass?
54- force_reimport ('enviroplus.noise' )
5524 from enviroplus .noise import Noise
5625
26+ numpy .mean .return_value = 10.0
27+
5728 noise = Noise (sample_rate = 16000 , duration = 0.1 )
5829 amp_low , amp_mid , amp_high , amp_total = noise .get_noise_profile (
5930 noise_floor = 100 ,
@@ -63,11 +34,10 @@ def test_noise_get_noise_profile(sounddevice, numpy):
6334
6435 sounddevice .rec .assert_called_with (0.1 * 16000 , samplerate = 16000 , blocking = True , channels = 1 , dtype = 'float64' )
6536
37+ assert amp_total == 10.0
38+
6639
6740def test_get_amplitude_at_frequency_range (sounddevice , numpy ):
68- # Ippity zippidy what is this farce
69- # a curious function that makes my tests pass?
70- force_reimport ('enviroplus.noise' )
7141 from enviroplus .noise import Noise
7242
7343 noise = Noise (sample_rate = 16000 , duration = 0.1 )
0 commit comments