生产多个pytest灯具

时间:2017-06-14 22:08:54

标签: python pytest fixtures

我正在测试一些工程方程式,其中包含多个返回浮点数组的参数。为了测试所有的情况,我想使用一些固定装置。我目前,将测试用例存储在一个简单的文件中,加载它们,然后返回感兴趣的测试用例。有更好的方法吗?

@pytest.fixture(params=[0, 1, 2])
def test_case(request):
    fpath = pathlib.Path(__file__).parent
    fpath /= 'test_data' / 'test_cases.json'
    test_cases = json.load(fpath.open())
    return test_cases[request.param]

def test_function(test_case):
    # Run the test

1 个答案:

答案 0 :(得分:0)

您可以使用pytest_generate_tests

def pytest_generate_tests(metafunc):
    if 'test_case' in metafunc.fixturenames:
        fpath = pathlib.Path(__file__).parent
        fpath /= 'test_data' / 'test_cases.json'
        test_cases = json.load(fpath.open())
        metafunc.fixturenames.append('test_case')
        metafunc.parametrize(
            'test_case',
            test_cases)
相关问题