Python 如何在pytest中的mark.parametrize中使用夹具
我有一个如下的场景:Python 如何在pytest中的mark.parametrize中使用夹具,python,unit-testing,testing,pytest,fixtures,Python,Unit Testing,Testing,Pytest,Fixtures,我有一个如下的场景: @pytest.fixture(scope="module", params=[5, 10]) def get_data(request): data = [] for i in range(request.param): data.append((i, 2)) return data @pytest.mark.parametrize(('test_input','expected'), get_data) def test_da
@pytest.fixture(scope="module", params=[5, 10])
def get_data(request):
data = []
for i in range(request.param):
data.append((i, 2))
return data
@pytest.mark.parametrize(('test_input','expected'), get_data)
def test_data_types(test_input, expected):
assert (test_input%expected) == 0
但我得到了一个错误“TypeError:‘function’object不可编辑”。如何实现我的目标。我了解到我们不能在参数化测试函数中使用fixture作为参数,但我需要一些替代方法。正如hoefling提到的,您可以使用普通函数来获取数据。这里有一个简单的例子。 我在每个测试文件中都有一个get_data()函数,它从excel文件的不同工作表中提取数据
from utils.excel_utils import ExcelUtils
import pytest
def get_data():
data = ExcelUtils("inputData.xlsx", "Session").get_input_rows()
for row in data:
yield row
@pytest.mark.parametrize("test_input", get_data())
def test_session(test_input):
print(test_input)
assert "session" in test_input
pytest
不支持在mark.parametrize
中使用夹具作为参数,请参阅。这就是我问这个问题的原因。我要求另一种选择,即不使用固定装置作为参数。使用变量或普通函数。嗨,在我的例子中,我想在参数化中调用的函数正在使用fixture,而我不能在函数调用中使用这些fixture。有工作吗?@GopinathS-嗨,问一个新的问题和更多细节可能是个好主意。是的,我在这里发布了一个。还没有收到任何答复。你能帮上忙吗?