[Tutor] Puzzling case of assertRaises not running properly
angela ebirim
cebirim at gmail.com
Fri Dec 30 16:51:46 EST 2016
Hi,
Learning about how to write tests in Python and have a query.
*#bill.py*
class Bill(object):
def tax(self, total):
tax = float(total * 0.05)
return round(tax, 2)
def check_input(self, seat1, app1):
try:
seat1 = float(seat1)
app1 = float(app1)
except ValueError:
print "Not a float"
return (seat1, app1)
def bill(self, seat1, app1):
(seat1, app1) = self.check_input(seat1, app1)
tax = self.tax(seat1 + app1)
total = seat1 + app1 + tax
return (total, tax)
if __name__=='__main__':
bill = Bill()
(total, tax) = bill.bill(2,3)
print("tax $%.2f" % tax)
print("total charge for meal $%.2f" % total)
*#bill_test.py*
class BillTest(unittest.TestCase):
def setUp(self):
self.bill = Bill()
def test_bill_pay_3_4(self):
self.assertEqual((7.35, 0.35), self.bill.bill(3,4))
def test_input_not_float_raises_ValueError(self):
with self.assertRaises(ValueError):
self.bill.check_input("aa","bb")
if __name__=='__main__':
unittest.main()
I'm using Python 2.7.
When I run the test doing python bill_test.py -v, I get the following
result:-
test_bill_pay_3_4 (__main__.BillTest) ... ok
test_input_not_float_raises_ValueError (__main__.BillTest) ... Not a float
FAIL
======================================================================
FAIL: test_input_not_float_raises_ValueError (__main__.BillTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "billa_test.py", line 13, in test_input_not_float_raises_ValueError
self.bill.check_input("aa","bb")
AssertionError: ValueError not raised
----------------------------------------------------------------------
Ran 2 tests in 0.003s
FAILED (failures=1)
Can someone please tell me what I'm doing wrong?
Many thanks
More information about the Tutor
mailing list