Python 对多个功能重复相同的测试
我正在编写一个数据库客户端,希望确保日志记录和重试机制对我的所有CRUD方法都有效 是否有办法对列表中的所有方法重复相同的测试 或者这里的最佳实践是什么Python 对多个功能重复相同的测试,python,pytest,repeat,Python,Pytest,Repeat,我正在编写一个数据库客户端,希望确保日志记录和重试机制对我的所有CRUD方法都有效 是否有办法对列表中的所有方法重复相同的测试 或者这里的最佳实践是什么 @patch_whatever def test_all(self,log_mock,execute_mock): db = DBClient() l = [db.get1,db.get2] for function in l: function()
@patch_whatever
def test_all(self,log_mock,execute_mock):
db = DBClient()
l = [db.get1,db.get2]
for function in l:
function()
self.assertEqual(3, log_mock.call_count)
self.assertEqual(3, execute_moock.call_count)
在这种情况下,不会重置断言。我该怎么走?我应该尝试一些参数化测试吗?您可以使用夹具来实现这一点
import pytest
@pytest.fixture(scope="module")
def db_client():
return DBClient()
@pytest.fixture(scope="module")
def db_function(request, db_client):
for func in [db_client.get1, db_client.get2]:
yield func
@patch_whatever
def test_all(self, log_mock, execute_mock, db_function):
db_function()
self.assertEqual(3, log_mock.call_count)
self.assertEqual(3, execute_mock.call_count)
你可以用固定装置
import pytest
@pytest.fixture(scope="module")
def db_client():
return DBClient()
@pytest.fixture(scope="module")
def db_function(request, db_client):
for func in [db_client.get1, db_client.get2]:
yield func
@patch_whatever
def test_all(self, log_mock, execute_mock, db_function):
db_function()
self.assertEqual(3, log_mock.call_count)
self.assertEqual(3, execute_mock.call_count)
对参数化测试(检查),函数可以是参数。感谢您的输入,这确实是答案。参数化测试(检查),函数可以是参数。感谢您的输入,这确实是答案