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 <wich...@wiggy.net>    It is simple to make things.
http://www.wiggy.net/                   It is hard to make things simple.

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

Reply via email to