0

Hi I am trying to implement behave test framework in my django python app. However not sure what the problem is and I keep getting connection refused.

Following is the content of features/environment.py:

import os
import django
import urlparse
os.environ['DJANGO_SETTINGS_MODULE'] = 'tilesproj.settings'
django.setup()


def before_all(context):
    from django.test.runner import DiscoverRunner
    context.runner = DiscoverRunner()
    import wsgi_intercept
    from django.core.handlers.wsgi import WSGIHandler
    host = context.host = 'localhost'
    port = context.port = 8000
    from django.core.wsgi import get_wsgi_application
    from wsgiref.simple_server import make_server
    application = get_wsgi_application()
    wsgi_intercept.add_wsgi_intercept(host, port, lambda: application)
    import mechanize
    context.browser = mechanize.Browser()

    def browser_url(url):
        return urlparse.urljoin('http://%s:%d/' % (host, port), url)

    context.browser_url = browser_url

    from BeautifulSoup import BeautifulSoup
    def parse_soup():
        r = context.browser.response()
        html = r.read()
        r.seek(0)
        return BeautifulSoup(html)

    context.parse_soup = parse_soup


def before_scenario(context, scenario):
    context.runner.setup_test_environment()
    context.old_db_config = context.runner.setup_databases()


def after_scenario(context, scenario):
    context.runner.teardown_databases(context.old_db_config)
    context.runner.teardown_test_environment()

I am trying to figure out how to bootstrap my django app when behave script is run so can test my web app.

vivsriva
  • 43
  • 5

1 Answers1

0

You should try the example Django Behave configuration from the website. Essentially you use a fake browser to perform requests which don't actually go through a real website. Your connections are refused because there is no real server to connect to.

So you should get the mechanize browser from wsgi_intercept instead of from mechanize directly:

### Set up the Mechanize browser.
from wsgi_intercept import mechanize_intercept
# MAGIC: All requests made by this monkeypatched browser to the magic
# host and port will be intercepted by wsgi_intercept via a
# fake socket and routed to Django's WSGI interface.
browser = context.browser = mechanize_intercept.Browser()
Matthew Franglen
  • 4,441
  • 22
  • 32