0

I have the following code:

class cutomTests(unittest.TestCase, moduleName.className):

    def test_input_too_large(self):
      '''className.functionName should fail with large input'''
      self.assertRaises(moduleName.OutOfRangeError, self.functionName(4000)

I got the following result:

======================================================================
ERROR: test_input_too_large (__main__.customTests)
className.functionName should fail with large input
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/workspace//ClassNameTests.py", line 37, in test_input_too_large
  self.assertRaises(toRoman.OutOfRangeError, self.answer(4000))
  File "/home/user/workspace/moduleName.py", line 47, in answer
  raise OutOfRangeError()
OutOfRangeError

So the results should be pass right because the exception is raised???

2 Answers 2

2

You need to let assertRaises call the function rather than doing it yourself:

self.assertRaises(OutOfRangeError, self.functionName, 4000)

Right now, you're calling self.functionName and passing its result to assertRaises. Obviously, if self.functionName raises and exception, it won't get caught by assertRaises because assertRaises won't have been called yet at that point :-).

Note that as of python2.7, assertRaises can be used as a context manager which is a lot more convenient:

with self.assertRaises(OutOfRangeError):
    self.functionName(4000)
Sign up to request clarification or add additional context in comments.

Comments

1

No. You should use assertRaises method as context manager.

with self.assertRaises(moduleName.OutOfRangeError):
    self.functionName(4000)

Another usage is to pass function and arguments as next arguments. However it's not that pretty:

self.assertRaises(moduleName.OutOfRangeError, self.functionName, 4000)

1 Comment

what is the difference between the two and what does context manager do

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.