【问题标题】:Python test mock some methods but not othersPython 测试模拟一些方法,但不模拟其他方法
【发布时间】:2017-11-14 18:47:59
【问题描述】:

如何模拟测试的某个部分而不是另一个。我想获得我的 url 请求的实际返回,但在 dynamodb 部分进一步使用模拟

import requests
from moto import mock_dynamodb
from requests import Session


@mock_dynamodb
def test_this():

    session = requests.Session()
    response = session.get(
        url,
        headers=headers,
        auth=auth)

但是我收到了一个连接错误(如果我不模拟,我就不会收到)

ConnectionError:连接被拒绝:GET

../../.virtualenvs/pw/lib/python2.7/site-packages/moto/packages/responses/responses.py:250: 连接错误

【问题讨论】:

  • 我认为这里的问题不在于您只需要在测试的某些部分进行模拟,而是您实际上应该将此测试一分为二。一个测试你的数据库被模拟,另一个没有模拟。
  • @Eqomatic 我试过了,但请求仍然收到连接错误def get_feed(url): session = requests.Session() response = session.get( url, headers=headers, auth=auth ) return response @mock_dynamodb def test_this(): response = get_feed(url)
  • 你考虑过嘲笑session.get()吗?

标签: python unit-testing pytest moto


【解决方案1】:

moto 不仅捕获到 AWS 端点的所有 http 请求。我建议您使用requests mock 或简单地使用模拟库来模拟特定请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2020-04-12
    • 2013-08-18
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多