A specialization of TestLoader that tags some extra attributes onto test classes as they are loaded.
| 12 | |
| 13 | |
| 14 | class DecoratingLoader(loader.TestLoader): |
| 15 | """ |
| 16 | A specialization of TestLoader that tags some extra attributes |
| 17 | onto test classes as they are loaded. |
| 18 | """ |
| 19 | def __init__(self, params): |
| 20 | self._params = params |
| 21 | super(DecoratingLoader, self).__init__() |
| 22 | |
| 23 | def _apply_params(self, obj): |
| 24 | for k, v in self._params.items(): |
| 25 | if obj.__class__ is type: |
| 26 | cls = obj |
| 27 | else: |
| 28 | cls = obj.__class__ |
| 29 | setattr(cls, k, v) |
| 30 | |
| 31 | def loadTestsFromTestCase(self, testCaseClass): |
| 32 | self._apply_params(testCaseClass) |
| 33 | return super(DecoratingLoader, self).loadTestsFromTestCase(testCaseClass) |
| 34 | |
| 35 | def loadTestsFromName(self, name, module=None): |
| 36 | result = super(DecoratingLoader, self).loadTestsFromName(name, module) |
| 37 | |
| 38 | # Special case for when we were called with the name of a method, we get |
| 39 | # a suite with one TestCase |
| 40 | tests_in_result = list(result) |
| 41 | if len(tests_in_result) == 1 and isinstance(tests_in_result[0], case.TestCase): |
| 42 | self._apply_params(tests_in_result[0]) |
| 43 | |
| 44 | return result |
| 45 | |
| 46 | |
| 47 | class LogStream(object): |