Commits

Anonymous committed 3668975

attest branch

  • Participants
  • Parent commits 254a1e4
  • Branches attest-integration

Comments (0)

Files changed (4)

 syntax: glob
 
+*.coverage
 *.egg-info
+*.log
 *.pyc
 *.db
 *.swp
 *.swo
 *.zip
-*.egg
 *.orig
+*.cfg
+*.tox
+
+
 *~
 
+fab_settings.py
+production_settings.py
+
 dist
 docs/output
 
+_uploads
 
+.git
+.gitignore

flaskext/testing.py

 import unittest
 import twill
 import simplejson
+import attest
 
 from werkzeug import cached_property
+from attest import Assert, Tests, test
 
 try:
     # we'll use signals for template-related tests if
 except ImportError:
     _is_signals = False
 
-__all__ = ["TestCase", "TwillMixin"]
+__all__ = ["TestCase", "Twill", "Assert", "TestBase",
+           "Tests", "test"]
 
 class ContextVariableDoesNotExist(Exception):
     pass
     return TestResponse
 
 
+class TestBase(attest.TestBase):
+    """
+    TestBase subclass which handles all the app setup stuff.
+    Adds the following variables to test scope::
+        
+        self.app -> Flask app instance
+        self.client -> test client
+        self.ctx -> test context
+        self.templates -> templates used
+
+    To use this class, first subclass and implement ``create_app``.
+    As with the TestCase class this should return a Flask instance.
+
+    Two hooks, ``setup`` and ``teardown`` are provided, which are 
+    called just after and before the above variables are created/
+    destroyed. You can therefore use these for creating fixtures,
+    database handling, etc.
+    """
+
+    def setup(self):
+        """
+        This is called after the app is set up but before tests are
+        run. Add your own setup code here.
+        """
+
+    def teardown(self):
+        """
+        This is called after the tests are run. Add your own teardown 
+        code here.
+        """
+
+    def create_app(self):
+        """
+        This class should be implemented, in order to return an
+        Flask instance initialized as required.
+        """
+
+        raise NotImplementedError
+
+    def __context__(self):
+            
+        self.app = self.create_app()
+
+        # use custom response 
+        response_class = self.app.response_class
+        self.app.response_class = _make_test_response(app.response_class)
+
+        self.ctx = self.app.test_request_context()
+        self.ctx.push()
+
+        self.templates = []
+
+        def _add_template(app, template, context):
+            self.templates.append((template, context))
+
+        template_rendered.connect(_add_template)
+
+        self.client = self.app.test_client()
+
+        try:
+            self.setup()
+            yield
+            self.teardown()
+
+        finally:
+            if self.ctx is not None:
+                self.ctx.pop()
+
+            #self.app.response_class = response_class
+            template_rendered.disconnect(_add_template)
+
+            del self.client
+            del self.ctx
+            del self.app
+            del self.templates
+
+
 class TestCase(unittest.TestCase):
     
     def create_app(self):
     install_requires=[
         'Flask',
         'twill',
+        'attest',
         'simplejson',
     ],
     tests_require=[

tests/__init__.py

 
     return app
 
+class TestAttest(TestCase):
+
+    def test_create_test_base(self):
+
+        from flaskext.testing import TestBase, Assert, Tests, test
+
+        class Views(TestBase):
+
+            def create_app(self):
+                return create_app()
+
+            @test
+            def assert_200(self): 
+
+                response = self.client.get("/")
+                Assert(response.status_code) == 200
+
+            @test
+            def get_json(self):
+
+                response = self.client.get("/ajax/")
+                Assert(response.json) == dict(name="test")
+
+        suite = Tests([Views()])
+        suite.run()
+
+
 class TestSetup(TestCase):
 
     def create_app(self):