Commits

Anonymous committed 254a1e4

0.3 changes, TwillTestCase deprecated

Comments (0)

Files changed (6)

 # built documents.
 #
 # The short X.Y version.
-version = '0.2'
+version = '0.3'
 # The full version, including alpha/beta/rc tags.
-release = '0.2'
+release = '0.3'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
 ----------------
 
 `Twill`_ is a simple language for browing the Web through
-a command line interface. You can use it in conjunction with ``TwillTestCase`` to write
-functional tests for your views. 
+a command line interface. 
 
-``TwillTestCase`` is a subclass of ``TestCase``. It sets up `Twill`_ for use with your test 
-cases. See the `API`_ below for details.
+``Flask-Testing`` comes with a helper class for creating functional tests using Twill::
+
+    def test_something_with_twill(self):
+
+        with Twill(self.app, port=3000) as t:
+            t.browser.go(t.url("/"))
+
+
+The older ``TwillTestCase`` has been deprecated.
 
 Testing with SQLAlchemy
 -----------------------
 .. autoclass:: TestCase
    :members:
 
+.. autoclass:: Twill
+   :members:
+
 .. autoclass:: TwillTestCase
    :members:
 

examples/twill_site/tests.py

-from flaskext.testing import TwillTestCase
+from flaskext.testing import TestCase, Twill
 
 from todos import create_app
 
-class TestViews(TwillTestCase):
+class TestViews(TestCase, Twill):
 
     def create_app(self):
-        return create_app()
+        app = create_app()
+        self.twill = Twill(app)
+        return app
 
     def test_manually(self):
 
-        self.browser.go(self.make_twill_url("/"))
-        self.browser.showforms()
-        self.browser.submit(0)
+        self.twill.browser.go(self.twill.url("/"))
+        self.twill.browser.showforms()
+        self.twill.browser.submit(0)
 
     def test_bad_manually(self):
         """
         This will fail !
         """
-        self.browser.go(self.make_twill_url("/foo/"))
-        self.browser.showforms()
-        self.browser.submit(1)
+        self.twill.browser.go(self.twill.url("/foo/"))
+        self.twill.browser.showforms()
+        self.twill.browser.submit(1)
 
 

flaskext/testing.py

 except ImportError:
     _is_signals = False
 
-__all__ = ["TestCase", "TwillTestCase"]
+__all__ = ["TestCase", "TwillMixin"]
 
 class ContextVariableDoesNotExist(Exception):
     pass
     assert_405 = assert405
 
 
+class Twill(object):
+    """
+
+    :versionadded: 0.3
+
+    Twill wrapper utility class.
+
+    Creates a Twill ``browser`` instance and handles
+    WSGI intercept.
+        
+    Usage::
+
+        t = Twill(self.app)
+        with t:
+            t.browser.go("/")
+            t.url("/")
+
+    """
+    def __init__(self, app, host='127.0.0.1', port=5000, scheme='http'):
+        
+        self.app = app
+        self.host = host
+        self.port = port
+        self.scheme = scheme
+
+        self.browser = twill.get_browser()
+
+    def __enter__(self):
+        twill.set_output(StringIO.StringIO())
+        twill.commands.clear_cookies()
+        twill.add_wsgi_intercept(self.host, 
+                                 self.port, 
+                                 lambda: self.app)
+    
+        return self
+
+    def __exit__(self, exc_type, exc_value, tb):
+
+        twill.remove_wsgi_intercept(self.host, 
+                                    self.port)
+
+        twill.commands.reset_output()
+
+    def url(self, url):
+        """
+        Makes complete URL based on host, port and scheme
+        Twill settings.
+
+        :param url: relative URL
+        """
+        return "%s://%s:%d%s" % (self.scheme,
+                                 self.host, 
+                                 self.port,
+                                 url)
+
 class TwillTestCase(TestCase):
+
     """
-    TestCase with Twill helper methods.
+    :deprecated: use Twill helper class instead.
 
     Creates a Twill ``browser`` instance and handles
     WSGI intercept.
 
 setup(
     name='Flask-Testing',
-    version='0.2.5',
+    version='0.3',
     url='http://bitbucket.org/danjac/flask-testing',
     license='BSD',
     author='Dan Jacob',

tests/__init__.py

     jsonify, render_template
 
 from flaskext.testing import TestCase, TwillTestCase, \
-    ContextVariableDoesNotExist
+    ContextVariableDoesNotExist, Twill
 
 def create_app():
 
         assert self.client
         assert self._ctx
 
-class TestTwill(TwillTestCase):
+class TestTwill(TestCase):
     
     def create_app(self):
         app = create_app()
 
     def test_twill_setup(self):
         
-        assert self.twill_host == '127.0.0.1'
-        assert self.twill_port == 5000
+        twill = Twill(self.app)
+
+        assert twill.host == '127.0.0.1'
+        assert twill.port == 5000
+        assert twill.browser
 
     def test_make_twill_url(self):
+        with Twill(self.app) as t:
+            assert t.url("/") == "http://127.0.0.1:5000/"
 
-        assert self.make_twill_url("/") == \
-            "http://127.0.0.1:5000/"
 
 class TestClientUtils(TestCase):
 
         except RuntimeError:
             pass
 
+ 
+class TestTwillDeprecated(TwillTestCase):
     
+    def create_app(self):
+        app = create_app()
+        app.config.from_object(self)
+        return app
+
+    def test_twill_setup(self):
+        
+        assert self.app
+        assert self.twill_host == '127.0.0.1'
+        assert self.twill_port == 5000
+        assert self.browser
+
+    def test_make_twill_url(self):
+
+        assert self.make_twill_url("/") == \
+            "http://127.0.0.1:5000/"
+
+   
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.