From 859a79aa84757716c943580779b05ba4e45a32fd Mon Sep 17 00:00:00 2001 From: Jonathan Lange Date: Sun, 17 Oct 2010 16:19:51 +0100 Subject: [PATCH] Add a class variable, run_tests_with, to allow controlling which runner to run tests with. --- testtools/testcase.py | 6 +++++- testtools/tests/test_runtest.py | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/testtools/testcase.py b/testtools/testcase.py index f387657..2ccde98 100644 --- a/testtools/testcase.py +++ b/testtools/testcase.py @@ -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) diff --git a/testtools/tests/test_runtest.py b/testtools/tests/test_runtest.py index e01213a..f94af1d 100644 --- a/testtools/tests/test_runtest.py +++ b/testtools/tests/test_runtest.py @@ -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():