当我试图运行此测试时:
from unittest import mock
@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_zeros(self, response):
self.assertEqual(0, 0)
它说TypeError: test_zeros() takes 2 positional arguments but 3 were given
。那么,如何在一次测试中模拟两种不同的方法(我需要requests.get
和requests.post
)?
答案 0 :(得分:1)
装饰器中定义的模拟被传递给修饰函数。我不知道response
应该是什么,但你需要接受get和post对象的参数。
@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_zeros(self, post_mock, get_mock):
self.assertEqual(0, 0)