On Jul 3, 10:53 am, Ben Bangert <[email protected]> wrote:
> On Jul 3, 2010, at 6:57 AM, Hans Lellelid wrote:
>
> > But obviously the better approach would be to keep the changes in the
> > code I control :)
>
> This is exactly the approach I'm taking to fix testing up for Pylons 1.1.
> Here is what the tests/__init__.py looks like in the 1.1 branch for example
> (which will work fine for projects right now using Pylons):
> """Pylons application test package
>
> This package assumes the Pylons environment is already loaded, such as
> when this script is imported from the `nosetests --with-pylons=test.ini`
> command.
>
> This module initializes the application via ``websetup`` (`paster
> setup-app`) and provides the base testing objects.
> """
> from unittest import TestCase
> import os
> import sys
>
> import pylons
> from pylons.i18n.translation import _get_translator
> from paste.deploy import loadapp
> from pylons import url
> from routes.util import URLGenerator
> from webtest import TestApp
>
> from {{package}}.config.environment import load_environment
>
> __all__ = ['environ', 'url', 'TestController']
>
> environ = {}
> here_dir = os.path.dirname(os.path.abspath(__file__))
> conf_dir = os.path.dirname(os.path.dirname(here_dir))
>
> sys.path.insert(0, conf_dir)
>
> class TestController(TestCase):
> def __init__(self, *args, **kwargs):
> wsgiapp = loadapp('config:test.ini', relative_to=conf_dir)
> config = wsgiapp.config
> pylons.app_globals._push_object(config['pylons.app_globals'])
> pylons.config._push_object(config)
>
> # Initialize a translator for tests that utilize i18n
> translator = _get_translator(pylons.config.get('lang'))
> pylons.translator._push_object(translator)
>
> url._push_object(URLGenerator(config['routes.map'], environ))
> self.app = TestApp(wsgiapp)
> TestCase.__init__(self, *args, **kwargs)
The only thing I don't like about this approach is that you have to do
`app.config = config` at the end of `make_app`. It feels clunky for
one thing, but more importantly, it won't work in every case--for
example, when the app is wrapped in filtering middleware that's
specified in a config file; in this case the app returned from the
`loadapp` call is not the same app that's returned from `make_app`.
Possible solutions:
- At the end of load_environment, set config as a global in the
environment module, which can then be imported into the tests module.
- In load_environment, call into a test setup function
# environment.py
from my_pkg.tests import setup_test_environment
def load_environment(...):
# ...
if testing:
setup_test_environment(config)
return config
# my_pkg/tests/__init__.py
def setup_test_environment(app_config):
global config
config = app_config
# maybe setup the test environ, too:
environ[key] = app_config[key]
In both cases, the `config = wsgiapp.config` would be removed from
TestController.__init__.
> [...]
--
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.