2

我有一个如下场景:

@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 is not iterable”的错误。如何实现我的目标。我读到我们不能在参数化测试函数中使用夹具作为参数,但我想要一些替代方案。

4

1 回答 1

3

正如 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
于 2018-12-17T16:05:22.717 回答