pythonpython-3.xwebtest

How to Run WebTest Example?


I'm trying to understand how to use WebTest to do integration testing and I'm stuck on the first example.

I've tried to follow the instructions. First I created a module that contains the code I want to test:

# functions.py
def application(environ, start_response):
    """docstring for application"""
    # I added body, otherwise you get an undefined variable error
    body = 'foobar'
    headers = [('Content-Type', 'text/html; charset=utf8'),
               ('Content-Length', str(len(body)))]
    start_response('200 OK', headers)
    return [body]

Then I created a test runner file:

# test.py
from webtest import TestApp
from functions import application

app = TestApp(application)
resp = app.get('/')
assert resp.status == '200 OK'
assert resp.status_int == 200

When I execute test.py, I get the following error:

AssertionError: Iterator returned a non- object: 'foobar'.

What do I need to do to make this sample code from the WebTest documentation run?


Solution

  • In WSGI the body must be an iterable of bytes:

    body = b'foobar'