On Wed, 2010-12-22 at 21:18 -0800, jerry wrote:
> Thanks Chris.
> 
> Well, I thought routing will always happen after view functions are
> decorated with view_config()
> 
> Allow me to step back one step and ask: How can routing be unit
> tested?

When you test that your views are properly routed to in a unit test,
you're not testing your application, you're testing Pyramid (because
Pyramid is the thing that does the routing).

I think you probably want both functional tests (as per the URL I pasted
below) *and* unit tests.

To make sure your routing is configured correctly, write a functional
test for each route to make sure the right view gets invoked.  Nothing
fancy, something like:

import unittest

class FunctionalTests(unittest.TestCase):
    def setUp(self):
        from myapp import main
        app = main({})
        from webtest import TestApp
        self.testapp = TestApp(app)

    def test_routename(self):
        res = self.testapp.get('/routename/id', status=200)
        self.failUnless('some html i know is returned' in res.body)

But then *also* write unit tests that test the "guts" of each view.
Technically unit tests only test a single unit (such as a function or
method), not the interaction of that function or method with the rest of
the system.  Since they do that, it's easier to get better test coverage
with unit tests than functional tests, and they're faster to run,
because you can provide "mock" implementations of things on which the
view relies instead of setting up and tearing down the entire system for
each test.

So, something like this might be a good start for a unit test:

import unittest
from pyramid.testing import DummyRequest

import TestMyView(unittest.TestCase):
    def call_view(self, request):
        from mypackage.views import myview
        return myview(request)

    def test_view(self):
        request = DummyRequest()
        result = self.call_view(request)
        self.assertEqual(result['abc'], '123')

At least if "myview" was implemented like:

@view_config(renderer='some/renderer.pt')
def myview(request):
   return {'abc':'123'}

The Pyramid testing chapter has more info about setting up and tearing
down tests that rely on framework components, if you need to do that.

- C




> 
> Jerry
> 
> BTW, your attached a local URI, the public URL should be
> http://docs.pylonshq.com/pyramid/dev/narr/testing.html#creating-functional-tests
> 
> On Dec 23, 11:53 am, Chris McDonough <[email protected]> wrote:
> > On Wed, 2010-12-22 at 19:45 -0800, jerry wrote:
> > > Hi,
> >
> > > I have the following URL dispatch setup --
> >
> > > <__init__.py>
> > > config.add_route('user', '/user')
> > > config.add_route('user_id', '/user/{user_id}')
> > > config.scan()
> > > </__init.py__>
> >
> > > <views/user.py>
> > > @view_config(route_name='user', renderer='user.genshi')
> > > @view_config(route_name='user_id', renderer='user_id.genshi')
> > > def user_view(request):
> > > </views/user.py>
> >
> > > which works fine.
> >
> > > However, my unit test case always gets routed to 'user' instead of
> > > 'user_id' no matter how I tweak the request.path_info --
> >
> > > <test_user_view.py>
> > > request = DummyRequest()
> > > request.path_info = route_url('user_id', request, user_id=u'42')
> > > #request.path_info = '/user/42'
> > > resp = views.user.user_view(request)
> > > </test_user_view.py>
> >
> > > I'm sure I must be missing something very simple there.
> >
> > "route_url" returns a fully qualified URL (with host, scheme, port,
> > path).  You probably want "route_path".  Not sure if this is the problem
> > you're writing about, but it's *a* problem.
> >
> > I actually don't understand the question, to be honest.  You're
> > explicitly calling user_view.  No routing is actually happening; you're
> > not running a request through the "router".  What are you trying to
> > test?  If you're trying to write a functional test, do it like this
> > instead:
> >
> > file:///home/chrism/projects/pyramid/docs/_build/html/narr/testing.html#creating-functional-tests
> >
> > - C
> 


-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to