Ticket #3909: 3909.patch

File 3909.patch, 10.5 KB (added by Andrii V. Mishkovskyi, 9 years ago)

Reresubmitted version of the patch

  • twisted/trial/runner.py

    diff --git twisted/trial/runner.py twisted/trial/runner.py
    index 87296ef..fef9b4d 100644
    def _resolveDirectory(fn): 
    120120    return fn
    121121
    122122
     123def _getMethodNameInClass(method):
     124    """
     125    Find the attribute name on the method's class which refers to the method.
     126
     127    For some methods, notably decorators which have not had __name__ set correctly:
     128
     129    getattr(method.im_class, method.__name__) != method
     130    """
     131    if getattr(method.im_class, method.__name__, object()) != method:
     132        for alias in dir(method.im_class):
     133            if getattr(method.im_class, alias, object()) == method:
     134                return alias
     135    return method.__name__
     136
    123137
    124138class DestructiveTestSuite(TestSuite):
    125139    """
    class TestLoader(object): 
    533547        """
    534548        if not isinstance(method, types.MethodType):
    535549            raise TypeError("%r not a method" % (method,))
    536         return self._makeCase(method.im_class, method.__name__)
     550        return self._makeCase(method.im_class, _getMethodNameInClass(method))
    537551
    538552    def _makeCase(self, klass, methodName):
    539553        return klass(methodName)
    class TrialRunner(object): 
    862876            if not result.wasSuccessful():
    863877                break
    864878        return result
    865 
  • twisted/trial/test/sample.py

    diff --git twisted/trial/test/sample.py twisted/trial/test/sample.py
    index a3f9568..d9e1a65 100644
    the names the tests in this module. 
    55
    66import unittest as pyunit
    77from twisted.trial import unittest
     8from twisted.python.util import mergeFunctionMetadata
     9
     10
    811
    912class FooTest(unittest.TestCase):
     13
     14
    1015    def test_foo(self):
    1116        pass
    1217
     18
    1319    def test_bar(self):
    1420        pass
    1521
    1622
     23
     24def bad_decorator(fn):
     25    def name_collision(*args, **kwargs):
     26        return fn(*args, **kwargs)
     27    return name_collision
     28
     29
     30
     31def good_decorator(fn):
     32    def name_collision(*args, **kwargs):
     33        return fn(*args, **kwargs)
     34    return mergeFunctionMetadata(fn, name_collision)
     35
     36
     37
     38class DecorationTest(unittest.TestCase):
     39
     40
     41    def test_bad_decorator(self):
     42        pass
     43    test_bad_decorator = bad_decorator(test_bad_decorator)
     44
     45
     46    def test_good_decorator(self):
     47        pass
     48    test_good_decorator = good_decorator(test_good_decorator)
     49
     50
     51    def renamed_decorator(self):
     52        pass
     53    test_renamed_decorator = good_decorator(renamed_decorator)
     54
     55
     56    def name_collision(self):
     57        # not a test
     58        pass
     59
     60
     61
    1762class PyunitTest(pyunit.TestCase):
     63
     64
    1865    def test_foo(self):
    1966        pass
    2067
     68
    2169    def test_bar(self):
    2270        pass
    2371
    2472
     73
    2574class NotATest(object):
     75
     76
    2677    def test_foo(self):
    2778        pass
    2879
    2980
     81
    3082class AlphabetTest(unittest.TestCase):
     83
     84
    3185    def test_a(self):
    3286        pass
    3387
     88
    3489    def test_b(self):
    3590        pass
    3691
     92
    3793    def test_c(self):
    3894        pass
    39 
    40 
  • twisted/trial/test/test_loader.py

    diff --git twisted/trial/test/test_loader.py twisted/trial/test/test_loader.py
    index 3344fcc..d9307b2 100644
    class FileTest(packages.SysPathManglingTest): 
    181181
    182182
    183183class LoaderTest(packages.SysPathManglingTest):
     184    """
     185    Tests for L{trial.TestLoader}.
     186    """
    184187
    185188    def setUp(self):
    186189        self.loader = runner.TestLoader()
    187190        packages.SysPathManglingTest.setUp(self)
    188191
     192
    189193    def test_sortCases(self):
    190194        import sample
    191195        suite = self.loader.loadClass(sample.AlphabetTest)
    class LoaderTest(packages.SysPathManglingTest): 
    198202        self.failUnlessEqual(newOrder,
    199203                             [test._testMethodName for test in suite._tests])
    200204
     205
    201206    def test_loadMethod(self):
    202207        import sample
    203208        suite = self.loader.loadMethod(sample.FooTest.test_foo)
    204209        self.failUnlessEqual(1, suite.countTestCases())
    205210        self.failUnlessEqual('test_foo', suite._testMethodName)
    206211
     212
    207213    def test_loadFailingMethod(self):
    208214        # test added for issue1353
    209215        import erroneous
    class LoaderTest(packages.SysPathManglingTest): 
    213219        self.failUnlessEqual(result.testsRun, 1)
    214220        self.failUnlessEqual(len(result.failures), 1)
    215221
     222
    216223    def test_loadNonMethod(self):
    217224        import sample
    218225        self.failUnlessRaises(TypeError, self.loader.loadMethod, sample)
    class LoaderTest(packages.SysPathManglingTest): 
    222229        self.failUnlessRaises(TypeError,
    223230                              self.loader.loadMethod, ('foo', 'bar'))
    224231
     232
     233    def test_loadBadDecorator(self):
     234        """
     235        Load a decorated test method for which the decorator has failed to set
     236        the method's __name__ correctly.
     237        """
     238        import sample
     239        suite = self.loader.loadMethod(sample.DecorationTest.test_bad_decorator)
     240        self.assertEqual(1, suite.countTestCases())
     241        self.assertEqual('test_bad_decorator', suite._testMethodName)
     242
     243
     244    def test_loadGoodDecorator(self):
     245        """
     246        Load a decorated test method for which the decorator has set the
     247        method's __name__ correctly.
     248        """
     249        import sample
     250        suite = self.loader.loadMethod(
     251            sample.DecorationTest.test_good_decorator)
     252        self.assertEqual(1, suite.countTestCases())
     253        self.assertEqual('test_good_decorator', suite._testMethodName)
     254
     255
     256    def test_loadRenamedDecorator(self):
     257        """
     258        Load a decorated method which has been copied to a new name inside the
     259        class.  Thus its __name__ and its key in the class's __dict__ no
     260        longer match.
     261        """
     262        import sample
     263        suite = self.loader.loadMethod(
     264            sample.DecorationTest.test_renamed_decorator)
     265        self.assertEqual(1, suite.countTestCases())
     266        self.assertEqual('test_renamed_decorator', suite._testMethodName)
     267
     268
    225269    def test_loadClass(self):
    226270        import sample
    227271        suite = self.loader.loadClass(sample.FooTest)
    class LoaderTest(packages.SysPathManglingTest): 
    239283        self.failUnlessRaises(TypeError,
    240284                              self.loader.loadClass, ('foo', 'bar'))
    241285
     286
    242287    def test_loadNonTestCase(self):
    243288        import sample
    244289        self.failUnlessRaises(ValueError, self.loader.loadClass,
    245290                              sample.NotATest)
    246291
     292
    247293    def test_loadModule(self):
    248294        import sample
    249295        suite = self.loader.loadModule(sample)
    250         self.failUnlessEqual(7, suite.countTestCases())
     296        self.assertEqual(10, suite.countTestCases())
     297
    251298
    252299    def test_loadNonModule(self):
    253300        import sample
    class LoaderTest(packages.SysPathManglingTest): 
    259306        self.failUnlessRaises(TypeError,
    260307                              self.loader.loadModule, ('foo', 'bar'))
    261308
     309
    262310    def test_loadPackage(self):
    263311        import goodpackage
    264312        suite = self.loader.loadPackage(goodpackage)
    265313        self.failUnlessEqual(7, suite.countTestCases())
    266314
     315
    267316    def test_loadNonPackage(self):
    268317        import sample
    269318        self.failUnlessRaises(TypeError,
    class LoaderTest(packages.SysPathManglingTest): 
    274323        self.failUnlessRaises(TypeError,
    275324                              self.loader.loadPackage, ('foo', 'bar'))
    276325
     326
    277327    def test_loadModuleAsPackage(self):
    278328        import sample
    279329        ## XXX -- should this instead raise a ValueError? -- jml
    280330        self.failUnlessRaises(TypeError, self.loader.loadPackage, sample)
    281331
     332
    282333    def test_loadPackageRecursive(self):
    283334        import goodpackage
    284335        suite = self.loader.loadPackage(goodpackage, recurse=True)
    285336        self.failUnlessEqual(14, suite.countTestCases())
    286337
     338
    287339    def test_loadAnythingOnModule(self):
    288340        import sample
    289341        suite = self.loader.loadAnything(sample)
    290342        self.failUnlessEqual(sample.__name__,
    291343                             suite._tests[0]._tests[0].__class__.__module__)
    292344
     345
    293346    def test_loadAnythingOnClass(self):
    294347        import sample
    295348        suite = self.loader.loadAnything(sample.FooTest)
    296349        self.failUnlessEqual(2, suite.countTestCases())
    297350
     351
    298352    def test_loadAnythingOnMethod(self):
    299353        import sample
    300354        suite = self.loader.loadAnything(sample.FooTest.test_foo)
    301355        self.failUnlessEqual(1, suite.countTestCases())
    302356
     357
    303358    def test_loadAnythingOnPackage(self):
    304359        import goodpackage
    305360        suite = self.loader.loadAnything(goodpackage)
    306361        self.failUnless(isinstance(suite, self.loader.suiteFactory))
    307362        self.failUnlessEqual(7, suite.countTestCases())
    308363
     364
    309365    def test_loadAnythingOnPackageRecursive(self):
    310366        import goodpackage
    311367        suite = self.loader.loadAnything(goodpackage, recurse=True)
    312368        self.failUnless(isinstance(suite, self.loader.suiteFactory))
    313369        self.failUnlessEqual(14, suite.countTestCases())
    314370
     371
    315372    def test_loadAnythingOnString(self):
    316373        # the important thing about this test is not the string-iness
    317374        # but the non-handledness.
    318375        self.failUnlessRaises(TypeError,
    319376                              self.loader.loadAnything, "goodpackage")
    320377
     378
    321379    def test_importErrors(self):
    322380        import package
    323381        suite = self.loader.loadPackage(package, recurse=True)
    class LoaderTest(packages.SysPathManglingTest): 
    407465        names2.sort()
    408466        self.assertEqual(names1, names2)
    409467
     468
    410469    def test_loadByNamesDuplicate(self):
    411470        """
    412471        Check that loadByNames ignores duplicate names
    class LoaderTest(packages.SysPathManglingTest): 
    416475        suite2 = self.loader.loadByName(module, True)
    417476        self.assertSuitesEqual(suite1, suite2)
    418477
     478
    419479    def test_loadDifferentNames(self):
    420480        """
    421481        Check that loadByNames loads all the names that it is given
    class PackageOrderingTest(packages.SysPathManglingTest): 
    537597            d = md5(n).hexdigest()
    538598            return d
    539599        self.loadSortedPackages(sillySorter)
    540 
    541 
  • twisted/trial/test/test_runner.py

    diff --git twisted/trial/test/test_runner.py twisted/trial/test/test_runner.py
    index 7aee2b6..e5e34c0 100644
    class TestRunner(unittest.TestCase): 
    299299                               'startTest', 'addSuccess', 'stopTest',
    300300                               'startTest', 'addSuccess', 'stopTest',
    301301                               'startTest', 'addSuccess', 'stopTest',
     302                               'startTest', 'addSuccess', 'stopTest',
     303                               'startTest', 'addSuccess', 'stopTest',
     304                               'startTest', 'addSuccess', 'stopTest',
    302305                               'startTest', 'addSuccess', 'stopTest']
    303306
    304307