[Tutor] Puzzling case of assertRaises not running properly
Martin A. Brown
martin at linux-ip.net
Fri Dec 30 20:32:31 EST 2016
Hello,
Overall, I must say.... good work and your question was clear.
>Learning about how to write tests in Python and have a query.
Excellent!
I will snip everything except where I think you should look...
def check_input(self, seat1, app1):
try:
seat1 = float(seat1)
app1 = float(app1)
except ValueError:
print "Not a float"
return (seat1, app1)
You have a try / except block here and your test is deliberately
attempting to trigger the ValueError. That is good.
But, in the except handling, you simply print something and you do
not re-raise the error. So, Python thinks you have caught the
Exception, done what you needed and will continue program flow.
If you wish to propagate the exception (instead of catching it and
handling it), you could consider something like the following:
def check_input(self, seat1, app1):
try:
seat1 = float(seat1)
app1 = float(app1)
except ValueError:
print "Not a float"
raise
return (seat1, app1)
[I will observe that in your __name__ == '__main__' code, you are
using the print_function, but in your except handling, you are using
the print statement. You probably want to pick one. And, I would
recommend the print_function--something that seems you already have
a handle on.]
>if __name__=='__main__':
> bill = Bill()
> (total, tax) = bill.bill(2,3)
> print("tax $%.2f" % tax)
> print("total charge for meal $%.2f" % total)
>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
Good luck!
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list