【问题标题】:Pytest on return value of a function that makes an API call对进行 API 调用的函数的返回值进行 Pytest
【发布时间】:2021-07-29 08:23:39
【问题描述】:

我有一个进行 API 调用的函数,我想将该 API 调用的返回值 (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 之外运行。

【问题讨论】:

  • 您的补丁字符串看起来不正确。 test_connectionABC 中的一个函数,因此您可能需要修补function.ABC 并将所需的值分配给mocked_abc.return_value.test_connection.return_value

标签: python pytest pytest-mock


【解决方案1】:

你需要返回一个字典而不是一个字符串,所以mocker.patch('includes.abc.functions.ABC.test_connection', return_value='{"message":"NoPong"}')应该变成:mocker.patch('includes.abc.functions.ABC.test_connection', return_value={"message":"NoPong"})

没有引号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2020-05-06
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多