route_path in functional tests

310 views
Skip to first unread message

Juliusz Gonera

unread,
May 1, 2011, 4:52:53 PM5/1/11
to pylons-...@googlegroups.com
Hi,

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/

Chris McDonough

unread,
May 2, 2011, 11:58:55 AM5/2/11
to pylons-...@googlegroups.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


Juliusz Gonera

unread,
May 2, 2011, 4:39:00 PM5/2/11
to pylons-...@googlegroups.com
Chris McDonough wrote:

> 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.

Renier de Bruyn

unread,
Dec 10, 2015, 2:57:58 AM12/10/15
to pylons-discuss
Hi Julisz,

Did you manage to solve this problem? I am stuck with the same issue.

Kind regards,
Renier de Bruyn
Reply all
Reply to author
Forward
0 new messages