Add a class variable, run_tests_with, to allow controlling which runner

to run tests with.
This commit is contained in:
Jonathan Lange
2010-10-17 16:19:51 +01:00
parent 2692fd4757
commit 859a79aa84
2 changed files with 19 additions and 9 deletions

View File

@@ -74,10 +74,14 @@ class TestCase(unittest.TestCase):
:ivar exception_handlers: Exceptions to catch from setUp, runTest and
tearDown. This list is able to be modified at any time and consists of
(exception_class, handler(case, result, exception_value)) pairs.
:cvar run_tests_with: A `RunTest` class to run tests with. Defaults to
`RunTest`.
"""
skipException = TestSkipped
run_tests_with = RunTest
def __init__(self, *args, **kwargs):
"""Construct a TestCase.
@@ -86,7 +90,7 @@ class TestCase(unittest.TestCase):
supplied testtools.runtest.RunTest is used. The instance to be
used is created when run() is invoked, so will be fresh each time.
"""
runTest = kwargs.pop('runTest', RunTest)
runTest = kwargs.pop('runTest', self.run_tests_with)
unittest.TestCase.__init__(self, *args, **kwargs)
self._cleanups = []
self._unique_id_gen = itertools.count(1)

View File

@@ -179,12 +179,10 @@ class TestRunTest(TestCase):
class CustomRunTest(RunTest):
def __init__(self, marker, *args, **kwargs):
super(CustomRunTest, self).__init__(*args, **kwargs)
self._marker = marker
marker = object()
def run(self, result=None):
return self._marker
return self.marker
class TestTestCaseSupportForRunTest(TestCase):
@@ -194,11 +192,19 @@ class TestTestCaseSupportForRunTest(TestCase):
def test_foo(self):
pass
result = TestResult()
marker = object()
case = SomeCase(
'test_foo', runTest=lambda *args: CustomRunTest(marker, *args))
case = SomeCase('test_foo', runTest=CustomRunTest)
from_run_test = case.run(result)
self.assertThat(from_run_test, Is(marker))
self.assertThat(from_run_test, Is(CustomRunTest.marker))
def test_default_is_runTest_class_variable(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():