Commits

Lukas Linhart  committed 0f74e66

Twill integration (refs #31)

  • Participants
  • Parent commits f759874

Comments (0)

Files changed (4)

File djangosanetesting/cases.py

-from django.core.urlresolvers import clear_url_caches
 from nose.tools import (
                 assert_equals,
                 assert_almost_equals,
 )
 from nose import SkipTest
 
+from djangosanetesting.utils import twill_patched_go
+
 __all__ = ("UnitTestCase", "DatabaseTestCase", "DestructiveDatabaseTestCase", "HttpTestCase", "SeleniumTestCase")
 
 class SaneTestCase(object):
     required_sane_plugins = ["django", "http"]
     http_plugin_started = False
     test_type = "http"
+
+    def __init__(self, *args, **kwargs):
+        super(HttpTestCase, self).__init__(*args, **kwargs)
+
+        self._twill = None
+
+    def get_twill(self):
+        if not self._twill:
+            try:
+                import twill
+            except ImportError:
+                raise SkipTest("Twill must be installed if You want to use it")
+
+            from twill import get_browser
+
+            self._twill = get_browser()
+            self._twill.go = twill_patched_go(self._twill.go)
+
+        return self._twill
+
+    twill = property(fget=get_twill)
     
 class SeleniumTestCase(HttpTestCase):
     """

File djangosanetesting/noseplugins.py

 #from djagnosanetesting.cache import flush_django_cache
 from djangosanetesting.selenium.driver import selenium
 from djangosanetesting.utils import (
-    get_live_server_path,
+    get_live_server_path, 
     DEFAULT_LIVE_SERVER_ADDRESS, DEFAULT_LIVE_SERVER_PORT
 )
 
         # clear test client for test isolation
         if test_instance:
             test_instance.client = None
-    
+
+    def stopTest(self, test):
+        test_instance = get_test_case_instance(test)
+        if getattr(test_instance, "_twill", None):
+            from twill.commands import reset_browser
+            reset_browser()
+
+
     def finalize(self, result):
         self.stop_test_server()
 

File djangosanetesting/utils.py

         getattr(settings, "URL_ROOT_SERVER_ADDRESS", DEFAULT_URL_ROOT_SERVER_ADDRESS),
         getattr(settings, "LIVE_SERVER_PORT", DEFAULT_LIVE_SERVER_PORT)
     ))
+
+def twill_patched_go(original_go):
+    """
+    If call is not beginning with http, prepent it with get_live_server_path
+    to allow relative calls
+    """
+    def twill_go_with_relative_paths(uri, *args, **kwargs):
+        if not uri.startswith("http"):
+            base = get_live_server_path()
+            if uri.startswith("/"):
+                base = base.rstrip("/")
+            uri = "%s%s" % (base, uri)
+        return original_go(uri, *args, **kwargs)
+
+    return twill_go_with_relative_paths
+

File testproject/test/test_liveserver.py

 # -*- coding: utf-8 -*-
 import urllib2
 from djangosanetesting.cases import HttpTestCase, SeleniumTestCase
+from djangosanetesting.utils import get_live_server_path
 
 from testapp.models import ExampleModel
 
 
     def test_czech_string_acquired_even_with_selenium(self):
         self.assert_equals(u"Přeložitelný řetězec", unicode(ExampleModel.get_translated_string()))
+
+class TestTwill(HttpTestCase):
+
+    def test_ok_retrieved(self):
+        self.twill.go("%stesttwohundred/" % get_live_server_path())
+        self.assert_equals(200, self.twill.get_code())
+
+    def test_live_server_added_when_missing(self):
+        self.twill.go("/testtwohundred/")
+        self.assert_equals(200, self.twill.get_code())
+
+    def test_missing_recognized(self):
+        self.twill.go("/this/should/never/exist/")
+        self.assert_equals(404, self.twill.get_code())