亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pytest:無法讓 Flask 應用程序在測試中運行

Pytest:無法讓 Flask 應用程序在測試中運行

慕森王 2021-11-02 17:04:00
我正在嘗試研究如何使用pytest來測試最基本的 Flask 應用程序。(我在 Windows 10 上)。這是應用程序代碼 myapp.py:from flask import Flaskapi = Flask(__name__)@api.route('/', methods=['GET'])def index():    return 'Index Page'當我在瀏覽器中訪問http://127.0.0.1:5000/時,或者當我使用 curl 向該 URL 發出 GET 請求時,它工作正常,我看到“索引頁面”響應文本。然后我設置了一個基本的測試腳本,test_app.py:import pytestfrom flask import Flaskdef test_assert():    assert Truedef test_home_page(client):  response = client.get('/')  assert response.status_code == [email protected] client():  flask_app = Flask(__name__)   client = flask_app.test_client()  return client我添加了第一個簡單的 test_assert() 函數只是為了確保 pytest 正常工作(我是 python 新手)?,F在,當我運行 pytest (>pytest -v) 時,第一個(瑣碎的)測試通過,但 test_home_page() 測試失敗。該應用程序在通過 pytest 運行時返回狀態代碼 404。collected 2 itemstest_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 == 200E     assert 404 == 200E       -404E       +200test_app.py:10: AssertionError========================================= 1 failed, 1 passed in 0.28 seconds ====我花了幾天時間閱讀試圖確定為什么 pytest 在這個簡單的例子中失敗——響應應該是 200,但它一直給出 404。誰能看到我做錯了什么或者為什么這不起作用?謝謝。
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

嘗試這個:


from YOUR_MODULE import api 


def test_assert():

    assert True


def test_home_page(client):

  response = client.get('/')

  assert response.status_code == 200


@pytest.fixture

def client():

  client = api.test_client()

  return client


查看完整回答
反對 回復 2021-11-02
  • 1 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號