running a controller method outside wsgi app

34 views
Skip to first unread message

Roberto Allende

unread,
Jan 19, 2009, 12:44:05 AM1/19/09
to pylons-...@googlegroups.com
Hello

I would like to run a controller method outside a wsgi app and i'm getting
TypeError: No object (name: C) has been registered for this thread

That method uses the pylons variable c and when it is used, in a piece
of code like

c.title = page.title

it raises the exception. I tried using a mock object but it didn't work
neither.

My motivation is to write a unit testing but even in other cases it
could have sense to use a controller function isolated. Or at least in
my case, this is the only restriction.

I'm planning to keep studing how c defined but probably you can give me
a hint in order to move faster, so any comment or help would be very
welcome.

Kind Regards
r.

--
http://menttes.com

Dalius Dobravolskas

unread,
Jan 19, 2009, 1:27:27 AM1/19/09
to pylons-...@googlegroups.com
Hello,

On Mon, Jan 19, 2009 at 7:44 AM, Roberto Allende <ro...@menttes.com> wrote:
My motivation is to write a unit testing but even in other cases it
could have sense to use a controller function isolated. Or at least in
my case, this is the only restriction.
If your intention is unit-testing do it Pylons way: http://wiki.pylonshq.com/display/pylonsdocs/Unit+Testing

I don't see any other reason to isolate function from controller. If you do that think if that function even belongs there.

--
Dalius
http://blog.sandbox.lt

Wichert Akkerman

unread,
Jan 19, 2009, 2:34:03 AM1/19/09
to Dalius Dobravolskas, pylons-...@googlegroups.com
Previously Dalius Dobravolskas wrote:
> Hello,
>
> On Mon, Jan 19, 2009 at 7:44 AM, Roberto Allende <ro...@menttes.com> wrote:
>
> > My motivation is to write a unit testing but even in other cases it
> > could have sense to use a controller function isolated. Or at least in
> > my case, this is the only restriction.
>
> If your intention is unit-testing do it Pylons way:
> http://wiki.pylonshq.com/display/pylonsdocs/Unit+Testing

That is not unit testing, that is functional testing.

If you want to unit test your controller and run it in isolation you
need to do a bit of setup work in your test case. I use this:

from unittest import TestCase
from paste.registry import Registry
import pylons
from pylons.util import ContextObj
from pylons.controllers.util import Request


class MockTranslator:
def ugettext(self, value):
return value


class PylonsTestCase(TestCase):
"""A basic test case which allows access to pylons.c and pylons.request.
"""

def setUp(self):
self.registry=Registry()
self.registry.prepare()

self.context_obj=ContextObj()
self.registry.register(pylons.c, self.context_obj)

self.request_obj=Request(dict(HTTP_HOST="nohost"))
self.registry.register(pylons.request, self.request_obj)

self.translator_obj=MockTranslator()
self.registry.register(pylons.translator, self.translator_obj)


If you just need database access you can use this:

class DatabaseTestCase(TestCase):
def setUp(self):
init_model(meta.engine)
meta.metadata.create_all(meta.engine)

def tearDown(self):
meta.Session.remove()
meta.metadata.drop_all(meta.engine)

and if you need both just inherit from both classes (and call setUp from both).

Wichert.

--
Wichert Akkerman <wic...@wiggy.net> It is simple to make things.
http://www.wiggy.net/ It is hard to make things simple.

Dalius Dobravolskas

unread,
Jan 19, 2009, 2:43:55 AM1/19/09
to Dalius Dobravolskas, pylons-...@googlegroups.com
On Mon, Jan 19, 2009 at 9:34 AM, Wichert Akkerman <wic...@wiggy.net> wrote:
Previously Dalius Dobravolskas wrote:
> Hello,
>
> On Mon, Jan 19, 2009 at 7:44 AM, Roberto Allende <ro...@menttes.com> wrote:
>
> > My motivation is to write a unit testing but even in other cases it
> > could have sense to use a controller function isolated. Or at least in
> > my case, this is the only restriction.
>
> If your intention is unit-testing do it Pylons way:
> http://wiki.pylonshq.com/display/pylonsdocs/Unit+Testing

