changeset 6556:a9e776515d8d

tests: add global test_context_fixture Move the existing app_test_context_fixture from test_pullrequests.py to conftest.py to make it available to all test modules. It is useful in two cases: 1. there is test setup code (xUnit style) that needs to execute in the same test context as the actual test. 2. even without test setup code, an entire test needs to be executed in a test context. In this case, the fixture just reduces code complexity by not requiring changes in the test code (compared to standard 'with test_context'). It is possible to apply this (or any) fixture to an entire test class using the class decorator @pytest.mark.usefixtures("...") This is similar to 'autouse=True' but can be used even if the fixture is defined elsewhere.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Sun, 15 Jan 2017 20:49:23 +0100
parents 213085032127
children 2f9313074853
files kallithea/tests/conftest.py kallithea/tests/functional/test_pullrequests.py
diffstat 2 files changed, 29 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/conftest.py	Mon Sep 12 17:50:11 2016 +0200
+++ b/kallithea/tests/conftest.py	Sun Jan 15 20:49:23 2017 +0100
@@ -12,6 +12,7 @@
 from kallithea.model.db import Setting, User, UserIpMap
 from kallithea.tests.base import invalidate_all_caches, TEST_USER_REGULAR_LOGIN
 
+from kallithea.tests.test_context import test_context
 
 def pytest_configure():
     path = os.getcwd()
@@ -94,3 +95,30 @@
 
     # IP permissions are cached, need to invalidate this cache explicitly
     invalidate_all_caches()
+
+@pytest.fixture
+def test_context_fixture(app_fixture):
+    """
+    Encompass the entire test using this fixture in a test_context,
+    making sure that certain functionality still works even if no call to
+    self.app.get/post has been made.
+    The typical error message indicating you need a test_context is:
+        TypeError: No object (name: context) has been registered for this thread
+
+    The standard way to fix this is simply using the test_context context
+    manager directly inside your test:
+        with test_context(self.app):
+            <actions>
+    but if test setup code (xUnit-style or pytest fixtures) also needs to be
+    executed inside the test context, that method is not possible.
+    Even if there is no such setup code, the fixture may reduce code complexity
+    if the entire test needs to run inside a test context.
+
+    To apply this fixture (like any other fixture) to all test methods of a
+    class, use the following class decorator:
+        @pytest.mark.usefixtures("test_context_fixture")
+        class TestFoo(TestController):
+            ...
+    """
+    with test_context(app_fixture):
+        yield
--- a/kallithea/tests/functional/test_pullrequests.py	Mon Sep 12 17:50:11 2016 +0200
+++ b/kallithea/tests/functional/test_pullrequests.py	Sun Jan 15 20:49:23 2017 +0100
@@ -208,14 +208,9 @@
                                  status=400)
         response.mustcontain('Invalid reviewer &#34;%s&#34; specified' % invalid_user_id)
 
+@pytest.mark.usefixtures("test_context_fixture") # apply fixture for all test methods
 class TestPullrequestsGetRepoRefs(TestController):
 
-    # this tests need test_context in addition to app_fixture
-    @pytest.fixture(autouse=True)
-    def app_test_context_fixture(self, app_fixture):
-        with test_context(self.app):
-            yield
-
     def setup_method(self, method):
         self.repo_name = u'main'
         repo = fixture.create_repo(self.repo_name, repo_type='hg')