小编Sar*_*ica的帖子

Pytest Flask 应用程序属性错误:模块“src.api”没有属性“test_client”

我正在尝试对“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)

我真的不确定如何进行这项工作。

pytest flask python-3.x

4
推荐指数
1
解决办法
855
查看次数

标签 统计

flask ×1

pytest ×1

python-3.x ×1