倍数模拟。
我想知道其他方法可以用多个补丁实现测试。这样:
class TestFunction(TestCase):
@mock.patch('path.to.value3', side_effect=value3) # Value3 -> Response object
@mock.patch('path.to.value2', return_value='value2')
@mock.patch('path.to.value1', return_value='value1')
def test_function(self, value1, value2, value3):
response = function_being_tested(payload)
assert response.status_code == 200
我尝试添加这样的固定装置,但无法使其正常工作:
class TestUpdateProduct(TestCase):
def test_function(self, mock_function):
response = function_being_tested(payload)
assert response.status_code == 200
@pytest.fixture()
def mock_function(monkeypatch):
monkeypatch.setattr('path.to.value3', value3)
monkeypatch.setattr('path.to.value2','value2')
monkeypatch.setattr('path.to.value1','value1')
I want to know others ways to implement a test with multiple patch.mocks like this:
class TestFunction(TestCase):
@mock.patch('path.to.value3', side_effect=value3) # Value3 -> Response object
@mock.patch('path.to.value2', return_value='value2')
@mock.patch('path.to.value1', return_value='value1')
def test_function(self, value1, value2, value3):
response = function_being_tested(payload)
assert response.status_code == 200
I tried adding a fixture like this but couldnt make it work:
class TestUpdateProduct(TestCase):
def test_function(self, mock_function):
response = function_being_tested(payload)
assert response.status_code == 200
@pytest.fixture()
def mock_function(monkeypatch):
monkeypatch.setattr('path.to.value3', value3)
monkeypatch.setattr('path.to.value2','value2')
monkeypatch.setattr('path.to.value1','value1')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论