callbacks added to DummyRequest using add_finished_callback not called when running tests

42 views
Skip to first unread message

Tjelvar

unread,
Apr 3, 2013, 7:34:37 AM4/3/13
to pylons-...@googlegroups.com
Dear all,

I'm not sure if this is the expected behaviour or not. In the documentation it states that DummyRequests implement add_finished_callback.

http://docs.pylonsproject.org/projects/pyramid/en/latest/whatsnew-1.2.html

However, to me it seems like the added callbacks never get called when I run the tests.

What I did.

1. Created a new "starter" project using pcreate
2. Modified the __init__.py file to include the lines

from pyramid.events import NewRequest
from pyramid.events import subscriber

@subscriber(NewRequest)
def new_request_subscriber(event):
    print 'In new request subscriber...'
    request = event.request
    print 'Request: %s' % request
    request.add_finished_callback(finished_callback)

def finished_callback(request):
    print 'In finished_callback...'

3. Modified the test_my_view function in the tests.py file

    def test_my_view(self):
        from .views import my_view
        from pyramid.events import NewRequest
        from test_pyramid import new_request_subscriber
        request = testing.DummyRequest()
       
        self.config.testing_add_subscriber(NewRequest)
        event = NewRequest(request)
        self.config.registry.notify(new_request_subscriber(event))

        info = my_view(request)
        self.assertEqual(info['project'], 'test_pyramid')

4. Ran the tests (python setup.py test -q)

In new request subscriber...
Request: <pyramid.testing.DummyRequest object at 0x8f60a2c>
.
----------------------------------------------------------------------
Ran 1 test in 0.008s

OK

Note that in the above the finished_callback was never triggered.

This is a silly example, but the problem is that in my real application I need the test code to be able to close a handle to a database that I have opened using the new_request_subscriber.

Any ideas how I can get the finished_callback to be triggered?




Michael Merickel

unread,
Apr 3, 2013, 11:00:06 AM4/3/13
to Pylons
The triggering of things like this is done in Pyramid's real request pipeline, which you are not simulating here by invoking the view directly. The DummyRequest supports catching finished_callbacks so that you may see what was added to the request, but it does not invoke them.

If you want to simulate things like this you need to make a functional test using something like WebTest that can shove a request through Pyramid's router and invoking all machinery.







--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tjelvar

unread,
Apr 3, 2013, 1:34:14 PM4/3/13
to pylons-...@googlegroups.com
Thank you for your clear answer Michael, much appriciated.

I re-factored the tests making use of "mockers" instead of real calls to the database. Will try to create some functional test using WebTest in the near future.

Kind regards,

Tjelvar
Reply all
Reply to author
Forward
0 new messages