使用test_client时出错(烧瓶) - 提高值错误( "unknown url type: %r" % self.full_url)



烧瓶应用代码:

# main.py 
from flask import Flask, Response
app = Flask(__name__)

@app.route("/", methods=["POST"])
def post_example():
return Response("aaa")

这是我的测试代码:

# e2e.py
from main import app
test_client = app.test_client()

def test_flask_api_call():
response = test_client.post("/", {"a": "b"})
pass

我一直在接收:

raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '://%7B%27a%27:%20%27b%27%7D/'

问题出在post方法调用上。您必须将数据参数命名为client.post("/", data={"a": "b"}),如果不是这样,它将被视为URL的一部分。

urllib.parse.quote("/" + str({"a": "b"}))
# '/%7B%27a%27%3A%20%27b%27%7D'

以下是为客户端初始化定义fixture而重写的测试代码。关于测试烧瓶应用的更多信息,请访问官方文档。

import pytest
from main import app

@pytest.fixture(scope="module")
def client():
with app.test_client() as client:
yield client

def test_flask_api_call(client):
response = client.post("/", data={"a": "b"})
assert response.status_code == 200, f"Got bad status code {response.status_code}"

相关内容

最新更新