Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.

Commit 1097ed5

Browse files
authored
Fixed typos and added step to save def in file before import
1 parent 23534b0 commit 1097ed5

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

β€Ž_episodes/04-units.mdβ€Ž

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Unit tests are so called because they exercise the functionality of the code by
2222
interrogating individual functions and methods. Functions and methods can often
2323
be considered the atomic units of software because they are indivisible.
2424
However, what is considered to be the smallest code _unit_ is subjective. The
25-
body of a function can be long are short, and shorter functions are arguably
25+
body of a function can be long or short, and shorter functions are arguably
2626
more unit-like than long ones.
2727

2828
Thus what reasonably constitutes a code unit typically varies from project to
@@ -58,6 +58,25 @@ class. Ultimately, the test occurs when an assertion is made, comparing the
5858
observed and expected values. For example, let us test that our mean function
5959
successfully calculates the known value for a simple list.
6060

61+
Before running the next code, save your `mean` function to a file called mean.py in the working directory.
62+
63+
You can use this code to save to file:
64+
65+
~~~
66+
def mean(num_list):
67+
try:
68+
return sum(num_list)/len(num_list)
69+
except ZeroDivisionError :
70+
return 0
71+
except TypeError as detail :
72+
msg = "The algebraic mean of an non-numerical list is undefined.\
73+
Please provide a list of numbers."
74+
raise TypeError(detail.__str__() + "\n" + msg)
75+
~~~
76+
{: .python}
77+
78+
Now, back in your Jupyter Notebook run the following code:
79+
6180
~~~
6281
from mean import *
6382
@@ -70,9 +89,9 @@ def test_ints():
7089
{: .python}
7190

7291
The test above:
73-
- sets up the input parameters (the simple list [1, 2, 3, 4, 5].
74-
- collects the observed result
75-
- declares the expected result (calculated with our human brain).
92+
- sets up the input parameters (the simple list [1, 2, 3, 4, 5]);
93+
- collects the observed result;
94+
- declares the expected result (calculated with our human brain);
7695
- and compares the two with an assertion.
7796

7897
A unit test suite is made up of many tests just like this one. A single
@@ -118,6 +137,6 @@ def test_complex():
118137
~~~
119138
{: .python}
120139

121-
Use IPython to import the `test_mean` package and run each test.
140+
Use Jupyter Notebook to import the `test_mean` package and run each test.
122141

123-
Well, **that** was tedious.
142+
Well, **that** was tedious.

0 commit comments

Comments
 (0)