Skip to content

Commit 08ea892

Browse files
author
Matthew West
committed
Add cleanup before each test, to avoid cascading failures due to already-setup pins
Reformat file
1 parent 472e2f9 commit 08ea892

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

test/test_pwm_setup.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import pytest
1+
import glob
22
import os
33
import platform
4-
import glob
54

65
import Adafruit_BBIO.PWM as PWM
6+
import pytest
77

88
kernel = platform.release()
99

@@ -14,6 +14,7 @@ def teardown_module(module):
1414

1515
class TestPwmSetup:
1616
def test_start_pwm(self):
17+
PWM.cleanup()
1718
PWM.start("P9_14", 0)
1819

1920
if kernel >= '4.1.0':
@@ -29,9 +30,9 @@ def test_start_pwm(self):
2930
pwm_dir = results[0]
3031
else:
3132
files = os.listdir('/sys/devices')
32-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
33+
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
3334
files = os.listdir(ocp)
34-
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
35+
pwm_dir = ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
3536

3637
assert os.path.exists(pwm_dir)
3738
if kernel >= '4.1.0':
@@ -44,15 +45,16 @@ def test_start_pwm(self):
4445
PWM.cleanup()
4546

4647
def test_start_pwm_with_polarity_one(self):
48+
PWM.cleanup()
4749
PWM.start("P9_14", 0, 2000, 1)
4850

4951
if kernel >= '4.1.0':
5052
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
5153
else:
5254
files = os.listdir('/sys/devices')
53-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
55+
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
5456
files = os.listdir(ocp)
55-
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
57+
pwm_dir = ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
5658

5759
assert os.path.exists(pwm_dir)
5860
if kernel >= '4.1.0':
@@ -71,15 +73,16 @@ def test_start_pwm_with_polarity_one(self):
7173
PWM.cleanup()
7274

7375
def test_start_pwm_with_polarity_default(self):
76+
PWM.cleanup()
7477
PWM.start("P9_14", 0, 2000)
7578

7679
if kernel >= '4.1.0':
7780
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
7881
else:
7982
files = os.listdir('/sys/devices')
80-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
83+
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
8184
files = os.listdir(ocp)
82-
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
85+
pwm_dir = ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
8386

8487
assert os.path.exists(pwm_dir)
8588
if kernel >= '4.1.0':
@@ -98,15 +101,16 @@ def test_start_pwm_with_polarity_default(self):
98101
PWM.cleanup()
99102

100103
def test_start_pwm_with_polarity_zero(self):
104+
PWM.cleanup()
101105
PWM.start("P9_14", 0, 2000, 0)
102106

103107
if kernel >= '4.1.0':
104108
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
105109
else:
106110
files = os.listdir('/sys/devices')
107-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
111+
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
108112
files = os.listdir(ocp)
109-
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
113+
pwm_dir = ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
110114

111115
assert os.path.exists(pwm_dir)
112116
if kernel >= '4.1.0':
@@ -125,61 +129,73 @@ def test_start_pwm_with_polarity_zero(self):
125129
PWM.cleanup()
126130

127131
def test_pwm_start_invalid_pwm_key(self):
132+
PWM.cleanup()
128133
with pytest.raises(ValueError):
129134
PWM.start("P8_25", -1)
130135

131136
def test_pwm_start_invalid_duty_cycle_negative(self):
137+
PWM.cleanup()
132138
with pytest.raises(ValueError):
133139
PWM.start("P9_14", -1)
134140

135141
def test_pwm_start_valid_duty_cycle_min(self):
142+
PWM.cleanup()
136143
# testing an exception isn't thrown
137144
PWM.start("P9_14", 0)
138145
PWM.cleanup()
139146

140147
def test_pwm_start_valid_duty_cycle_max(self):
148+
PWM.cleanup()
141149
# testing an exception isn't thrown
142150
PWM.start("P9_14", 100)
143151
PWM.cleanup()
144152

145153
def test_pwm_start_invalid_duty_cycle_high(self):
154+
PWM.cleanup()
146155
with pytest.raises(ValueError):
147156
PWM.start("P9_14", 101)
148157

149158
def test_pwm_start_invalid_duty_cycle_string(self):
159+
PWM.cleanup()
150160
with pytest.raises(TypeError):
151161
PWM.start("P9_14", "1")
152162

153163
def test_pwm_start_invalid_frequency_negative(self):
164+
PWM.cleanup()
154165
with pytest.raises(ValueError):
155166
PWM.start("P9_14", 0, -1)
156167

157168
def test_pwm_start_invalid_frequency_string(self):
169+
PWM.cleanup()
158170
with pytest.raises(TypeError):
159171
PWM.start("P9_14", 0, "1")
160172

