Commits

danjac  committed 6025cb4

Testing fixes

  • Participants
  • Parent commits e72aa4b
  • Branches alfajor-integration

Comments (0)

Files changed (2)

File flaskext/testing.py

 
 class _TestContext(object):
 
-    def __init__(self):
+    def __init__(self, app):
 
         self.app = app
         
         #self.browser.configure_in_scope()
     
         self.client = self.app.test_client()
-        self.fixtures = self._GlobalFixtures()
+        self.fixtures = _GlobalFixtures()
 
     def push(self):
 
         if tb is None:
             self.pop()
 
-
            
 class TestCase(unittest.TestCase):
     
-    def create_app(self):
-        """
-        Create your Flask app here, with any
-        configuration you need.
-        """
-        raise NotImplementedError
-
     def __call__(self, result=None):
         """
         Does the required setup, doing it here
         @functools.wraps(func)
         def decorate():
 
+            app = self.create_app()
+
             _app_ctx = None
             _test_ctx = None
 
             def _setup():
-                
-                app = self.create_app()
+                global _app_ctx, _test_ctx
 
                 _app_ctx = app.test_request_context()
                 _app_ctx.push()
                 _test_ctx = _TestContext(app)
                 _test_ctx.push()
 
-                for _func in self._setup_funcs:
-                    _func()
+                for setup_func in self._setup_funcs:
+                    setup_func()
 
             def _teardown():
+                global _app_ctx, _test_ctx
                 
-                for _func in self._teardown_funcs:
-                    _func()
+                for teardown_func in self._teardown_funcs:
+                    teardown_func()
 
                 _test_ctx.pop()
                 _app_ctx.pop()
                 
-            @with_setup(_setup, _teardown)
-            def _wrapper():
+            _setup()
+            try:
                 func()
-            return _wrapper
-
+            finally:
+                _teardown()
+                
         return decorate
  

File tests/__init__.py

 
     return app
 
+testing = Testing(create_app())
+
+@testing.with_context
+def test_is_current_app():
+    assert current_app
+
+@testing.with_context
+def test_index():
+    response = client.get("/")
+    assert response.status_code == 200
+
+@testing.with_context
+def test_assert_something():
+    response = client.get("/foo/")
+    testing.assert_404(response)
+
+@testing.setup
+def set_x():
+    fixtures.x = 10
+
+@testing.with_context
+def test_fixtures():
+    assert fixtures.x == 10
+
+
 class TestSetup(TestCase):
 
     def create_app(self):
             "http://127.0.0.1:5000/"
 
 
-testing = Testing(create_app)
-
-@testing.with_context
-def test_is_current_app():
-    assert current_app
-
-@testing.with_context
-def test_index():
-    response = client.get("/")
-    assert response.status_code == 200
-
-@testing.with_context
-def test_assert_something():
-    response = client.get("/foo/")
-    testing.assert_404(response)
-
-@testing.setup
-def set_x():
-    fixtures.x = 10
-
-@testing.with_context
-def test_fixtures():
-    assert fixtures.x == 10
-
 class TestClientUtils(TestCase):
 
     def create_app(self):