]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Extended gen_test decorator to pass args and kwargs. 1061/head
authorStefan Tjarks <stefan@demonware.net>
Fri, 16 May 2014 23:03:01 +0000 (16:03 -0700)
committerStefan Tjarks <stefan@demonware.net>
Fri, 16 May 2014 23:03:01 +0000 (16:03 -0700)
A use case might be a decorated test class that passes arguments to each class method, like mock.patch for instance.

tornado/test/testing_test.py
tornado/testing.py

index aabdaced723179626825d1fdda21a482fca294a5..1c8a865070e28ea21486ca0c0346d324e8c16c95 100644 (file)
@@ -198,5 +198,23 @@ class GenTest(AsyncTestCase):
 
         self.finished = True
 
+    def test_with_method_args(self):
+        @gen_test
+        def test_with_args(self, *args):
+            self.assertEqual(args, ('test',))
+            yield gen.Task(self.io_loop.add_callback)
+
+        test_with_args(self, 'test')
+        self.finished = True
+
+    def test_with_method_kwargs(self):
+        @gen_test
+        def test_with_kwargs(self, **kwargs):
+            self.assertDictEqual(kwargs, {'test': 'test'})
+            yield gen.Task(self.io_loop.add_callback)
+
+        test_with_kwargs(self, test='test')
+        self.finished = True
+
 if __name__ == '__main__':
     unittest.main()
index ec6505584ede3312e2bc7d3285df51e55a5cd2df..8967a43a01990605dbeb4faa16d3386a6e5a81fc 100644 (file)
@@ -471,8 +471,8 @@ def gen_test(func=None, timeout=None):
         # This is a good case study arguing for either some sort of
         # extensibility in the gen decorators or cancellation support.
         @functools.wraps(f)
-        def pre_coroutine(self):
-            result = f(self)
+        def pre_coroutine(self, *args, **kwargs):
+            result = f(self, *args, **kwargs)
             if isinstance(result, types.GeneratorType):
                 self._test_generator = result
             else:
@@ -482,10 +482,11 @@ def gen_test(func=None, timeout=None):
         coro = gen.coroutine(pre_coroutine)
 
         @functools.wraps(coro)
-        def post_coroutine(self):
+        def post_coroutine(self, *args, **kwargs):
             try:
                 return self.io_loop.run_sync(
-                    functools.partial(coro, self), timeout=timeout)
+                    functools.partial(coro, self, *args, **kwargs),
+                    timeout=timeout)
             except TimeoutError as e:
                 # run_sync raises an error with an unhelpful traceback.
                 # If we throw it back into the generator the stack trace