I'm writing some functional tests for my application but I'd like to use
URL generation instead of typing the URLs in my tests. E.g. instead of:
response = self.testapp.get('/', status=200)
I'd like to write:
response = self.testapp.get(route_path('home'), status=200)
The problem is that I don't know how to get a request object in a
functional test. I tried this:
class FunctionalTests(unittest.TestCase):
def setUp(self):
request = testing.DummyRequest()
self.config = testing.setUp(request=request)
from webtest import TestApp
self.testapp = TestApp("config:test.ini", relative_to="./")
def tearDown(self):
testing.tearDown()
def test_home(self):
request = get_current_request()
response = self.testapp.get(route_path('home', request),
status=200)
self.failUnless('<html' in response.body)
self.failUnless('discuss' in response.body)
but it doesn't work. I get such a stack:
======================================================================
ERROR: test_home (inscuss.tests.test_pages.PagesTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/julas/inscuss/inscuss/tests/test_pages.py", line 10, in
test_home
response = self.testapp.get(route_path('pages/home',
get_current_request()), status=200)
File
"/home/julas/pyramid/lib/python2.6/site-packages/pyramid/url.py", line
184, in route_path
return route_url(route_name, request, *elements, **kw)
File
"/home/julas/pyramid/lib/python2.6/site-packages/pyramid/url.py", line
116, in route_url
mapper = reg.getUtility(IRoutesMapper)
File
"/home/julas/pyramid/lib/python2.6/site-packages/zope/component/registry.py",
line 168, in getUtility
raise ComponentLookupError(provided, name)
ComponentLookupError: (<InterfaceClass
pyramid.interfaces.IRoutesMapper>, u'')
Is there any way to use URL generation in tests?
Regards,
--
Juliusz Gonera
http://juliuszgonera.com/
Hi Julisz, What you've done above *should* work, but it doesn't due to
a bug in Pyramid 1.0 that has been fixed on the trunk. In the meantime,
under 1.0, you can work around it like this, though (untested):
class FunctionalTests(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.request = testing.DummyRequest()
self.request.registry = self.config.registry
from webtest import TestApp
self.testapp = TestApp("config:test.ini", relative_to="./")
def tearDown(self):
testing.tearDown()
def test_home(self):
response = self.testapp.get(route_path('home', self.request),
status=200)
self.failUnless('<html' in response.body)
self.failUnless('discuss' in response.body)
- C
> Hi Julisz, What you've done above *should* work, but it doesn't due to
> a bug in Pyramid 1.0 that has been fixed on the trunk. In the meantime,
> under 1.0, you can work around it like this, though (untested):
>
> class FunctionalTests(unittest.TestCase):
> def setUp(self):
> self.config = testing.setUp()
> self.request = testing.DummyRequest()
> self.request.registry = self.config.registry
> from webtest import TestApp
> self.testapp = TestApp("config:test.ini", relative_to="./")
>
> def tearDown(self):
> testing.tearDown()
>
> def test_home(self):
> response = self.testapp.get(route_path('home', self.request),
> status=200)
> self.failUnless('<html' in response.body)
> self.failUnless('discuss' in response.body)
Unfortunately, it gives me the same error.