@@ -22,7 +22,7 @@ Unit tests are so called because they exercise the functionality of the code by
2222interrogating individual functions and methods. Functions and methods can often
2323be considered the atomic units of software because they are indivisible.
2424However, 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
2626more unit-like than long ones.
2727
2828Thus 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
5858observed and expected values. For example, let us test that our mean function
5959successfully 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~~~
6281from mean import *
6382
@@ -70,9 +89,9 @@ def test_ints():
7089{: .python}
7190
7291The 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
7897A 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