我正在尝试对“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)
Run Code Online (Sandbox Code Playgroud)
这就是我为测试写的
测试_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
Run Code Online (Sandbox Code Playgroud)
结构
my_project
__init__.py
src
__init__.py
api.py
test
__init__.py
test_api.py
Run Code Online (Sandbox Code Playgroud)
我从根运行测试,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'
Run Code Online (Sandbox Code Playgroud)
我真的不确定如何进行这项工作。