在 py.test 中模拟 REST 时出现导入问题
Import issue while mocking REST in py.test
我尝试在 Python 中为 REST 客户端编写一些单元测试。我无法模拟服务器的响应。
具体来说,我想要一个 GET 查询 return 一个预先准备好的 JSON 字符串而不连接到服务器。正如我在 Python 中学到的那样,方法是使用 unittest.mock
或 pytest-mock
。我的项目结构是这样的:
workspace/__init__.py
workspace/module/__init__.py
workspace/module/connector.py
workspace/tests/__init__.py
workspace/tests/test_connector.py
我把测试代码放在了test_connector.py
。我尝试以这种方式使用 unittest.mock.patch
:
@patch()
test_get_container_list(mocker):
(....)
以及pytest-mock
:
mocker.patch('module.connector.Connector.get_container_list.requests.sessions.get')
mocker.return_value.ok = True
mocker.return_value.json.return_value = container_list
然而我被这个错误难住了:
ImportError: No module named 'module.connector.Connector'; 'module.connector' is not a package
我也尝试过修改路径中的内容,但我得到的只是:
ImportError: No module named 'Connector'
ImportError: No module named 'connector.Connector'; 'connector' is not a package
我用这个命令启动了测试:
pytest 测试/
来自 module
目录。我的导入看起来像这样:
import configparser
import pytest
from module.connector import Connector
环境: Ubuntu 16.04, Python 3.5.2, pytest-3.2.5, py-1.5.2, pluggy-0.4.0 ;插件:mock-1.6.3、cov-2.5.1
所有剩余的测试都没有问题,只有这个带有模拟的测试没有。
我的问题:如何使模拟测试正常工作? (我使用 pytest-mock
或 unittest.mock
时会出现导入错误)。
我走了另一条路,我没有使用 pytest-mock
,而是使用了 request-mock
。这里的方法是使用 requests
正常执行 HTTP 请求并模拟特定 URL.
的响应
import requests
import requests_mock
def test_requests():
with requests_mock.Mocker() as mock:
session = requests.Session()
url = 'https://google.com/'
mock.register_uri('GET', url, text='{"answer": "My return text"}') # mocking the http response within this context
resp = session.get('https://google.com/')
assert resp.json()['answer'] == 'My return text'
查看更多信息here and probably you'll want to check regarding request matching here。
希望这能给您一些启发,找到另一种方法。
我认为这个问题可能是 Pytest 和导入的问题,从我在这里遇到的类似问题的数量来看,基本上每个人(包括我自己)似乎都有。
This answer 对我特别有帮助。
我尝试在 Python 中为 REST 客户端编写一些单元测试。我无法模拟服务器的响应。
具体来说,我想要一个 GET 查询 return 一个预先准备好的 JSON 字符串而不连接到服务器。正如我在 Python 中学到的那样,方法是使用 unittest.mock
或 pytest-mock
。我的项目结构是这样的:
workspace/__init__.py
workspace/module/__init__.py
workspace/module/connector.py
workspace/tests/__init__.py
workspace/tests/test_connector.py
我把测试代码放在了test_connector.py
。我尝试以这种方式使用 unittest.mock.patch
:
@patch()
test_get_container_list(mocker):
(....)
以及pytest-mock
:
mocker.patch('module.connector.Connector.get_container_list.requests.sessions.get')
mocker.return_value.ok = True
mocker.return_value.json.return_value = container_list
然而我被这个错误难住了:
ImportError: No module named 'module.connector.Connector'; 'module.connector' is not a package
我也尝试过修改路径中的内容,但我得到的只是:
ImportError: No module named 'Connector'
ImportError: No module named 'connector.Connector'; 'connector' is not a package
我用这个命令启动了测试:
pytest 测试/
来自 module
目录。我的导入看起来像这样:
import configparser
import pytest
from module.connector import Connector
环境: Ubuntu 16.04, Python 3.5.2, pytest-3.2.5, py-1.5.2, pluggy-0.4.0 ;插件:mock-1.6.3、cov-2.5.1
所有剩余的测试都没有问题,只有这个带有模拟的测试没有。
我的问题:如何使模拟测试正常工作? (我使用 pytest-mock
或 unittest.mock
时会出现导入错误)。
我走了另一条路,我没有使用 pytest-mock
,而是使用了 request-mock
。这里的方法是使用 requests
正常执行 HTTP 请求并模拟特定 URL.
import requests
import requests_mock
def test_requests():
with requests_mock.Mocker() as mock:
session = requests.Session()
url = 'https://google.com/'
mock.register_uri('GET', url, text='{"answer": "My return text"}') # mocking the http response within this context
resp = session.get('https://google.com/')
assert resp.json()['answer'] == 'My return text'
查看更多信息here and probably you'll want to check regarding request matching here。
希望这能给您一些启发,找到另一种方法。
我认为这个问题可能是 Pytest 和导入的问题,从我在这里遇到的类似问题的数量来看,基本上每个人(包括我自己)似乎都有。
This answer 对我特别有帮助。