That is not unit testing, that is functional testing.
What's the difference except that your way might be a little bit more-efficient (like 20%)? I have seen several different ways of unit-testing pylons applications but basically they just help to do the same. What makes unit-testing functional testing in your opinion?
 
--
Dalius
http://blog.sandbox.lt

Wichert Akkerman

unread,
Jan 19, 2009, 3:18:00 AM1/19/09
to Dalius Dobravolskas, pylons-...@googlegroups.com

I run the controller method in isolation, which means:

- no other middleware that influences the result
- I can put stuf in c before I call the controller method, and
introspect c afterwards
- no paste.fixture or WebTest influencing the result

with a unittest you want to call something in isolation, without
anything else being present. This is the only way to do that.

Dalius Dobravolskas

unread,
Jan 19, 2009, 3:36:28 AM1/19/09
to Dalius Dobravolskas, pylons-...@googlegroups.com
On Mon, Jan 19, 2009 at 10:18 AM, Wichert Akkerman <wic...@wiggy.net> wrote:
I run the controller method in isolation, which means:

- no other middleware that influences the result
- I can put stuf in c before I call the controller method, and
 introspect c afterwards
- no paste.fixture or WebTest influencing the result

with a unittest you want to call something in isolation, without
anything else being present. This is the only way to do that.

I see some value in that. Thanks.

--
Dalius
http://blog.sandbox.lt

Roberto Allende

unread,
Jan 20, 2009, 12:18:15 AM1/20/09
to pylons-...@googlegroups.com
Wichert Akkerman escribió:

> Previously Dalius Dobravolskas wrote:
>
>> Hello,
>>
>> On Mon, Jan 19, 2009 at 7:44 AM, Roberto Allende <ro...@menttes.com> wrote:
>>
>>
>>> My motivation is to write a unit testing but even in other cases it
>>> could have sense to use a controller function isolated. Or at least in
>>> my case, this is the only restriction.
>>>
>> If your intention is unit-testing do it Pylons way:
>> http://wiki.pylonshq.com/display/pylonsdocs/Unit+Testing
>>
>
> That is not unit testing, that is functional testing.
>
> If you want to unit test your controller and run it in isolation you
> need to do a bit of setup work in your test case. I use this:
>

Thanks a lot Wiggy, your mails were very helpful. Although, it seems
we're using different versions of Pylons (or i'm doing smt wrong)
because following your approach i also needed wsgiapp variables Buffet,
g and session at least.
Just for the record, i'm using 0.9.6.2.

By the other hand, in pylons.wsgiapp there's a method called
load_test_env wich seems to use a test environment. If we're able to use
that instead, we wouldn't be using a mock object but at least, we would
use a test environment.

I'll keep trying. Thanks again!


Kind Regards
r.

--
http://menttes.com
http://robertoallende.com

Wichert Akkerman

unread,
Jan 20, 2009, 3:52:50 AM1/20/09
to Roberto Allende, pylons-...@googlegroups.com
Previously Roberto Allende wrote:
>
> Wichert Akkerman escribió:
> > Previously Dalius Dobravolskas wrote:
> >
> >> Hello,
> >>
> >> On Mon, Jan 19, 2009 at 7:44 AM, Roberto Allende <ro...@menttes.com> wrote:
> >>
> >>
> >>> My motivation is to write a unit testing but even in other cases it
> >>> could have sense to use a controller function isolated. Or at least in
> >>> my case, this is the only restriction.
> >>>
> >> If your intention is unit-testing do it Pylons way:
> >> http://wiki.pylonshq.com/display/pylonsdocs/Unit+Testing
> >>
> >
> > That is not unit testing, that is functional testing.
> >
> > If you want to unit test your controller and run it in isolation you
> > need to do a bit of setup work in your test case. I use this:
> >
>
> Thanks a lot Wiggy, your mails were very helpful. Although, it seems
> we're using different versions of Pylons (or i'm doing smt wrong)
> because following your approach i also needed wsgiapp variables Buffet,
> g and session at least.
> Just for the record, i'm using 0.9.6.2.

I'm using 0.9.7rc5, so there are bound to be some differences.

Reply all
Reply to author
Forward
0 new messages