def setUp(inner_self):
context = self.enable()
if self.attr_name:
setattr(inner_self, self.attr_name, context)
try:
decorated_setUp(inner_self)
except:
self.disable()
raise Exception
class TestContextDecoratorImplementation(TestContextDecorator):
some_var = False
def enable(self):
self.__class__.some_var = True
def disable(self):
self.__class__.some_var = False
class TestContextDecoratorTests(SimpleTestCase):
def test_disable_method_after_exception(self):
@TestContextDecoratorImplementation()
class TestWithException(unittest.TestCase):
def setUp(self):
raise Exception("HIDE THIS")
def test_var_is_true(self):
self.assertTrue(TestContextDecoratorImplementation.some_var)
class SimpleTest(unittest.TestCase):
def test_var_is_false(self):
self.assertFalse(TestContextDecoratorImplementation.some_var)
runTest = lambda test: unittest.TextTestRunner().run(test)
result = runTest(TestWithException('test_var_is_true'))
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.errors), 1)
result = runTest(SimpleTest('test_var_is_false'))
self.assertEqual(result.testsRun, 1)
self.assertEqual(len(result.failures), 0)
System check identified no issues (0 silenced).E======================================================================ERROR: test_var_is_true (test_utils.tests.TestContextDecoratorTests.test_disable_method_after_exception.<locals>.TestWithException)----------------------------------------------------------------------Traceback (most recent call last): File "/home/pluto/git/django/django/django/test/utils.py", line 351, in setUp decorated_setUp(inner_self) File "/home/pluto/git/django/django/tests/test_utils/tests.py", line 1244, in setUp raise Exception("HIDE THIS")Exception: HIDE THIS
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/home/pluto/git/django/django/django/test/utils.py", line 354, in setUp raise ExceptionException
----------------------------------------------------------------------Ran 1 test in 0.000s
FAILED (errors=1).----------------------------------------------------------------------Ran 1 test in 0.000s
OK.----------------------------------------------------------------------Ran 1 test in 0.001s
OK
Kamil,
Maybe I am missing something, but you can test for exceptions like this:
class TestSomething(unittest.TestCase):
def test_something_that_raises_an_exception(self):
with self.assertRaises(Exception):
# Do something that is supposed to raise an exception
John--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69e80b65-c604-45cf-b48b-e46200d56780%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.