Improve Python testing catching exceptions
Catch expected exceptions only
This commit is contained in:
parent
7c34d3828f
commit
64d3617b3c
3 changed files with 35 additions and 35 deletions
|
|
@ -3,14 +3,14 @@ import contract
|
||||||
contract.test_preassert(1, 2)
|
contract.test_preassert(1, 2)
|
||||||
try:
|
try:
|
||||||
contract.test_preassert(-1, 3)
|
contract.test_preassert(-1, 3)
|
||||||
print "Failed! Preassertions are broken"
|
raise Exception("Failed! Preassertions are broken")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
contract.test_postassert(3)
|
contract.test_postassert(3)
|
||||||
try:
|
try:
|
||||||
contract.test_postassert(-3)
|
contract.test_postassert(-3)
|
||||||
print "Failed! Postassertions are broken"
|
raise Exception("Failed! Postassertions are broken")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -18,13 +18,13 @@ contract.test_prepost(2, 3)
|
||||||
contract.test_prepost(5, -4)
|
contract.test_prepost(5, -4)
|
||||||
try:
|
try:
|
||||||
contract.test_prepost(-3, 4)
|
contract.test_prepost(-3, 4)
|
||||||
print "Failed! Preassertions are broken"
|
raise Exception("Failed! Preassertions are broken")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
contract.test_prepost(4, -10)
|
contract.test_prepost(4, -10)
|
||||||
print "Failed! Postassertions are broken"
|
raise Exception("Failed! Postassertions are broken")
|
||||||
|
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
@ -33,14 +33,14 @@ f = contract.Foo()
|
||||||
f.test_preassert(4, 5)
|
f.test_preassert(4, 5)
|
||||||
try:
|
try:
|
||||||
f.test_preassert(-2, 3)
|
f.test_preassert(-2, 3)
|
||||||
print "Failed! Method preassertion."
|
raise Exception("Failed! Method preassertion.")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
f.test_postassert(4)
|
f.test_postassert(4)
|
||||||
try:
|
try:
|
||||||
f.test_postassert(-4)
|
f.test_postassert(-4)
|
||||||
print "Failed! Method postassertion"
|
raise Exception("Failed! Method postassertion")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -48,33 +48,33 @@ f.test_prepost(3, 4)
|
||||||
f.test_prepost(4, -3)
|
f.test_prepost(4, -3)
|
||||||
try:
|
try:
|
||||||
f.test_prepost(-4, 2)
|
f.test_prepost(-4, 2)
|
||||||
print "Failed! Method preassertion."
|
raise Exception("Failed! Method preassertion.")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f.test_prepost(4, -10)
|
f.test_prepost(4, -10)
|
||||||
print "Failed! Method postassertion."
|
raise Exception("Failed! Method postassertion.")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
contract.Foo_stest_prepost(4, 0)
|
contract.Foo_stest_prepost(4, 0)
|
||||||
try:
|
try:
|
||||||
contract.Foo_stest_prepost(-4, 2)
|
contract.Foo_stest_prepost(-4, 2)
|
||||||
print "Failed! Static method preassertion"
|
raise Exception("Failed! Static method preassertion")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
contract.Foo_stest_prepost(4, -10)
|
contract.Foo_stest_prepost(4, -10)
|
||||||
print "Failed! Static method posteassertion"
|
raise Exception("Failed! Static method posteassertion")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
b = contract.Bar()
|
b = contract.Bar()
|
||||||
try:
|
try:
|
||||||
b.test_prepost(2, -4)
|
b.test_prepost(2, -4)
|
||||||
print "Failed! Inherited preassertion."
|
raise Exception("Failed! Inherited preassertion.")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -82,54 +82,54 @@ except RuntimeError:
|
||||||
d = contract.D()
|
d = contract.D()
|
||||||
try:
|
try:
|
||||||
d.foo(-1, 1, 1, 1, 1)
|
d.foo(-1, 1, 1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.foo(1, -1, 1, 1, 1)
|
d.foo(1, -1, 1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.foo(1, 1, -1, 1, 1)
|
d.foo(1, 1, -1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.foo(1, 1, 1, -1, 1)
|
d.foo(1, 1, 1, -1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.foo(1, 1, 1, 1, -1)
|
d.foo(1, 1, 1, 1, -1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
d.bar(-1, 1, 1, 1, 1)
|
d.bar(-1, 1, 1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.bar(1, -1, 1, 1, 1)
|
d.bar(1, -1, 1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.bar(1, 1, -1, 1, 1)
|
d.bar(1, 1, -1, 1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.bar(1, 1, 1, -1, 1)
|
d.bar(1, 1, 1, -1, 1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
d.bar(1, 1, 1, 1, -1)
|
d.bar(1, 1, 1, 1, -1)
|
||||||
print "Failed! Inherited preassertion (D)."
|
raise Exception("Failed! Inherited preassertion (D).")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -137,6 +137,6 @@ except RuntimeError:
|
||||||
my = contract.myClass(1)
|
my = contract.myClass(1)
|
||||||
try:
|
try:
|
||||||
my = contract.myClass(0)
|
my = contract.myClass(0)
|
||||||
print "Failed! constructor preassertion"
|
raise Exception("Failed! constructor preassertion")
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ dc.delete_AA(aa)
|
||||||
try:
|
try:
|
||||||
b = dc.new_B()
|
b = dc.new_B()
|
||||||
print "Whoa. new_BB created."
|
print "Whoa. new_BB created."
|
||||||
except:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_b = dc.delete_B
|
del_b = dc.delete_B
|
||||||
|
|
@ -25,7 +25,7 @@ del_b = dc.delete_B
|
||||||
try:
|
try:
|
||||||
bb = dc.new_BB()
|
bb = dc.new_BB()
|
||||||
print "Whoa. new_BB created."
|
print "Whoa. new_BB created."
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_bb = dc.delete_BB
|
del_bb = dc.delete_BB
|
||||||
|
|
@ -33,7 +33,7 @@ del_bb = dc.delete_BB
|
||||||
try:
|
try:
|
||||||
c = dc.new_C()
|
c = dc.new_C()
|
||||||
print "Whoa. new_C created."
|
print "Whoa. new_C created."
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_c = dc.delete_C
|
del_c = dc.delete_C
|
||||||
|
|
@ -44,7 +44,7 @@ dc.delete_CC(cc)
|
||||||
try:
|
try:
|
||||||
d = dc.new_D()
|
d = dc.new_D()
|
||||||
print "Whoa. new_D created"
|
print "Whoa. new_D created"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_d = dc.delete_D
|
del_d = dc.delete_D
|
||||||
|
|
@ -52,7 +52,7 @@ del_d = dc.delete_D
|
||||||
try:
|
try:
|
||||||
dd = dc.new_DD()
|
dd = dc.new_DD()
|
||||||
print "Whoa. new_DD created"
|
print "Whoa. new_DD created"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
dd = dc.delete_DD
|
dd = dc.delete_DD
|
||||||
|
|
@ -60,7 +60,7 @@ dd = dc.delete_DD
|
||||||
try:
|
try:
|
||||||
ad = dc.new_AD()
|
ad = dc.new_AD()
|
||||||
print "Whoa. new_AD created"
|
print "Whoa. new_AD created"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_ad = dc.delete_AD
|
del_ad = dc.delete_AD
|
||||||
|
|
@ -74,7 +74,7 @@ dc.delete_EE(ee)
|
||||||
try:
|
try:
|
||||||
eb = dc.new_EB()
|
eb = dc.new_EB()
|
||||||
print "Whoa. new_EB created"
|
print "Whoa. new_EB created"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
del_eb = dc.delete_EB
|
del_eb = dc.delete_EB
|
||||||
|
|
|
||||||
|
|
@ -8,35 +8,35 @@ g = Grok(f)
|
||||||
try:
|
try:
|
||||||
x = b.x
|
x = b.x
|
||||||
print "Error! b.x"
|
print "Error! b.x"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = s.x
|
x = s.x
|
||||||
print "Error! s.x"
|
print "Error! s.x"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = g.x
|
x = g.x
|
||||||
print "Error! g.x"
|
print "Error! g.x"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = b.getx()
|
x = b.getx()
|
||||||
print "Error! b.getx()"
|
print "Error! b.getx()"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = s.getx()
|
x = s.getx()
|
||||||
print "Error! s.getx()"
|
print "Error! s.getx()"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
x = g.getx()
|
x = g.getx()
|
||||||
print "Error! g.getx()"
|
print "Error! g.getx()"
|
||||||
except:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue