Add a decorator for specifying the RunTest object to use.

This commit is contained in:
Jonathan Lange
2010-10-17 16:47:33 +01:00
parent 84482c3ebc
commit aec8b4be4f
3 changed files with 24 additions and 2 deletions

View File

@@ -11,6 +11,7 @@ __all__ = [
'MultipleExceptions',
'MultiTestResult',
'PlaceHolder',
'run_tests_with',
'TestCase',
'TestResult',
'TextTestResult',
@@ -33,6 +34,7 @@ from testtools.testcase import (
PlaceHolder,
TestCase,
clone_test_with_new_id,
run_tests_with,
skip,
skipIf,
skipUnless,

View File

@@ -6,10 +6,11 @@ __metaclass__ = type
__all__ = [
'clone_test_with_new_id',
'MultipleExceptions',
'TestCase',
'run_tests_with',
'skip',
'skipIf',
'skipUnless',
'TestCase',
]
import copy
@@ -61,6 +62,13 @@ except ImportError:
"""
def run_tests_with(test_runner):
def decorator(f):
f._run_tests_with = test_runner
return f
return decorator
class MultipleExceptions(Exception):
"""Represents many exceptions raised from some operation.
@@ -100,7 +108,8 @@ class TestCase(unittest.TestCase):
# __details is lazy-initialized so that a constructed-but-not-run
# TestCase is safe to use with clone_test_with_new_id.
self.__details = None
self.__RunTest = runTest
test_method = self._get_test_method()
self.__RunTest = getattr(test_method, '_run_tests_with', runTest)
self.__exception_handlers = []
self.exception_handlers = [
(self.skipException, self._report_skip),

View File

@@ -4,6 +4,7 @@
from testtools import (
ExtendedToOriginalDecorator,
run_tests_with,
RunTest,
TestCase,
TestResult,
@@ -206,6 +207,16 @@ class TestTestCaseSupportForRunTest(TestCase):
from_run_test = case.run(result)
self.assertThat(from_run_test, Is(CustomRunTest.marker))
def test_decorator_for_run_test(self):
class SomeCase(TestCase):
@run_tests_with(CustomRunTest)
def test_foo(self):
pass
result = TestResult()
case = SomeCase('test_foo')
from_run_test = case.run(result)
self.assertThat(from_run_test, Is(CustomRunTest.marker))
def test_suite():
from unittest import TestLoader