Browse Source

Add `celery_parameters` test fixture to be able to use customized `Celery` init parameters. (#3626)

Steffen Allner 8 years ago
parent
commit
a380f09006
2 changed files with 44 additions and 1 deletions
  1. 21 1
      celery/contrib/pytest.py
  2. 23 0
      docs/userguide/testing.rst

+ 21 - 1
celery/contrib/pytest.py

@@ -13,13 +13,18 @@ NO_WORKER = os.environ.get('NO_WORKER')
 
 
 @contextmanager
-def _create_app(request, enable_logging=False, use_trap=False, **config):
+def _create_app(request,
+                enable_logging=False,
+                use_trap=False,
+                parameters={},
+                **config):
     # type: (Any, **Any) -> Celery
     """Utility context used to setup Celery app for pytest fixtures."""
     test_app = TestApp(
         set_as_current=False,
         enable_logging=enable_logging,
         config=config,
+        **parameters
     )
     # request.module is not defined for session
     _module = getattr(request, 'module', None)
@@ -50,6 +55,7 @@ def use_celery_app_trap():
 @pytest.fixture(scope='session')
 def celery_session_app(request,
                        celery_config,
+                       celery_parameters,
                        celery_enable_logging,
                        use_celery_app_trap):
     # type: (Any) -> Celery
@@ -59,6 +65,7 @@ def celery_session_app(request,
     with _create_app(request,
                      enable_logging=celery_enable_logging,
                      use_trap=use_celery_app_trap,
+                     parameters=celery_parameters,
                      **config) as app:
         if not use_celery_app_trap:
             app.set_default()
@@ -119,9 +126,21 @@ def celery_config():
     return {}
 
 
+@pytest.fixture(scope='session')
+def celery_parameters():
+    # type: () -> Mapping[str, Any]
+    """Redefine this fixture to change the init parameters of test Celery app.
+
+    The dict returned by your fixture will then be used
+    as parameters when instantiating :class:`~celery.Celery`.
+    """
+    return {}
+
+
 @pytest.fixture()
 def celery_app(request,
                celery_config,
+               celery_parameters,
                celery_enable_logging,
                use_celery_app_trap):
     """Fixture creating a Celery application instance."""
@@ -130,6 +149,7 @@ def celery_app(request,
     with _create_app(request,
                      enable_logging=celery_enable_logging,
                      use_trap=use_celery_app_trap,
+                     parameters=celery_parameters,
                      **config) as app:
         yield app
 

+ 23 - 0
docs/userguide/testing.rst

@@ -193,6 +193,29 @@ Example:
             'result_backend': 'rpc',
         }
 
+
+``celery_parameters`` - Override to setup Celery test app parameters.
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+You can redefine this fixture to change the ``__init__`` parameters of test
+Celery app. In contrast to :func:`celery_config`, these are directly passed to
+when instantiating :class:`~celery.Celery`.
+
+The config returned by your fixture will then be used
+to configure the :func:`celery_app`, and :func:`celery_session_app` fixtures.
+
+Example:
+
+.. code-block:: python
+
+    @pytest.fixture(scope='session')
+    def celery_parameters():
+        return {
+            'task_cls':  my.package.MyCustomTaskClass,
+            'strict_typing': False,
+        }
+
+
 ``celery_enable_logging`` - Override to enable logging in embedded workers.
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~