首页 > 解决方案 > 如何将 pytest.mark.parametrize 与固定装置一起使用

问题描述

我想用夹具参数化测试。像这样的东西:

def count_lines(path):
    with open(path, 'r') as fh:
        return len(fh.readlines())


@pytest.fixture
def data_path():
    return '/path/to/test/data'


@pytest.fixture
def simple_file(data_path):
    return os.path.join(data_path, 'simpletestfile.ext')


@pytest.fixture
def other_file(data_path):
    return os.path.join(data_path, 'othertestfile.ext')


@pytest.mark.parametrize('given, expected', [
    (simple_file, 3),
    (other_file, 9),
])
def test_count_lines(given, expected):
    observed = count_lines(given)
    assert observed == expected

上面的代码失败了,因为测试没有解析sample_fileandother_file固定装置,而是接收了使测试在open()语句中失败的函数。

为了使它工作,我必须做这样的事情:

@pytest.mark.parametrize('given, expected', [
    (simple_file(data_path()), 3),
    (other_file(data_path()), 9),
])

但这似乎违背了 py.test 的目的。

另一种选择是

class TestCountLines(object):

    TESTDATA = [
        {
            'id': 'simple case',
            'input_file': 'simpletestfile.ext',
            'expected': 3,
        },
        {
            'id': 'other case',
            'input_file': 'othertestfile.ext',
            'expected': 9,
        },
    ]

    @pytest.fixture(params=TESTDATA, ids=lambda x: x['id'])
    def staged_testdata(self, request, data_path):
        datafile = os.path.join(data_path, request.param['input_file'])
        expected = request.param['expected']
        yield datafile, expected

    def test_count_lines(self, staged_testdata):
        given, expected = staged_testdata
        observed = count_lines(given)
        assert observed == expected

然而我发现上面的代码很奇怪,因为staged_testdata夹具产生了两件事,然后我需要在测试之前解包测试数据。那么,如何使用夹具参数化测试?

标签: pythontestingpytest

解决方案


推荐阅读