def run_test(self):
try:
myFunc()
except ExceptionType:
self.fail("myFunc() raised ExceptionType unexpectedly!")
Answer from DGH on Stack Overflowunit testing - Python unittest - opposite of assertRaises? - Stack Overflow
unit testing - Test to check that executing a function does not raise an exception - Software Engineering Stack Exchange
unit testing - How do you test that a Python function throws an exception? - Stack Overflow
unit testing - Pass a Python unittest if an exception isn't raised - Stack Overflow
def run_test(self):
try:
myFunc()
except ExceptionType:
self.fail("myFunc() raised ExceptionType unexpectedly!")
Hi - I want to write a test to establish that an Exception is not raised in a given circumstance.
That's the default assumption -- exceptions are not raised.
If you say nothing else, that's assumed in every single test.
You don't have to actually write an any assertion for that.
Use TestCase.assertRaises from the unittest module, for example:
import mymod
class MyTestCase(unittest.TestCase):
def test1(self):
self.assertRaises(SomeCoolException, mymod.myfunc)
Since Python 2.7 you can use context manager to get ahold of the actual Exception object thrown:
import unittest
def broken_function():
raise Exception('This is broken')
class MyTestCase(unittest.TestCase):
def test(self):
with self.assertRaises(Exception) as context:
broken_function()
self.assertTrue('This is broken' in context.exception)
if __name__ == '__main__':
unittest.main()
assertRaises
In Python 3.5, you have to wrap context.exception in str, otherwise you'll get a TypeError
self.assertTrue('This is broken' in str(context.exception))
If I understand your question correctly, you could do something like this:
def test_does_not_raise_on_valid_input(self):
raised = False
try:
do_something(42)
except:
raised = True
self.assertFalse(raised, 'Exception raised')
...assuming that you have a corresponding test that the correct Exception gets raised on invalid input, of course:
def test_does_raise_on_invalid_input(self):
self.assertRaises(OutOfCheese, do_something, 43)
However, as pointed out in the comments, you need to consider what it is that you are actually testing. It's likely that a test like...
def test_what_is_42(self):
self.assertEquals(do_something(42), 'Meaning of life')
...is better because it tests the desired behaviour of the system and will fail if an exception is raised.
Many of the comments on this page treat errors and failures as equivalent, which they are not. The right solution in my opinion is to explicitly fail the test if the exception is raised. E.g.:
def test_does_not_error(self):
try:
code_under_test()
except ThatException:
self.fail("code_under_test raised ThatException")
In my code, I'm raising an exception if a certain condition is met. However, when writing my unit tests, I'm getting an error that the exception is not raised.
Below is a pseudo example of the code
def API_Endpoint():
if x==y:
raise InvalidValueException()
def api_test(self):
url = api_endpoint
data = {"x":y}
with self.assertRaises(InvalidValueException):
response = self.client.post(url=url, data=data)
I can't figure out why this is failing. if I print the response.json(), it shows
{'type': 'InvalidValueException', 'detail': {'non_field_errors': ['exception message.']}, 'fallback_message': 'exception message.'}
A subsequent test of
self.assertEquals(response.status_code, HTTP_400_BAD_REQUEST)
passes. So how come the with self.assertRaises() is failing?