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