This example uses "tappy" which has not been integrated into Anaconda yet. It can be installed with the following command:
pip install tap.py or %install_ext ...
Reproducibility is the key
assert, doctest, and unit testslogging, unittest, and nose
In [ ]:
while True print 'Hello world'
In [ ]:
1/0
In [ ]:
factorial
In [ ]:
'1'+1
In [ ]:
try:
file=open('test.txt')
except IOError:
print 'No such file'
In [ ]:
def newfunction():
raise NotImplementedError
newfunction()
In [ ]:
def bar(y):
return foo(1-y)
def foo(x):
print "test and",
if x==0.0:
print "branch..."
return float('Inf')
else:
return 1/x
bar(1)
bar(1.0)
In [ ]:
def foo(x):
try:
print "test and",
return 1/x
except ZeroDivisionError:
print "trap exception..."
return float('Inf')
bar(1)
bar(1.0)
In [ ]:
#i=raw_input("Please enter an integer: ")
i="123"
if not isinstance(i,int):
print "Casting ", i, " to integer."
i=int(i)
else:
print "already an int:",i
In [ ]:
i=8
if i%3 == 0:
print 1
elif i%3 == 1:
print 2
else:
assert i%3 == 2
print 3
In [ ]:
%%file myfactorial.py
def factorial2(n):
""" Details to come ...
"""
raise NotImplementedError
def test():
from math import factorial
for x in range(10):
print ".",
assert factorial2(x) == factorial(x), \
"My factorial function is incorrect for n = %i" % x
In [ ]:
import myfactorial
myfactorial.test()
Looks like we will have to implement our function, if we want to make any progress...
In [ ]:
%%file myfactorial.py
def factorial2(n):
""" Details to come ...
"""
if n == 0:
return 1
else:
return n*factorial2(n-1)
def test():
from math import factorial
for x in range(10):
assert factorial2(x) == factorial(x), \
"My factorial function is incorrect for n = %i" % x
In [ ]:
reload(myfactorial)
myfactorial.test()
In [ ]:
%%file myfactorial.py
def factorial2(n):
""" Find n!. Raise an AssertionError if n is negative or non-integral.
"""
assert n>=0. and type(n) is int, "Unrecognized input"
if n == 0:
return 1
else:
return n*factorial2(n-1)
def test():
from math import factorial
for x in range(10):
assert factorial2(x) == factorial(x), \
"My factorial function is incorrect for n = %i" % x
In [ ]:
from myfactorial import factorial2
[factorial2(n) for n in range(5)]
unittest and nosenoseteststest in a module
beginning with test
In [ ]:
import scipy.integrate
scipy.integrate.test()
Mathematically
$ x = (\sqrt(x))^2$.
So what is happening here:
In [ ]:
import math
assert 2 == math.sqrt(2)**2
In [ ]:
math.sqrt(2)**2
In [ ]:
import numpy as np
np.testing.assert_almost_equal(2, math.sqrt(2)**2)
In [ ]:
x=1.000001
y=1.000002
np.testing.assert_almost_equal(x,y, decimal=5)
What if we consider x and y almost equal? Can we modify our assertion?
In [ ]:
np.testing.assert_almost_equal?
a simple TAP example:
In [ ]:
!nosetests --with-tap myTAP.py
In [ ]:
!cat FunctionTestCase.tap
In [ ]:
!tappy FunctionTestCase.tap
TAP is language agnostic. How would you integrate testing against a project with Fortran, C, and Python as well as Bash scripts?
Test Summary Report
t/iterators.t (Tests: 92 Failed: 8)
Failed tests: 7-13, 15
t/nofork-mux.t (Tests: 6 Failed: 0)
t/regression.t (Tests: 4794 Failed: 103)
Failed tests: 2, 5, 31, 34, 58, 61, 85, 88, 114, 118, 145-146, 171-172, 200-201, 226-227, 252, 255, 278-279, 308, 312, 338, 342, 368-369, 395-396, 422, 425, 452, 454-455, 481, 484, 509-510, 538-539, 563, 567, 593, 597, 623, 627, 653, 657, 683-684, 686, 690, 716, 720, 746, 749, 775-776, 803-804, 831-832, 835-837, 866, 870, 896-897, 923-924, 926-927, 929, 955, 958, 984, 987, 1013-1014, 1040, 1043, 1069, 1073, 1099, 1102, 1126-1127, 1129, 1133, 1159, 1163, 1189-1190, 1192, 1196, 1222-1223, 1226-1227, 1253, 1257
Plans=47 Tests=9370
Result: FAIL
In [8]:
%%file bats_tap_ex
@test "no arguments prints usage instructions" {
run bats
[ $status -eq 1 ]
[ $(expr "${lines[1]}" : "Usage:") -ne 0 ]
}
@test "-v and --version print version number" {
run bats -v
[ $status -eq 0 ]
[ $(expr "$output" : "Bats [0-9][0-9.]*") -ne 0 ]
}
In [9]:
!bats bats_tap_ex --tap
Assignment #1: Write a simple test and run it through nose
Assignment #2: Pick a language (any language) and write a function or subroutine "ok" which takes the following parameters:
and auto-increments test_num.
Now write a couple of tests (1==1, 1==0,...)
Add a "plan" -- ie "1.." followed by the number of tests you ran.