Pytest 对调用 API 的函数的 return 值进行测试
Pytest on return value of a function that makes an API call
我有一个调用 API 的函数,我想将该 API 调用的 return 值 (response
) 设为 {"message":"NoPong"}
这样 ValueError
就会被提升。到目前为止,我有
# root_folder/subfolder/includes/abc/functions.py
from abc import ABC # 3rd party library
def _check_connection(conn_id):
api_key = get_api_key(conn_id)
api_client = ABC(api_key=api_key)
response = api_client.test_connection()
print(response)
if response['message'] != "pong":
raise ValueError("Failed connection to ABC")
---------------
# root_folder/tests/test_functions.py
import pytest
import sys
sys.path.insert(0, '../subfolder/')
from includes.abc.functions import _check_connection
def test_check_connection_raiseError(mocker):
with pytest.raises(ValueError) as execinfo:
mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
_check_connection("conn_A")
assert str(execinfo.value) == 'Failed connection to ABC'
我得到的pytest结果是
================================================== FAILURES ==================================================
______________________________________ test_check_connection_raiseError ______________________________________
mocker = <pytest_mock.plugin.MockerFixture object at 0x7f85f30c7220>
def test_check_connection_raiseError(mocker):
with pytest.raises(ValueError) as execinfo:
mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
> _check_connection("conn_A")
test_functions.py:73:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
conn_id = 'conn_A'
def _check_connection(conn_id):
api_key = get_api_key(conn_id)
api_client = ABC(api_key=api_key)
response = api_client.test_connection()
print(response)
> if response['message'] != "pong":
E TypeError: string indices must be integers
../subfolder/includes/abc/functions.py:70: TypeError
-------------------------------------------- Captured stdout call --------------------------------------------
{"message":"NoPong"}
========================================== short test summary info ===========================================
FAILED test_functions.py::test_check_connection_raiseError - TypeError: string indices must be integers
为什么 pytest 抱怨我的 _check_connection
函数?它使用键 message
正确访问字典,并且它在 pytest 之外工作。
你需要return一个字典而不是一个字符串,所以mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
应该变成:mocker.patch('includes.abc.functions.ABC.test_connection', return_value={"message":"NoPong"})
没有引号。
我有一个调用 API 的函数,我想将该 API 调用的 return 值 (response
) 设为 {"message":"NoPong"}
这样 ValueError
就会被提升。到目前为止,我有
# root_folder/subfolder/includes/abc/functions.py
from abc import ABC # 3rd party library
def _check_connection(conn_id):
api_key = get_api_key(conn_id)
api_client = ABC(api_key=api_key)
response = api_client.test_connection()
print(response)
if response['message'] != "pong":
raise ValueError("Failed connection to ABC")
---------------
# root_folder/tests/test_functions.py
import pytest
import sys
sys.path.insert(0, '../subfolder/')
from includes.abc.functions import _check_connection
def test_check_connection_raiseError(mocker):
with pytest.raises(ValueError) as execinfo:
mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
_check_connection("conn_A")
assert str(execinfo.value) == 'Failed connection to ABC'
我得到的pytest结果是
================================================== FAILURES ==================================================
______________________________________ test_check_connection_raiseError ______________________________________
mocker = <pytest_mock.plugin.MockerFixture object at 0x7f85f30c7220>
def test_check_connection_raiseError(mocker):
with pytest.raises(ValueError) as execinfo:
mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
> _check_connection("conn_A")
test_functions.py:73:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
conn_id = 'conn_A'
def _check_connection(conn_id):
api_key = get_api_key(conn_id)
api_client = ABC(api_key=api_key)
response = api_client.test_connection()
print(response)
> if response['message'] != "pong":
E TypeError: string indices must be integers
../subfolder/includes/abc/functions.py:70: TypeError
-------------------------------------------- Captured stdout call --------------------------------------------
{"message":"NoPong"}
========================================== short test summary info ===========================================
FAILED test_functions.py::test_check_connection_raiseError - TypeError: string indices must be integers
为什么 pytest 抱怨我的 _check_connection
函数?它使用键 message
正确访问字典,并且它在 pytest 之外工作。
你需要return一个字典而不是一个字符串,所以mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')
应该变成:mocker.patch('includes.abc.functions.ABC.test_connection', return_value={"message":"NoPong"})
没有引号。