Automating Tests

Automated Testing

Many languages provide frameworks for automated testing:

[List of unit test frameworks]

We will focus on doctest. It is:

A simple example

Version 1 - a simple example

We will do the manual calculations to work out some examples:

fib(0)  = 0
fib(10) = 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
fib(15) = 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
#!/usr/bin/env python3

""" Fibonacci Module """

def fib(n):
   """ Calculates the n-th Fibonacci number """
   a, b = 0, 1
   for i in range(n):
       a, b = b, a+b
   return a

if __name__ == "__main__":
#    if fib(0) == 0 and fib(10) == 55 and fib(50) == 12586269025:
    if fib(0) == 0 and fib(10) == 55 and fib(15) == 610:
        print("fib() test OK")
    else:
        print("fib() test failed")


[Download]

Result

fib() test OK

Version 2 - use doctest

Run it interactively

bash-4.2$ python3
Python 3.3.2 (default, Dec  4 2014, 12:49:00) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from fib1 import fib
>>> fib(0)
0
>>> fib(1)
1
>>> fib(10)
55
>>> fib(50)
12586269025
>>> 
#!/usr/bin/env python3

""" Fibonacci Module """

def fib(n):
   """ Calculates the n-th Fibonacci number 

   >>> fib(0)
   0
   >>> fib(1)
   1
   >>> fib(10)
   55
   >>> fib(50)
   12586269025
   >>> 

   """
   a, b = 0, 1
   for i in range(n):
       a, b = b, a+b
   return a

if __name__ == "__main__":
   import doctest
   doctest.testmod()


[Download]

Result

Everything OK:

 

Error:

**********************************************************************
File "./ex2.py", line 8, in __main__.fib
Failed example:
    fib(0)
Expected:
    0
Got:
    1
**********************************************************************
File "./ex2.py", line 12, in __main__.fib
Failed example:
    fib(10)
Expected:
    55
Got:
    89
**********************************************************************
File "./ex2.py", line 14, in __main__.fib
Failed example:
    fib(50)
Expected:
    12586269025
Got:
    20365011074
**********************************************************************
1 items had failures:
   3 of   4 in __main__.fib
***Test Failed*** 3 failures.
Continue