问题描述:

I've been looking around all week and can't wrap my head around how to get an instance of our pylons server started for use with the Behave BDD. Can any of you point me to an example or offer one of your own? Here's what I'm working with:

From the Tutorial page on the Behave doc site, starting a simple server and using selenium, this is their example code for Behave's features/environment.py:

import threading

from wsgiref import simple_server

from selenium import webdriver

from my_application import model

from my_application import web_app

def before_all(context):

context.server = simple_server.WSGIServer(('', 8000))

context.server.set_app(web_app.main(environment='test'))

context.thread = threading.Thread(target=context.server.serve_forever)

context.thread.start()

context.browser = webdriver.Chrome()

def after_all(context):

context.server.shutdown()

context.thread.join()

context.browser.quit()

def before_feature(context, feature):

model.init(environment='test')

It's a start, but I have no idea how to marry this to how our 'pylowiki' flavor of pylons is started up. There is another example I've found that may provide better clues, it is here. In this example I assume I'd replace "world" with "context", but aside from that the way the server is started in this example is different from how our own environment.py starts things up.

One last thing I think may be handy is to include the code from our current environment setup:

pylowiki/config/environment.py

# -*- coding: utf-8 -*-

"""Pylons environment configuration"""

import os

from mako.lookup import TemplateLookup

from pylons import config

from pylons.error import handle_mako_error

from sqlalchemy import engine_from_config

import pylowiki.lib.app_globals as app_globals

import pylowiki.lib.helpers

from pylowiki.config.routing import make_map

from pylowiki.model import init_model

def load_environment(global_conf, app_conf):

"""Configure the Pylons environment via the ``pylons.config``

object

"""

# Pylons paths

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

paths = dict(root=root,

controllers=os.path.join(root, 'controllers'),

static_files=os.path.join(root, 'public'),

templates=[os.path.join(root, 'templates')])

# Initialize config with the basic options

config.init_app(global_conf, app_conf, package='pylowiki', paths=paths)

config['routes.map'] = make_map()

config['pylons.app_globals'] = app_globals.Globals()

config['pylons.h'] = pylowiki.lib.helpers

# Create the Mako TemplateLookup, with the default auto-escaping

config['pylons.app_globals'].mako_lookup = TemplateLookup(

directories=paths['templates'],

error_handler=handle_mako_error,

module_directory=os.path.join(app_conf['cache_dir'], 'templates'),

input_encoding='utf-8', output_encoding='utf-8', default_filters=['escape'],

imports=['from webhelpers.html import escape'])

# Setup the SQLAlchemy database engine

engine = engine_from_config(config, 'sqlalchemy.')

init_model(engine)

# CONFIGURATION OPTIONS HERE (note: all config options will override

# any Pylons config options)

相关阅读:
Top