Skip to content

Commit 71de138

Browse files
add unit tests
1 parent f311cff commit 71de138

15 files changed

Lines changed: 236 additions & 41 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"[python]": {
77
"editor.defaultFormatter": "ms-python.black-formatter"
88
},
9-
"editor.fontSize": 18,
9+
"editor.fontSize": 26,
1010
"terminal.integrated.defaultProfile.windows": "Git Bash",
1111
"window.restoreWindows": "preserve",
1212
"workbench.editorAssociations": {

project/lib/led_light.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, pin, flashing=False, debug=False):
1010
self.__debug = debug
1111
self.__pin = pin
1212
self.__flashing = flashing
13-
self._last_toggle_time = time()
13+
self.__last_toggle_time = time()
1414

1515
def on(self):
1616
# method overiding polymorphism of the parent class
@@ -47,6 +47,6 @@ def led_light_state(self, value):
4747
def flash(self):
4848
# Non-blocking flash: toggles LED every 0.5s for the given duration
4949
now = time()
50-
if self.__flashing and now - self._last_toggle_time >= 0.5:
50+
if self.__flashing and now - self.__last_toggle_time >= 0.5:
5151
self.toggle()
52-
self._last_toggle_time = now
52+
self.__last_toggle_time = now

project/py_scripts/v10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Lecture 3 - Abstraction
33
"""
44

5-
from led_light import Led_Light
5+
from project.py_scripts.LL_unit_Test import Led_Light
66
from time import sleep
77

88
red_light = Led_Light(3, True, True)

project/py_scripts/v11.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Lecture 3 - Extend Led_light class
33
"""
44

5-
from led_light import Led_Light
5+
from project.py_scripts.LL_unit_Test import Led_Light
66
from time import sleep
77

88
red_light = Led_Light(3, True, True)

project/py_scripts/v12.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
11
"""
2-
Lecture 3 - Completed Led_light Implementation
2+
Lecture 3 - Unit Test Led_light Implementation
33
"""
44

5-
from led_light import Led_Light
65
from time import sleep
6+
from led_light import Led_Light
77

8-
# Create a Led_Light on GPIO pin 3, with flashing and debug enabled
8+
# Replace 3 with a valid GPIO pin number for your board
99
led = Led_Light(3, flashing=True, debug=True)
1010

11-
# Turn the LED on
11+
print("Testing on()")
1212
led.on()
13+
sleep(0.1)
14+
if led.value() == 1:
15+
print(".on() method passed")
16+
else:
17+
print(".on() method failed")
1318

14-
# Turn the LED off
19+
print("Testing off()")
1520
led.off()
21+
sleep(0.1)
22+
if led.value() == 0:
23+
print(".off() method passed")
24+
else:
25+
print(".off() method failed")
26+
1627

17-
# Toggle the LED state
28+
print("Testing toggle()")
1829
led.toggle()
30+
sleep(0.1)
31+
if led.value() == 1:
32+
print(".toggle() .on() method passed")
33+
else:
34+
print(".toggle() .on() method failed")
35+
36+
led.toggle()
37+
sleep(0.1)
38+
if led.value() == 0:
39+
print(".toggle() .off() method passed")
40+
else:
41+
print(".toggle() .off() method failed")
42+
1943

20-
# Set the LED state using the property (0 = ON, 1 = OFF)
21-
led.led_light_state = 0 # Turns LED ON
22-
led.led_light_state = 1 # Turns LED OFF
44+
print("Testing led_light_state property (getter)")
45+
state = led.led_light_state
46+
sleep(0.1)
47+
if state == led.value():
48+
print(".led_light_state passed")
49+
else:
50+
print(".led_light_state getter failed")
2351

24-
# Non-blocking flash: call repeatedly in your main loop
25-
while True:
26-
led.flash() # Will toggle every 0.5s if flashing is enabled
27-
sleep(0.1)
52+
print("Testing led_light_state property (setter) to 1 (should turn on)")
53+
set1 = led.led_light_state = 1
54+
set0 = led.led_light_state = 0
55+
if set1 == 1 and set0 == 0 :
56+
print(".led_light_state setter passed")
57+
else:
58+
print(".led_light_state setter failed")

project/py_scripts/v13.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
"""
2-
Lecture 4 - Completed Pedestrian_Button Implementation
2+
Lecture 4 - Manually unit test Pedestrian_Button Implementation
33
"""
44

5+
from time import sleep
56
from pedestrian_button import Pedestrian_Button
6-
import time
77

8-
# Create a Pedestrian_Button on GPIO pin 14 with debug enabled
8+
# Replace 22 with the GPIO pin your button is connected to
99
button = Pedestrian_Button(22, debug=True)
1010

11-
while True:
12-
# Check if the button has been pressed (pedestrian waiting)
11+
print("Testing initial button_state (should be False if not pressed)")
12+
initial_state = button.button_state
13+
if initial_state is False:
14+
print("Initial .button_state passed")
15+
else:
16+
print("Initial .button_state failed")
17+
18+
print("Please press and release the button within 5 seconds...")
19+
pressed = False
20+
for _ in range(50):
1321
if button.button_state:
14-
print("Pedestrian button pressed!")
15-
# Reset the waiting state after handling
16-
button.button_state = False
17-
time.sleep(0.1)
22+
pressed = True
23+
break
24+
sleep(0.1)
25+
26+
if pressed:
27+
print("Button press detected: .button_state passed")
28+
else:
29+
print("Button press not detected: .button_state failed")
30+
31+
print("Testing button_state setter (reset to False)")
32+
button.button_state = False
33+
sleep(0.1)
34+
if button.button_state is False:
35+
print(".button_state setter passed")
36+
else:
37+
print(".button_state setter failed")
38+
39+
print("Manual test complete.")

