0 Replies - 143 Views - Last Post: 16 April 2013 - 12:42 PM Rate Topic: -----

#1 Midwest Product  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 05-February 10

Using assertRaises to check a method

Posted 16 April 2013 - 12:42 PM

I know that assertRaises follows the format (exception Class, callable object).

I've used it before to validate that some inputs are correct, for example, if the number for "fingers" from class Hands is negative, then an exception is thrown:

self.assertRaises(ValueError, hands.Hands, fingers = -1)



The test passes, because the exception was thrown!

Fingers is a parameter in the constructor for hands.

My problem now is that I want to check to see if an exception is thrown in another method in class Hand, outside of the constructor. Let's say I have a method called numOfHands(num). If you have some sort of defect or for medical reasons lost a hand, you could have 0, 1, or 2. Anything greater than 2 would yield an exception for "ValueError: too many hands!"

However, I don't think that assertRaises handles methods. If I try this:
handTest = Hands() #creates constructor
self.assertRaises(ValueError, hands.Hands, handTest.numOfHands(3))



I get the traceback "ValueError: too many hands!" printed out, but assertRaises doesn't catch it, and my test fails.

Does anyone know of another test to make this work, or how to make the formatting work?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1