Ticket #18551: 18551-2.diff

File 18551-2.diff, 3.5 KB (added by claudep, 3 years ago)

Really defer checking condition for classes

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 95e751d..4362901 100644
    a b class TestCase(TransactionTestCase): 
    846846            transaction.leave_transaction_management(using=db)
    847847
    848848
     849class CheckCondition(object):
     850    """ Descriptor class for deferred condition checking """
     851    def __init__(self, cond_func):
     852        self.cond_func = cond_func
     853    def __get__(self, obj, objtype):
     854        return self.cond_func()
     855
    849856def _deferredSkip(condition, reason):
    850857    def decorator(test_func):
    851858        if not (isinstance(test_func, type) and
    852                 issubclass(test_func, TestCase)):
     859                issubclass(test_func, ut2.TestCase)):
    853860            @wraps(test_func)
    854861            def skip_wrapper(*args, **kwargs):
    855862                if condition():
    856863                    raise ut2.SkipTest(reason)
    857864                return test_func(*args, **kwargs)
    858865            test_item = skip_wrapper
     866            test_item.__unittest_skip_why__ = reason
     867            return test_item
    859868        else:
    860             test_item = test_func
    861         test_item.__unittest_skip_why__ = reason
    862         return test_item
     869            # Assume a class is decorated
     870            test_func.__unittest_skip__ = CheckCondition(condition)
     871            test_func.__unittest_skip_why__ = reason
     872            return test_func
    863873    return decorator
    864874
    865875
  • tests/regressiontests/test_utils/tests.py

    diff --git a/tests/regressiontests/test_utils/tests.py b/tests/regressiontests/test_utils/tests.py
    index 46479eb..f37d94d 100644
    a b  
    11# -*- coding: utf-8 -*-
    22from __future__ import absolute_import, unicode_literals
    33
     4from io import BytesIO
     5
    46from django.forms import EmailField, IntegerField
    57from django.http import HttpResponse
    68from django.template.loader import render_to_string
    7 from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
    8 from django.utils.unittest import skip
     9from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature
     10from django.utils import unittest
    911
    1012from .models import Person
    1113
    class SkippingTestCase(TestCase): 
    2123        self.assertRaises(ValueError, test_func)
    2224
    2325
     26class SkippingClassTestCase(TestCase):
     27    def test_skip_class_unless_db_feature(self):
     28        @skipUnlessDBFeature("__class__")
     29        class NotSkippedTests(unittest.TestCase):
     30            def test_dummy(self):
     31                return
     32
     33        @skipIfDBFeature("__class__")
     34        class SkippedTests(unittest.TestCase):
     35            def test_will_be_skipped(self):
     36                self.fail("We should never arrive here.")
     37
     38        test_suite = unittest.TestSuite()
     39        test_suite.addTest(NotSkippedTests('test_dummy'))
     40        test_suite.addTest(SkippedTests('test_will_be_skipped'))
     41        result = unittest.TextTestRunner(stream=BytesIO()).run(test_suite)
     42        self.assertEqual(result.testsRun, 2)
     43        self.assertEqual(len(result.skipped), 1)
     44
     45
    2446class AssertNumQueriesTests(TestCase):
    2547    urls = 'regressiontests.test_utils.urls'
    2648
    class SkippingExtraTests(TestCase): 
    458480        with self.assertNumQueries(0):
    459481            super(SkippingExtraTests, self).__call__(result)
    460482
    461     @skip("Fixture loading should not be performed for skipped tests.")
     483    @unittest.skip("Fixture loading should not be performed for skipped tests.")
    462484    def test_fixtures_are_skipped(self):
    463485        pass
    464486
Back to Top