Ticket #18551: 18551-3.diff

File 18551-3.diff, 3.3 KB (added by Tim Graham, 11 years ago)
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index d044236..6f3f1c0 100644
    a b class TestCase(TransactionTestCase):  
    879879            self.atomics[db_name].__exit__(None, None, None)
    880880
    881881
     882class CheckCondition(object):
     883    """Descriptor class for deferred condition checking"""
     884    def __init__(self, cond_func):
     885        self.cond_func = cond_func
     886
     887    def __get__(self, obj, objtype):
     888        return self.cond_func()
     889
     890
    882891def _deferredSkip(condition, reason):
    883892    def decorator(test_func):
    884893        if not (isinstance(test_func, type) and
    885                 issubclass(test_func, TestCase)):
     894                issubclass(test_func, unittest.TestCase)):
    886895            @wraps(test_func)
    887896            def skip_wrapper(*args, **kwargs):
    888897                if condition():
    def _deferredSkip(condition, reason):  
    890899                return test_func(*args, **kwargs)
    891900            test_item = skip_wrapper
    892901        else:
     902            # Assume a class is decorated
    893903            test_item = test_func
     904            test_item.__unittest_skip__ = CheckCondition(condition)
    894905        test_item.__unittest_skip_why__ = reason
    895906        return test_item
    896907    return decorator
  • tests/test_utils/tests.py

    diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
    index 2c2bc24..5b54a87 100644
    a b  
    22from __future__ import absolute_import, unicode_literals
    33
    44import unittest
    5 from unittest import skip
    65
    76from django.db import connection
    87from django.forms import EmailField, IntegerField
    98from django.http import HttpResponse
    109from django.template.loader import render_to_string
    11 from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
     10from django.test import SimpleTestCase, TestCase, skipIfDBFeature, skipUnlessDBFeature
    1211from django.test.html import HTMLParseError, parse_html
    1312from django.test.utils import CaptureQueriesContext, IgnoreAllDeprecationWarningsMixin
    1413from django.utils import six
    class SkippingTestCase(TestCase):  
    2726        self.assertRaises(ValueError, test_func)
    2827
    2928
     29class SkippingClassTestCase(TestCase):
     30    def test_skip_class_unless_db_feature(self):
     31        @skipUnlessDBFeature("__class__")
     32        class NotSkippedTests(unittest.TestCase):
     33            def test_dummy(self):
     34                return
     35
     36        @skipIfDBFeature("__class__")
     37        class SkippedTests(unittest.TestCase):
     38            def test_will_be_skipped(self):
     39                self.fail("We should never arrive here.")
     40
     41        test_suite = unittest.TestSuite()
     42        test_suite.addTest(NotSkippedTests('test_dummy'))
     43        test_suite.addTest(SkippedTests('test_will_be_skipped'))
     44        result = unittest.TextTestRunner(stream=six.StringIO()).run(test_suite)
     45        self.assertEqual(result.testsRun, 2)
     46        self.assertEqual(len(result.skipped), 1)
     47
     48
    3049class AssertNumQueriesTests(TestCase):
    3150    urls = 'test_utils.urls'
    3251
    class SkippingExtraTests(TestCase):  
    561580        with self.assertNumQueries(0):
    562581            super(SkippingExtraTests, self).__call__(result)
    563582
    564     @skip("Fixture loading should not be performed for skipped tests.")
     583    @unittest.skip("Fixture loading should not be performed for skipped tests.")
    565584    def test_fixtures_are_skipped(self):
    566585        pass
    567586
Back to Top