【问题标题】:How to Run WebTest Example?如何运行 WebTest 示例?
【发布时间】:2017-07-22 16:10:00
【问题描述】:

我正在尝试了解如何使用 WebTest 进行集成测试,但我停留在第一个示例上。

我已尝试按照说明进行操作。首先,我创建了一个包含我要测试的代码的模块:

# 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]

然后我创建了一个测试运行器文件:

# 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

当我执行 test.py 时,出现以下错误:

AssertionError: Iterator 返回了一个非对象:'foobar'。

我需要做什么才能使 WebTest 文档中的示例代码运行?

【问题讨论】:

    标签: python python-3.x webtest


    【解决方案1】:

    在 WSGI 中,body 必须是一个可迭代的字节:

    body = b'foobar'
    

    【讨论】:

    • 哦。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2021-03-06
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多