Ticket #18551: 18551.diff

File 18551.diff, 3.2 KB (added by Claude Paroz, 12 years ago)

Allow skipUnlessDBFeature / skipIfDBFeature to decorate a class

  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 95e751d..7ca17fa 100644
    a b class TestCase(TransactionTestCase):  
    849849def _deferredSkip(condition, reason):
    850850    def decorator(test_func):
    851851        if not (isinstance(test_func, type) and
    852                 issubclass(test_func, TestCase)):
     852                issubclass(test_func, ut2.TestCase)):
    853853            @wraps(test_func)
    854854            def skip_wrapper(*args, **kwargs):
    855855                if condition():
    856856                    raise ut2.SkipTest(reason)
    857857                return test_func(*args, **kwargs)
    858858            test_item = skip_wrapper
     859            test_item.__unittest_skip_why__ = reason
     860            return test_item
    859861        else:
    860             test_item = test_func
    861         test_item.__unittest_skip_why__ = reason
    862         return test_item
     862            # Assume a class is decorated
     863            if condition():
     864                test_func.__unittest_skip__ = True
     865                test_func.__unittest_skip_why__ = reason
     866            return test_func
    863867    return decorator
    864868
    865869
  • 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