Python Pytest:无法使Flask应用程序在测试中运行

Python Pytest:无法使Flask应用程序在测试中运行,python,pytest,Python,Pytest,我正在尝试如何使用pytest测试最基本的Flask应用程序。(我在Windows 10上)。以下是应用程序代码myapp.py: from flask import Flask api = Flask(__name__) @api.route('/', methods=['GET']) def index(): return 'Index Page' 当我在浏览器中转到时,或者当我使用curl向该URL发出GET请求时,它工作正常,我会看到“索引页”响应文本 然后我设置了一个基本

我正在尝试如何使用pytest测试最基本的Flask应用程序。(我在Windows 10上)。以下是应用程序代码myapp.py:

from flask import Flask

api = Flask(__name__)

@api.route('/', methods=['GET'])
def index():
    return 'Index Page'
当我在浏览器中转到时,或者当我使用curl向该URL发出GET请求时,它工作正常,我会看到“索引页”响应文本

然后我设置了一个基本测试脚本test_app.py:

import pytest
from flask import Flask

def test_assert():
    assert True

def test_home_page(client):
  response = client.get('/')
  assert response.status_code == 200

@pytest.fixture
def client():
  flask_app = Flask(__name__) 
  client = flask_app.test_client()
  return client
我添加了第一个简单的test_assert()函数,以确保pytest正常工作(我是python新手)

现在,当我运行pytest(>pytest-v)时,第一个(琐碎的)测试通过,但是test_home_page()测试失败。当运行pytest时,应用程序返回状态代码404

collected 2 items

test_app.py::test_assert PASSED                                                                                  [ 50%]
test_app.py::test_home_page FAILED                                                                               [100%]

====================================================== FAILURES =======================================================
___________________________________________________ test_home_page ____________________________________________________

client = <FlaskClient <Flask 'test_app'>>

    def test_home_page(client):
      response = client.get('/')
>     assert response.status_code == 200
E     assert 404 == 200
E       -404
E       +200

test_app.py:10: AssertionError
========================================= 1 failed, 1 passed in 0.28 seconds ====
收集2项
test_app.py::test_断言通过[50%]
test_app.py::test_主页失败[100%]
========================================================================故障=======================================================
___________________________________________________测试主页____________________________________________________
客户=
def测试主页(客户端):
response=client.get(“/”)
>assert response.status_code==200
E断言404==200
E-404
E+200
测试应用程序py:10:AssertionError
=============================================================1失败,1在0.28秒内通过====
我花了几天时间四处阅读,试图确定为什么pytest在这个简单的示例中失败——响应应该是200,但它一直给出404

有人能看出我做错了什么,或者为什么这行不通吗?谢谢。

试试这个:

从您的_模块导入api
def test_assert():
断言正确
def测试主页(客户端):
response=client.get(“/”)
assert response.status_code==200
@pytest.fixture
def client():
client=api.test\u client()
返回客户端
试试这个:

从您的_模块导入api
def test_assert():
断言正确
def测试主页(客户端):
response=client.get(“/”)
assert response.status_code==200
@pytest.fixture
def client():
client=api.test\u client()
返回客户端

这是否相关/重复?测试客户端上没有声明路由。尝试为应用程序创建工厂,然后从工厂生成测试客户端。否则,请尝试从您的第一个代码段导入api,即client=api.test_client(),您能否用我提供的代码演示如何在测试客户端上添加路由。谢谢。也许这是相关的/重复的?测试客户端上没有声明路由。尝试为应用程序创建工厂,然后从工厂生成测试客户端。否则,请尝试从您的第一个代码段导入api,即client=api.test_client(),您能否用我提供的代码演示如何在测试客户端上添加路由。谢谢,成功了!非常感谢你!(从您的_模块导入api切换了顶行)成功了!非常感谢你!(从您的_模块导入api切换顶行)