
CHAPTER 1. PYTHON BASICS, UNIT TESTING, AND IMAGE PROCESSING
Unit testing means that we test small components (units) of our code independently. When
testing the entire functionality of a system it may be difficult to pinpoint exactly where the problem
is occurring. But if we test small units, we can determine where the problem lies. As a side benefit,
unit testing encourages us to construct our code in well contained functions and classes instead of
large monolithic blocks of code. This makes our code easier to read, maintain and reuse.
For the hands-on exercises in this chapter, it may seem tedious to use unit testing, because all
the functions implemented are very simple. However in later chapters we will be implementing more
complicated functions. For example, we will be implementing the gradient calculation of parameters
in a deep neural network. It is possible to derive or implement an incorrect gradient calculation,
but we can write a test that calculates the gradient numerically and compare it with our function.
If the numeric gradient and our calculated gradient match, then we can be more confident that our
function has no mistakes.
Note that it is also sometimes useful to test manually. A simple Python script can be written for
this purpose. Manual testing can be used in conjunction with automated testing. However if you
find yourself repeatedly testing the same thing manually, it might be useful to write an automated
test for it.
We will use a simple project to introduce automated unit testing in Python. In PyCharm open
the folder test project. In this folder we have two files src.py and test.py. The src.py file
includes the source code and test.py includes testing functions that are used to test the correctness
of our code.
src.py contains the following function that adds two numbers and returns the result:
de f add two numbers ( a , b ) :
r e t u r n a + b
test.py includes a test to insure that if the function is passed 2 and 3 as inputs, the return
value will be 5. For testing we will use the built-in Python package unittest. We use the import
statement to use this package in our Python code. PyCharm is integrated with the unittest
package, so we can visually inspect the test results.
To create a test case we create a class that is a subclass of unittest.TestCase. We can create
different test case classes for different parts of our code. This is useful if we don’t want to run all
of the tests every time. Within the test case class we define several test functions to test different
parts of our code. Note that there can be an optional SetUp function in the test case class that
will be called before each test is run. This can be used to set parameters or initial conditions for
the test. A short introduction about classes and inheritance in Python is available at http://www.
jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/.
Our first test case is defined as follows:
de f t e s t i n t e g e r a d d ( s e l f ) :
s e l f . a s s e r t E q u a l ( add two numbers ( 2 , 3) , 5)
This test will “pass” if the output is correct. The assertEqual will pass if the arguments are
equal, and fail if the arguments are not equal. Note that there are many types of assert statements
found in the unittest package, such as assertTrue and assertFalse.
In PyCharm we can run the tests by right-clicking the test.py name in the left pane and
clicking Run Unittests.... The tests will run and we can see that the test passed in the bottom
pane (Figure 1.9). We can also run the tests using the terminal window by navigating to the folder
where the test.py file is located and typing python -m unittest test at the command prompt.
You can get more information while running the tests by using the verbose -v flag and typing python
-m unittest -v test.
11