我正在努力进行一些API请求的单元测试,我在单元测试中得到了这个JSONDecodeError
,这对我来说没有意义,因为响应是内容类型application/json
,并且它不是空的。在这一点上,我没有更多的想法是什么造成的
我的api请求模块:
import requests
from netrc_auth import netrc_authentication
from config import HOST
client = requests.session()
user, password = netrc_authentication(HOST)
response = client.get(
f"https://{HOST}:8000/rest/folder/info?"
f"name=test-name",
auth=(user, password)
)
response_json = response.json()
if(
response.status_code == 200
and response_json["errors"][0]["desc"] == "No such folder!"
):
client.post(
f"https://{HOST}:8000/rest/folder/create?"
f"name=test-name"
f"&path={BASE_PATH}/test",
auth=(user, password),
)
单元测试模块:
def test_check_folder(monkeypatch, requests_mock, host, credentials):
client = requests.session()
if credentials == "":
monkeypatch.setattr(netrc_authentication, "netrc_authentication", mock_authentication)
user, password = netrc_authentication(HOST)
requests_mock.get(
f"https://{HOST}:8000/rest/folder/info?"
f"name=test-name",
text="Unauthorized",
status_code=401,
)
response = client.get(
f"https://{HOST}:8000/rest/folder/info?"
f"name=test-name",
auth=(user, password)
)
assert response.status_code == 401
else:
user, password = netrc_authentication(host)
requests_mock.get(
f"https://{HOST}:8000/rest/folder/info?"
f"name=test-name",
text="OK",
status_code=200,
)
response = client.get(
f"https://{HOST}:8000/rest/folder/info?"
f"name=test-name",
auth=(user, password)
)
response_json = response.json()
if (
response.status_code == 200
and response_json["errors"][0]["desc"]
== "No such folder!"
):
requests_mock.post(
f"https://{HOST}:8000/rest/folder/create?"
f"name=test-name"
f"&path={BASE_PATH}/test",
auth=(user, password),
text="OK",
status_code=201,
)
response = client.post(
f"https://{HOST}:8000/rest/folder/create?"
f"name=test-name"
f"&path={BASE_PATH}/test",
auth=(user, password),
)
assert response.status_code == 201
else:
assert response.status_code == 200
它在单元测试的response_json = response.json()
部分失败,使用json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
。你知道我还可以尝试什么吗
目前没有回答
相关问题 更多 >
编程相关推荐