How to test TestContextDecorator

19 views
Skip to first unread message

Kamil Pluciński

unread,
Aug 3, 2018, 4:22:32 PM8/3/18
to Django users
Hi all ;)
I work with this ticket: 29024. I changed decorate_class function in TextContextDecorator:
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

I have to test it, so I tried something like this:
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)

but I can`t remove exceptions from logs:
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 Exception
Exception

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK


How can I hide it? Or what is good way to test this class?
Regards :D

john-...@martinhome.org.uk

unread,
Aug 3, 2018, 7:19:14 PM8/3/18
to django...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages