Skip to content

Commit 41fdde5

Browse files
committed
differences for PR #12
1 parent e997c48 commit 41fdde5

26 files changed

+713
-1009
lines changed

.DS_Store

-6 KB
Binary file not shown.

04-unit-tests-best-practices.md

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ teaching: 10
44
exercises: 2
55
---
66

7-
:::::::::::::::::::::::::::::::::::::: questions
7+
:::::::::::::::::::::::::::::::::::::: questions
88

99
- What to do about complex functions & tests?
1010
- What are some testing best practices for testing?
@@ -40,7 +40,7 @@ def process_data(data: list, maximum_value: float):
4040
for i in range(len(data_negative_removed)):
4141
if data_negative_removed[i] <= maximum_value:
4242
data_maximum_removed.append(data_negative_removed[i])
43-
43+
4444
# Calculate the mean
4545
mean = sum(data_maximum_removed) / len(data_maximum_removed)
4646

@@ -63,9 +63,17 @@ def test_process_data():
6363

6464
```
6565

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
6767
check each function manually to find the bug. Not very efficient!
6868

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+
6977
## Unit Testing
7078

7179
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
156164
def test_calculate_mean():
157165
# Arrange
158166
data = [1, 2, 3, 4, 5]
159-
167+
160168
# Act
161169
mean = calculate_mean(data)
162-
170+
163171
# Assert
164172
assert mean == 3
165173
```
@@ -190,10 +198,10 @@ Here is an example of the TDD process:
190198
def test_calculate_mean():
191199
# Arrange
192200
data = [1, 2, 3, 4, 5]
193-
201+
194202
# Act
195203
mean = calculate_mean(data)
196-
204+
197205
# Assert
198206
assert mean == 3.5
199207
```
@@ -244,7 +252,7 @@ Random seeds work by setting the initial state of the random number generator.
244252
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.
245253

246254

247-
::::::::::::::::::::::::::::::::::::: challenge
255+
::::::::::::::::::::::::::::::::::::: challenge
248256

249257
## Challenge: Write your own unit tests
250258

@@ -258,35 +266,35 @@ Take this complex function, break it down and write unit tests for it.
258266
import random
259267

260268
def randomly_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,
266274
max_height: int
267275
):
268276
"""Participants is a list of tuples, containing the age and height of each participant
269277
participants = [
270-
{age: 25, height: 180},
271-
{age: 30, height: 170},
272-
{age: 35, height: 160},
278+
{age: 25, height: 180},
279+
{age: 30, height: 170},
280+
{age: 35, height: 160},
273281
]
274282
"""
275-
283+
276284
# Get the indexes to sample
277285
indexes = random.sample(range(len(participants)), sample_size)
278286

279287
# Get the sampled participants
280288
sampled_participants = []
281289
for i in indexes:
282290
sampled_participants.append(participants[i])
283-
291+
284292
# Remove participants that are outside the age range
285293
sampled_participants_age_filtered = []
286294
for participant in sampled_participants:
287295
if participant['age'] >= min_age and participant['age'] <= max_age:
288296
sampled_participants_age_filtered.append(participant)
289-
297+
290298
# Remove participants that are outside the height range
291299
sampled_participants_height_filtered = []
292300
for participant in sampled_participants_age_filtered:
@@ -299,15 +307,15 @@ def randomly_sample_and_filter_participants(
299307
- Create a new file called `test_stats.py` in the `statistics` directory
300308
- Write unit tests for the `randomly_sample_and_filter_participants` function in `test_stats.py`
301309

302-
:::::::::::::::::::::::: solution
310+
:::::::::::::::::::::::: solution
303311

304312
The function can be broken down into smaller functions, each of which can be tested separately:
305313

306314
```python
307315
import random
308316

309317
def sample_participants(
310-
participants: list,
318+
participants: list,
311319
sample_size: int
312320
):
313321
indexes = random.sample(range(len(participants)), sample_size)
@@ -317,8 +325,8 @@ def sample_participants(
317325
return sampled_participants
318326

319327
def filter_participants_by_age(
320-
participants: list,
321-
min_age: int,
328+
participants: list,
329+
min_age: int,
322330
max_age: int
323331
):
324332
filtered_participants = []
@@ -328,8 +336,8 @@ def filter_participants_by_age(
328336
return filtered_participants
329337

330338
def filter_participants_by_height(
331-
participants: list,
332-
min_height: int,
339+
participants: list,
340+
min_height: int,
333341
max_height: int
334342
):
335343
filtered_participants = []
@@ -339,11 +347,11 @@ def filter_participants_by_height(
339347
return filtered_participants
340348

341349
def randomly_sample_and_filter_participants(
342-
participants: list,
343-
sample_size: int,
344-
min_age: int,
345-
max_age: int,
346-
min_height: int,
350+
participants: list,
351+
sample_size: int,
352+
min_age: int,
353+
max_age: int,
354+
min_height: int,
347355
max_height: int
348356
):
349357
sampled_participants = sample_participants(participants, sample_size)
@@ -447,7 +455,7 @@ When time is limited, it's often better to only write tests for the most critica
447455

448456
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.
449457

450-
::::::::::::::::::::::::::::::::::::: keypoints
458+
::::::::::::::::::::::::::::::::::::: keypoints
451459

452460
- Complex functions can be broken down into smaller, testable units.
453461
- Testing each unit separately is called unit testing.

0 commit comments

Comments
 (0)