Extend the decorator to take kwargs and pass them on.
Documentation, and rename to grammatically correct run_test_with.
This commit is contained in:
@@ -11,7 +11,7 @@ __all__ = [
|
|||||||
'MultipleExceptions',
|
'MultipleExceptions',
|
||||||
'MultiTestResult',
|
'MultiTestResult',
|
||||||
'PlaceHolder',
|
'PlaceHolder',
|
||||||
'run_tests_with',
|
'run_test_with',
|
||||||
'TestCase',
|
'TestCase',
|
||||||
'TestResult',
|
'TestResult',
|
||||||
'TextTestResult',
|
'TextTestResult',
|
||||||
@@ -34,7 +34,7 @@ from testtools.testcase import (
|
|||||||
PlaceHolder,
|
PlaceHolder,
|
||||||
TestCase,
|
TestCase,
|
||||||
clone_test_with_new_id,
|
clone_test_with_new_id,
|
||||||
run_tests_with,
|
run_test_with,
|
||||||
skip,
|
skip,
|
||||||
skipIf,
|
skipIf,
|
||||||
skipUnless,
|
skipUnless,
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ __metaclass__ = type
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
'clone_test_with_new_id',
|
'clone_test_with_new_id',
|
||||||
'MultipleExceptions',
|
'MultipleExceptions',
|
||||||
'run_tests_with',
|
'run_test_with',
|
||||||
'skip',
|
'skip',
|
||||||
'skipIf',
|
'skipIf',
|
||||||
'skipUnless',
|
'skipUnless',
|
||||||
@@ -62,9 +62,25 @@ except ImportError:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def run_tests_with(test_runner):
|
def run_test_with(test_runner, **kwargs):
|
||||||
|
"""Decorate a test as using a specific `RunTest`.
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
@run_test_with(CustomRunner, timeout=42)
|
||||||
|
def test_foo(self):
|
||||||
|
self.assertTrue(True)
|
||||||
|
|
||||||
|
:param test_runner: A `RunTest` factory that takes a test case and an
|
||||||
|
optional list of exception handlers. See `RunTest`.
|
||||||
|
:param **kwargs: Keyword arguments to pass on as extra arguments to
|
||||||
|
`test_runner`.
|
||||||
|
:return: A decorator to be used for marking a test as needing a special
|
||||||
|
runner.
|
||||||
|
"""
|
||||||
|
def make_test_runner(case, handlers=None):
|
||||||
|
return test_runner(case, handlers=handlers, **kwargs)
|
||||||
def decorator(f):
|
def decorator(f):
|
||||||
f._run_tests_with = test_runner
|
f._run_test_with = make_test_runner
|
||||||
return f
|
return f
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
@@ -109,7 +125,7 @@ class TestCase(unittest.TestCase):
|
|||||||
# TestCase is safe to use with clone_test_with_new_id.
|
# TestCase is safe to use with clone_test_with_new_id.
|
||||||
self.__details = None
|
self.__details = None
|
||||||
test_method = self._get_test_method()
|
test_method = self._get_test_method()
|
||||||
self.__RunTest = getattr(test_method, '_run_tests_with', runTest)
|
self.__RunTest = getattr(test_method, '_run_test_with', runTest)
|
||||||
self.__exception_handlers = []
|
self.__exception_handlers = []
|
||||||
self.exception_handlers = [
|
self.exception_handlers = [
|
||||||
(self.skipException, self._report_skip),
|
(self.skipException, self._report_skip),
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
from testtools import (
|
from testtools import (
|
||||||
ExtendedToOriginalDecorator,
|
ExtendedToOriginalDecorator,
|
||||||
run_tests_with,
|
run_test_with,
|
||||||
RunTest,
|
RunTest,
|
||||||
TestCase,
|
TestCase,
|
||||||
TestResult,
|
TestResult,
|
||||||
@@ -208,8 +208,9 @@ class TestTestCaseSupportForRunTest(TestCase):
|
|||||||
self.assertThat(from_run_test, Is(CustomRunTest.marker))
|
self.assertThat(from_run_test, Is(CustomRunTest.marker))
|
||||||
|
|
||||||
def test_decorator_for_run_test(self):
|
def test_decorator_for_run_test(self):
|
||||||
|
# Individual test methods can be marked as needing a special runner.
|
||||||
class SomeCase(TestCase):
|
class SomeCase(TestCase):
|
||||||
@run_tests_with(CustomRunTest)
|
@run_test_with(CustomRunTest)
|
||||||
def test_foo(self):
|
def test_foo(self):
|
||||||
pass
|
pass
|
||||||
result = TestResult()
|
result = TestResult()
|
||||||
@@ -217,6 +218,26 @@ class TestTestCaseSupportForRunTest(TestCase):
|
|||||||
from_run_test = case.run(result)
|
from_run_test = case.run(result)
|
||||||
self.assertThat(from_run_test, Is(CustomRunTest.marker))
|
self.assertThat(from_run_test, Is(CustomRunTest.marker))
|
||||||
|
|
||||||
|
def test_extended_decorator_for_run_test(self):
|
||||||
|
# Individual test methods can be marked as needing a special runner.
|
||||||
|
# Extra arguments can be passed to the decorator which will then be
|
||||||
|
# passed on to the RunTest object.
|
||||||
|
marker = object()
|
||||||
|
class FooRunTest(RunTest):
|
||||||
|
def __init__(self, case, handlers=None, bar=None):
|
||||||
|
super(FooRunTest, self).__init__(case, handlers)
|
||||||
|
self.bar = bar
|
||||||
|
def run(self, result=None):
|
||||||
|
return self.bar
|
||||||
|
class SomeCase(TestCase):
|
||||||
|
@run_test_with(FooRunTest, bar=marker)
|
||||||
|
def test_foo(self):
|
||||||
|
pass
|
||||||
|
result = TestResult()
|
||||||
|
case = SomeCase('test_foo')
|
||||||
|
from_run_test = case.run(result)
|
||||||
|
self.assertThat(from_run_test, Is(marker))
|
||||||
|
|
||||||
|
|
||||||
def test_suite():
|
def test_suite():
|
||||||
from unittest import TestLoader
|
from unittest import TestLoader
|
||||||
|
|||||||
Reference in New Issue
Block a user