【发布时间】:2019-11-24 20:25:45
【问题描述】:
我正在为“hello world”烧瓶应用程序尝试基本的 Pytest 测试 请看下面我在 src 文件中的内容
api.py:
from flask import Flask, jsonify
api = Flask(__name__)
@api.route('/')
def hello_world():
return jsonify(message="hello world")
if __name__ == '__main__':
api.run(debug=True)
这是我为测试写的
test_api.py
import pytest
from src import api
api.testing = True
client = api.test_client()
def test_route(client):
response = api.get('/')
assert response.status_code == 200
结构
my_project
__init__.py
src
__init__.py
api.py
test
__init__.py
test_api.py
我使用 python -m pytest
从根目录运行测试
我得到的错误信息是
test/test_api.py:13: in <module>
with api.test_client() as client:
E AttributeError: module 'src.api' has no attribute 'test_client'
我真的不确定如何完成这项工作。
【问题讨论】:
标签: python-3.x flask pytest