File: datadriven.py

package info (click to toggle)
python-openid 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,472 kB
  • ctags: 3,328
  • sloc: python: 16,708; xml: 234; sh: 38; makefile: 30
file content (47 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest
import types

class DataDrivenTestCase(unittest.TestCase):
    cases = []

    def generateCases(cls):
        return cls.cases

    generateCases = classmethod(generateCases)

    def loadTests(cls):
        tests = []
        for case in cls.generateCases():
            if isinstance(case, tuple):
                test = cls(*case)
            elif isinstance(case, dict):
                test = cls(**case)
            else:
                test = cls(case)
            tests.append(test)
        return tests

    loadTests = classmethod(loadTests)

    def __init__(self, description):
        unittest.TestCase.__init__(self, 'runOneTest')
        self.description = description

    def shortDescription(self):
        return '%s for %s' % (self.__class__.__name__, self.description)

def loadTests(module_name):
    loader = unittest.defaultTestLoader
    this_module = __import__(module_name, {}, {}, [None])

    tests = []
    for name in dir(this_module):
        obj = getattr(this_module, name)
        if (isinstance(obj, (type, types.ClassType)) and
            issubclass(obj, unittest.TestCase)):
            if hasattr(obj, 'loadTests'):
                tests.extend(obj.loadTests())
            else:
                tests.append(loader.loadTestsFromTestCase(obj))

    return unittest.TestSuite(tests)