|
| 1 | +# Lecture 8 |
| 2 | + |
| 3 | +## Lecture 7 concepts |
| 4 | + |
| 5 | +- [What Are Stubs and Drivers?](#what-are-stubs-and-drivers) |
| 6 | +- [Why Use Stubs and Drivers?](#why-use-stubs-and-drivers) |
| 7 | +- [Simple Example](#simple-example) |
| 8 | + - [Stub Example](#stub-example) |
| 9 | + - [Driver Example](#driver-example) |
| 10 | +- [How to Use Them Together](#how-to-use-them-together) |
| 11 | + |
| 12 | +## What Are Stubs and Drivers? |
| 13 | + |
| 14 | +Imagine you're building a puzzle: |
| 15 | +- A **stub** is a fake puzzle piece you use temporarily |
| 16 | +- A **driver** is a simple tool to check if your real pieces fit together |
| 17 | + |
| 18 | +Definitions |
| 19 | +A **stub** is a simplified version that replaces a real lower component so it doesn't need to be full implemented. |
| 20 | +A **driver** is a simple program in a higher system that tests a lower component without fully implementing the higher system. |
| 21 | + |
| 22 | +## Why Use Stubs and Drivers? |
| 23 | + |
| 24 | +1. **Test in parts**: Check each piece of your code separately |
| 25 | +2. **No dependencies**: Test without needing everything to be finished |
| 26 | +3. **Controlled testing**: Create specific test scenarios easily |
| 27 | +4. **Faster development**: Don't need to wait for other parts to be done |
| 28 | + |
| 29 | +This simple approach helps you build and test your code without needing to complete your system! |
| 30 | + |
| 31 | +## Simple Example |
| 32 | + |
| 33 | +Let's say we have a simple traffic light system: |
| 34 | + |
| 35 | +```python |
| 36 | +# traffic_light.py |
| 37 | +class TrafficLight: |
| 38 | + def __init__(self): |
| 39 | + self.color = "red" |
| 40 | + |
| 41 | + def change_color(self): |
| 42 | + if self.color == "red": |
| 43 | + self.color = "green" |
| 44 | + elif self.color == "green": |
| 45 | + self.color = "yellow" |
| 46 | + else: |
| 47 | + self.color = "red" |
| 48 | + |
| 49 | + def get_color(self): |
| 50 | + return self.color |
| 51 | +``` |
| 52 | + |
| 53 | +### Stub Example |
| 54 | + |
| 55 | +Remember a **stub** is a simplified version that replaces a real lower component so it doesn't need to be full implemented. |
| 56 | + |
| 57 | +```python |
| 58 | +# traffic_light_stub.py |
| 59 | +class TrafficLightStub: |
| 60 | + def __init__(self): |
| 61 | + self.color = "red" |
| 62 | + |
| 63 | + def change_color(self): |
| 64 | + # Just pretend to change color - always return green for testing |
| 65 | + self.color = "green" |
| 66 | + |
| 67 | + def get_color(self): |
| 68 | + return self.color |
| 69 | +``` |
| 70 | + |
| 71 | +### Driver Example |
| 72 | + |
| 73 | +Remember a **driver** is a simple program in a higher system that tests a lower component without fully implementing the higher system. |
| 74 | + |
| 75 | +```python |
| 76 | +# traffic_light_driver.py |
| 77 | + |
| 78 | +# Import the real component we want to test |
| 79 | +from traffic_light import TrafficLight |
| 80 | + |
| 81 | +def test_traffic_light(): |
| 82 | + print("Testing Traffic Light...") |
| 83 | + |
| 84 | + # Create the traffic light |
| 85 | + light = TrafficLight() |
| 86 | + |
| 87 | + # Test initial state |
| 88 | + print(f"Initial color: {light.get_color()}") |
| 89 | + |
| 90 | + # Test color change |
| 91 | + light.change_color() |
| 92 | + print(f"After first change: {light.get_color()}") |
| 93 | + |
| 94 | + light.change_color() |
| 95 | + print(f"After second change: {light.get_color()}") |
| 96 | + |
| 97 | + light.change_color() |
| 98 | + print(f"After third change: {light.get_color()}") |
| 99 | + |
| 100 | + print("Test complete!") |
| 101 | + |
| 102 | +# Run the test |
| 103 | +test_traffic_light() |
| 104 | +``` |
| 105 | + |
| 106 | +## How to Use Them Together |
| 107 | + |
| 108 | +You can use the stub to test other parts of your system: |
| 109 | + |
| 110 | +```python |
| 111 | +# car_driver.py |
| 112 | +from traffic_light_stub import TrafficLightStub |
| 113 | + |
| 114 | +def test_car_behavior(): |
| 115 | + print("Testing car behavior at traffic light...") |
| 116 | + |
| 117 | + # Create the stub traffic light |
| 118 | + light = TrafficLightStub() |
| 119 | + |
| 120 | + # Test car behavior |
| 121 | + print(f"Light is {light.get_color()}") |
| 122 | + |
| 123 | + if light.get_color() == "red": |
| 124 | + print("Car stops") |
| 125 | + else: |
| 126 | + print("Car drives") |
| 127 | + |
| 128 | + # Change the light |
| 129 | + light.change_color() |
| 130 | + print(f"Light changed to {light.get_color()}") |
| 131 | + |
| 132 | + if light.get_color() == "red": |
| 133 | + print("Car stops") |
| 134 | + else: |
| 135 | + print("Car drives") |
| 136 | + |
| 137 | + print("Test complete!") |
| 138 | + |
| 139 | +# Run the test |
| 140 | +test_car_behavior() |
| 141 | +``` |
0 commit comments