def _fail(): raise Exception("foobar")
="foo")
test_fail(_fail, contains
def _fail(): raise Exception()
test_fail(_fail)
Test
Simple test functions
We can check that code raises an exception when that’s expected (test_fail
).
To test for equality or inequality (with different types of things) we define a simple function test
that compares two objects with a given cmp
operator.
test_fail
test_fail (f, msg='', contains='', args=None, kwargs=None)
Fails with msg
unless f()
raises an exception and (optionally) has contains
in e.args
We can also pass args
and kwargs
to function to check if it fails with special inputs.
def _fail_args(a):
if a == 5:
raise ValueError
=(5,))
test_fail(_fail_args, args=dict(a=5)) test_fail(_fail_args, kwargs
test
test (a, b, cmp, cname=None)
assert
that cmp(a,b)
; display inputs and cname or cmp.__name__
if it fails
1,2],[1,2], operator.eq)
test([lambda: test([1,2],[1], operator.eq))
test_fail(1,2],[1], operator.ne)
test([lambda: test([1,2],[1,2], operator.ne)) test_fail(
all_equal
all_equal (a, b)
Compares whether a
and b
are the same length and have the same contents
'abc'], ['abc'], all_equal)
test([lambda: test(['abc'],['cab'], all_equal)) test_fail(
equals
equals (a, b)
Compares a
and b
for equality; supports sublists, tensors and arrays too
'abc'],['a']], [['abc'],['a']], equals)
test([['abc'],['a'],'b', [['x']]], [['abc'],['a'],'b', [['x']]], equals) # supports any depth and nested structure test([[
nequals
nequals (a, b)
Compares a
and b
for not equals
'abc'], ['ab' ], nequals) test([
test_eq test_ne, etc…
Just use test_eq
/test_ne
to test for ==
/!=
. test_eq_type
checks things are equal and of the same type. We define them using test
:
test_eq
test_eq (a, b)
test
that a==b
1,2],[1,2])
test_eq([1,2],map(int,[1,2]))
test_eq([1,2]),array([1,2]))
test_eq(array([1,2]),array([1,2]))
test_eq(array([1,2]),3],[array([1,2]),3])
test_eq([array([dict(a=1,b=2), dict(b=2,a=1))
test_eq(lambda: test_eq([1,2], 1), contains="==")
test_fail(lambda: test_eq(None, np.array([1,2])), contains="==")
test_fail('a', 'b', 'c'}, {'c', 'a', 'b'}) test_eq({
= pd.DataFrame(dict(a=[1,2],b=['a','b']))
df1 = pd.DataFrame(dict(a=[1,2],b=['a','b']))
df2 = pd.DataFrame(dict(a=[1,2],b=['a','c']))
df3
test_eq(df1,df2)
test_eq(df1.a,df2.a)lambda: test_eq(df1,df3), contains='==')
test_fail(class T(pd.Series): pass
0], T(df2.iloc[0])) # works with subclasses test_eq(df1.iloc[
10), torch.zeros(10, dtype=torch.float64))
test_eq(torch.zeros(10), torch.ones(10)-1)
test_eq(torch.zeros(lambda:test_eq(torch.zeros(10), torch.ones(1, 10)), contains='==')
test_fail(3), [0,0,0]) test_eq(torch.zeros(
test_eq_type
test_eq_type (a, b)
test
that a==b
and are same type
1,1)
test_eq_type(lambda: test_eq_type(1,1.))
test_fail(1,1],[1,1])
test_eq_type([lambda: test_eq_type([1,1],(1,1)))
test_fail(lambda: test_eq_type([1,1],[1,1.])) test_fail(
test_ne
test_ne (a, b)
test
that a!=b
1,2],[1])
test_ne([1,2],[1,3])
test_ne([1,2]),array([1,1]))
test_ne(array([1,2]),array([1,1]))
test_ne(array([1,2]),3],[array([1,2])])
test_ne([array([3,4],array([3]))
test_ne([3,4],array([3,5]))
test_ne([dict(a=1,b=2), ['a', 'b'])
test_ne('a', 'b'], dict(a=1,b=2)) test_ne([
is_close
is_close (a, b, eps=1e-05)
Is a
within eps
of b
test_close
test_close (a, b, eps=1e-05)
test
that a
is within eps
of b
1,1.001,eps=1e-2)
test_close(lambda: test_close(1,1.001))
test_fail(-0.001,1.001], [0.,1.], eps=1e-2)
test_close([-0.001,1.001]), np.array([0.,1.]), eps=1e-2)
test_close(np.array([-0.001,1.001]), array([0.,1.]), eps=1e-2) test_close(array([
test_is
test_is (a, b)
test
that a is b
lambda: test_is([1], [1]))
test_fail(= [1]
a
test_is(a, a)= [2]; test_fail(lambda: test_is(a, b)) b
test_shuffled
test_shuffled (a, b)
test
that a
and b
are shuffled versions of the same sequence of items
= list(range(50))
a = copy(a)
b
random.shuffle(b)
test_shuffled(a,b)lambda:test_shuffled(a,a)) test_fail(
= 'abc'
a = 'abcabc'
b lambda:test_shuffled(a,b)) test_fail(
= ['a', 42, True]
a = [42, True, 'a']
b test_shuffled(a,b)
test_stdout
test_stdout (f, exp, regex=False)
Test that f
prints exp
to stdout, optionally checking as regex
lambda: print('hi'), 'hi')
test_stdout(lambda: test_stdout(lambda: print('hi'), 'ho'))
test_fail(lambda: 1+1, '')
test_stdout(lambda: print('hi there!'), r'^hi.*!$', regex=True) test_stdout(
test_warns
test_warns (f, show=False)
lambda: warnings.warn("Oh no!"))
test_warns(lambda: test_warns(lambda: 2+2), contains='No warnings raised') test_fail(
lambda: warnings.warn("Oh no!"), show=True) test_warns(
<class 'UserWarning'>: Oh no!
= Image.open(TEST_IMAGE).resize((128,128)); im im
= Image.open(TEST_IMAGE_BW).resize((128,128)); im im
test_fig_exists
test_fig_exists (ax)
Test there is a figure displayed in ax
= plt.subplots()
fig,ax ; ax.imshow(array(im))
test_fig_exists(ax)
ExceptionExpected
ExceptionExpected (ex=<class 'Exception'>, regex='')
Context manager that tests if an exception is raised
def _tst_1(): assert False, "This is a test"
def _tst_2(): raise SyntaxError
with ExceptionExpected(): _tst_1()
with ExceptionExpected(ex=AssertionError, regex="This is a test"): _tst_1()
with ExceptionExpected(ex=SyntaxError): _tst_2()
exception
is an abbreviation for ExceptionExpected()
.
with exception: _tst_1()