Figure 5-2 Testing Pyramid
Python has a built-in unit test module, named unittest.
This module is quite full featured and can support a
tremendous number of test cases. There are other testing
modules that you can use, such as Nose and PyTest (who
comes up with these names?), but for the purposes of the
200-901 DevNet Associate DEVASC exam, you need to
know how unittest works. In order to use unittest, you
need a bit of code to test. Here is a simple function that
computes the area of a circle:
from math import pi
def area_of_circle(r):
return pi*(r**2)
You import from the math module the pi method to
make it a little easier. A function is defined with the
name area_of_circle that takes the argument r. The
function computes the radius of a circle and returns the
value. This is very simple, but what happens if the
function is called, and odd values are passed to it? You
guessed it: lots of errors. So in order to test this function,
you can create a unit test.