You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mean =sum(data_maximum_removed) /len(data_maximum_removed)
46
46
@@ -63,9 +63,17 @@ def test_process_data():
63
63
64
64
```
65
65
66
-
This test is very complex and hard to debug if it fails. Imagine if the calculation of the mean broke - the test would fail but it would not tell us what part of the function was broken, requiring us to
66
+
This test is hard to debug if it fails. Imagine if the calculation of the mean broke - the test would fail but it would not tell us what part of the function was broken, requiring us to
67
67
check each function manually to find the bug. Not very efficient!
68
68
69
+
:::::::::::::::::::::::::::: callout
70
+
71
+
Asserting that the standard deviation is equal to 16 decimal
72
+
places is also quite error prone. We'll see in a later lesson
73
+
how to improve this test.
74
+
75
+
::::::::::::::::::::::::::::::::::::
76
+
69
77
## Unit Testing
70
78
71
79
The process of unit testing is a fundamental part of software development. It is where you test individual units or components of a software instead of multiple things at once.
@@ -156,10 +164,10 @@ This makes your tests easier to read and understand for both yourself and others
156
164
deftest_calculate_mean():
157
165
# Arrange
158
166
data = [1, 2, 3, 4, 5]
159
-
167
+
160
168
# Act
161
169
mean = calculate_mean(data)
162
-
170
+
163
171
# Assert
164
172
assert mean ==3
165
173
```
@@ -190,10 +198,10 @@ Here is an example of the TDD process:
190
198
deftest_calculate_mean():
191
199
# Arrange
192
200
data = [1, 2, 3, 4, 5]
193
-
201
+
194
202
# Act
195
203
mean = calculate_mean(data)
196
-
204
+
197
205
# Assert
198
206
assert mean ==3.5
199
207
```
@@ -244,7 +252,7 @@ Random seeds work by setting the initial state of the random number generator.
244
252
This means that if you set the seed to the same value, you will get the same sequence of random numbers each time you run the function.
245
253
246
254
247
-
::::::::::::::::::::::::::::::::::::: challenge
255
+
::::::::::::::::::::::::::::::::::::: challenge
248
256
249
257
## Challenge: Write your own unit tests
250
258
@@ -258,35 +266,35 @@ Take this complex function, break it down and write unit tests for it.
258
266
import random
259
267
260
268
defrandomly_sample_and_filter_participants(
261
-
participants: list,
262
-
sample_size: int,
263
-
min_age: int,
264
-
max_age: int,
265
-
min_height: int,
269
+
participants: list,
270
+
sample_size: int,
271
+
min_age: int,
272
+
max_age: int,
273
+
min_height: int,
266
274
max_height: int
267
275
):
268
276
"""Participants is a list of tuples, containing the age and height of each participant
@@ -447,7 +455,7 @@ When time is limited, it's often better to only write tests for the most critica
447
455
448
456
You should discuss with your team how much of the code you think should be tested, and what the most critical parts of the code are in order to prioritize your time.
449
457
450
-
::::::::::::::::::::::::::::::::::::: keypoints
458
+
::::::::::::::::::::::::::::::::::::: keypoints
451
459
452
460
- Complex functions can be broken down into smaller, testable units.
453
461
- Testing each unit separately is called unit testing.
0 commit comments