相关疑难解决方法(0)

如何在python中将数据模拟为request.Response类型

我想写一些测试用例来练习isinstance(obj, requests.Response) 逻辑中的object_check。在我创建 Mock 数据作为 requests.post 的返回值之后。模拟数据的类型始终是 Mock 类。这样,我如何重写模拟数据,以便模拟数据可以是 requests.Response 类型?所以我可以锻炼线d = obj.json()

from unittest.mock import patch, Mock
import unittest
import requests
from requests.exceptions import HTTPError
import pytest
def object_check(obj):
    if isinstance(obj, bytes):
        d = ujson.decode(obj.decode())
    elif isinstance(obj, requests.Response):
        d = obj.json()
    else:
        raise ValueError('invalid type')
    return d

def service_post(params):
    """
    trivial function that does a GET request
    against google, checks the status of the
    result and returns the raw content
    """
    url = "https://www.iamdomain.com"
    params …
Run Code Online (Sandbox Code Playgroud)

python pytest python-3.x python-unittest python-unittest.mock

5
推荐指数
1
解决办法
1万
查看次数