161173
def test_pwm_start_negative_polarity(self):
174+
PWM.cleanup()
162175
with pytest.raises(ValueError):
163176
PWM.start("P9_14", 0, 100, -1)
164177

165178
def test_pwm_start_invalid_positive_polarity(self):
179+
PWM.cleanup()
166180
with pytest.raises(ValueError):
167181
PWM.start("P9_14", 0, 100, 2)
168182

169183
def test_pwm_start_invalid_polarity_type(self):
184+
PWM.cleanup()
170185
with pytest.raises(TypeError):
171186
PWM.start("P9_14", 0, 100, "1")
172187

173188
def test_pwm_duty_modified(self):
189+
PWM.cleanup()
174190
PWM.start("P9_14", 0)
175191

176192
if kernel >= '4.1.0':
177193
pwm_dir = "/sys/devices/platform/ocp/48302000.epwmss/48302200.ehrpwm/pwm/pwmchip2/pwm0"
178194
else:
179195
files = os.listdir('/sys/devices')
180-
ocp = '/sys/devices/'+[s for s in files if s.startswith('ocp')][0]
196+
ocp = '/sys/devices/' + [s for s in files if s.startswith('ocp')][0]
181197
files = os.listdir(ocp)
182-
pwm_dir = ocp+'/'+[s for s in files if s.startswith('pwm_test_P9_14')][0]
198+
pwm_dir = ocp + '/' + [s for s in files if s.startswith('pwm_test_P9_14')][0]
183199

184200
assert os.path.exists(pwm_dir)
185201
if kernel >= '4.1.0':
@@ -201,60 +217,69 @@ def test_pwm_duty_modified(self):
201217
PWM.cleanup()
202218

203219
def test_pwm_duty_cycle_non_setup_key(self):
220+
PWM.cleanup()
204221
with pytest.raises(RuntimeError):
205222
PWM.set_duty_cycle("P9_14", 100)
206223
PWM.cleanup()
207224

208225
def test_pwm_duty_cycle_invalid_key(self):
226+
PWM.cleanup()
209227
with pytest.raises(ValueError):
210228
PWM.set_duty_cycle("P9_15", 100)
211229
PWM.cleanup()
212230

213231
def test_pwm_duty_cycle_invalid_value_high(self):
232+
PWM.cleanup()
214233
PWM.start("P9_14", 0)
215234
with pytest.raises(ValueError):
216235
PWM.set_duty_cycle("P9_14", 101)
217236
PWM.cleanup()
218237

219238
def test_pwm_duty_cycle_invalid_value_negative(self):
239+
PWM.cleanup()
220240
PWM.start("P9_14", 0)
221241
with pytest.raises(ValueError):
222242
PWM.set_duty_cycle("P9_14", -1)
223243
PWM.cleanup()
224244

225245
def test_pwm_duty_cycle_invalid_value_string(self):
246+
PWM.cleanup()
226247
PWM.start("P9_14", 0)
227248
with pytest.raises(TypeError):
228249
PWM.set_duty_cycle("P9_14", "a")
229250
PWM.cleanup()
230251

231252
def test_pwm_frequency_invalid_value_negative(self):
253+
PWM.cleanup()
232254
PWM.start("P9_14", 0)
233255
with pytest.raises(ValueError):
234256
PWM.set_frequency("P9_14", -1)
235257
PWM.cleanup()
236258

237259
def test_pwm_frequency_invalid_value_string(self):
260+
PWM.cleanup()
238261
PWM.start("P9_14", 0)
239262
with pytest.raises(TypeError):
240263
PWM.set_frequency("P9_14", "11")
241264
PWM.cleanup()
242265

243266
def test_pwm_freq_non_setup_key(self):
267+
PWM.cleanup()
244268
with pytest.raises(RuntimeError):
245269
PWM.set_frequency("P9_14", 100)
246270
PWM.cleanup()
247271

248272
def test_pwm_freq_non_setup_invalid_key(self):
273+
PWM.cleanup()
249274
with pytest.raises(ValueError):
250275
PWM.set_frequency("P9_15", 100)
251-
PWM.cleanup()
276+
PWM.cleanup()
252277

253278
def test_stop_pwm(self):
254279
pass
255280
# PWM.start("P9_14", 1)
256281
# PWM.stop("P9_14")
257282
# assert os.path.exists('/sys/class/gpio/gpio68')
258283
# direction = open('/sys/class/gpio/gpio68/direction').read()
259-
# assert direction == 'out\n'
284+
# assert direction == 'out\n'
260285
# PWM.cleanup()

0 commit comments

Comments
 (0)