project/py_scripts/v14.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
"""
2-
Lecture 4 - Completed Audio_Notification Implementation
2+
Lecture 4 - Manual Unit Test for Audio_Notification Class
33
"""
44

5+
from time import sleep
56
from audio_notification import Audio_Notification
6-
import time
77

8-
# Create an Audio_Notification on GPIO pin 15 with debug enabled
9-
buzzer = Audio_Notification(27, debug=True)
8+
# Replace 18 with the GPIO pin your buzzer is connected to
9+
buzzer = Audio_Notification(18, debug=True)
1010

11-
while True:
12-
# Sound a warning beep (non-blocking, call repeatedly in your loop)
11+
print("Testing beep()")
12+
buzzer.beep(freq=1000, duration=200)
13+
print("Did you hear a beep? (Check your buzzer)")
14+
15+
print("Testing warning_on() (should beep every ~0.5s for 2 seconds)")
16+
start = buzzer._last_toggle_time
17+
for _ in range(5):
1318
buzzer.warning_on()
19+
sleep(0.5)
20+
21+
print("Testing warning_off() (should silence the buzzer)")
22+
buzzer.warning_off()
23+
print("Buzzer should now be off.")
24+
25+
print("Manual test complete.")

project/py_scripts/v15.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Lecture 5 - Association & Controller States
33
"""
44

5-
from led_light import Led_Light
5+
from project.py_scripts.LL_unit_Test import Led_Light
66
from pedestrian_button import Pedestrian_Button
77
from audio_notification import Audio_Notification
88
from time import sleep, time

project/py_scripts/v99.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Lecture 6 - Completed system with state machine
33
"""
44

5-
from led_light import Led_Light
5+
from project.py_scripts.LL_unit_Test import Led_Light
66
from pedestrian_button import Pedestrian_Button
77
from audio_notification import Audio_Notification
88
from controller import Controller

tutorials/Lecture3.md

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Abstraction
77
- Extend LED functionality
88
- Complete led light class
9+
- Unit test the led light class
910

1011
## Encapsulation
1112
Encapsulation restricts direct access to some of an object's components (such as attributes or methods), meaning the internal representation of the object is hidden from the outside. This is typically achieved by making certain attributes or methods private (i.e., inaccessible from outside the class) and providing public methods (such as getters and setters) to access or modify those private members.
@@ -178,6 +179,67 @@ while True:
178179
- The LED should be wired with an appropriate resistor to the specified GPIO pin.
179180
- The class uses the internal features of the `machine.Pin` class for output control.
180181

182+
### Class Unit Test
183+
184+
```python
185+
from time import sleep
186+
from led_light import Led_Light
187+
188+
# Replace 3 with a valid GPIO pin number for your board
189+
led = Led_Light(3, flashing=True, debug=True)
190+
191+
print("Testing on()")
192+
led.on()
193+
sleep(0.1)
194+
if led.value() == 1:
195+
print(".on() method passed")
196+
else:
197+
print(".on() method failed")
198+
199+
print("Testing off()")
200+
led.off()
201+
sleep(0.1)
202+
if led.value() == 0:
203+
print(".off() method passed")
204+
else:
205+
print(".off() method failed")
206+
207+
208+
print("Testing toggle()")
209+
led.toggle()
210+
sleep(0.1)
211+
if led.value() == 1:
212+
print(".toggle() .on() method passed")
213+
else:
214+
print(".toggle() .on() method failed")
215+
216+
led.toggle()
217+
sleep(0.1)
218+
if led.value() == 0:
219+
print(".toggle() .off() method passed")
220+
else:
221+
print(".toggle() .off() method failed")
222+
223+
224+
print("Testing led_light_state property (getter)")
225+
state = led.led_light_state
226+
sleep(0.1)
227+
if state == led.value():
228+
print(".led_light_state passed")
229+
else:
230+
print(".led_light_state getter failed")
231+
232+
print("Testing led_light_state property (setter) to 1 (should turn on)")
233+
set1 = led.led_light_state = 1
234+
set0 = led.led_light_state = 0
235+
if set1 == 1 and set0 == 0 :
236+
print(".led_light_state setter passed")
237+
else:
238+
print(".led_light_state setter failed")
239+
240+
```
241+
242+
181243
### Class Implementation
182244

183245
```python
@@ -193,7 +255,7 @@ class Led_Light(Pin):
193255
self.__debug = debug
194256
self.__pin = pin
195257
self.__flashing = flashing
196-
self._last_toggle_time = time()
258+
self.__last_toggle_time = time()
197259

198260
def on(self):
199261
# method overiding polymorphism of the parent class
@@ -230,7 +292,7 @@ class Led_Light(Pin):
230292
def flash(self):
231293
# Non-blocking flash: toggles LED every 0.5s for the given duration
232294
now = time()
233-
if self.__flashing and now - self._last_toggle_time >= 0.5:
295+
if self.__flashing and now - self.__last_toggle_time >= 0.5:
234296
self.toggle()
235-
self._last_toggle_time = now
236-
```
297+
self.__last_toggle_time = now
298+
```

0 commit comments

Comments
 (0)