【发布时间】:2018-03-19 04:34:29
【问题描述】:
我想为 conftest.py 中的 pytest 固定装置编写单元测试
如何模拟装饰器 pytest.fixture?
Conftest.py
import pytest
@pytest.fixture(scope="session", autouse="True")
def get_ip(dict_obj):
"""Assume some functionality"""
return dict_obj.get('ip')
@pytest.fixture(scope="class")
def get_server(create_obj):
"""Assume some functionality"""
pass
test_conftest.py
mock_fixture = patch('pytest.fixture', lambda x : x).start()
from tests.conftest import get_ip
class TestConftestTests:
def test_mgmt_ip(self):
assert mgmt_ip({"ip": "10.192.174.15"}) == "10.192.174.15"
E TypeError: <lambda>() got an unexpected keyword argument 'scope'
当我在导入要测试的函数之前尝试在测试模块的开头 mock pytest.fixture 时,出现错误 -
E TypeError: <lambda>() got an unexpected keyword argument 'scope'
如果我删除 lambda 函数,我会收到 E AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15'
test_conftest.py
patch('pytest.fixture').start()
from tests.conftest import get_ip
class TestConftestTests:
def test_mgmt_ip(self):
assert mgmt_ip({"ip": "10.192.174.15"}) == "10.192.174.15"
E AssertionError: assert <MagicMock name='fixture()()()' id='4443565456'> == '10.192.174.15'
有人可以帮我解决这个错误吗?谢谢!
【问题讨论】: