过滤 pytest 夹具

2023-12-20

这基本上与老问题 https://stackoverflow.com/questions/47696002/chaining-pytest-fixtures但希望现在有更好的解决方案。

问题:给定一个参数化夹具,如何使用夹具对象的子集参数化测试函数?

Example:

import pytest

@pytest.fixture(params=range(7))
def square(request):
    return request.param ** 2

def test_all_squares(square):
    sqrt = square ** 0.5
    assert sqrt == int(sqrt)

@pytest.fixture()
def odd_square(square):
    if square % 2 == 1:
        return square
    pytest.skip()

def test_all_odd_squares(odd_square):
    assert odd_square % 2 == 1
    sqrt = odd_square ** 0.5
    assert sqrt == int(sqrt)

Output:


$ pytest pytests.py  -v
=================================================================== test session starts ===================================================================
...
collected 14 items                                                                                                                                        

pytests.py::test_all_squares[0] PASSED                                                                                                              [  7%]
pytests.py::test_all_squares[1] PASSED                                                                                                              [ 14%]
pytests.py::test_all_squares[2] PASSED                                                                                                              [ 21%]
pytests.py::test_all_squares[3] PASSED                                                                                                              [ 28%]
pytests.py::test_all_squares[4] PASSED                                                                                                              [ 35%]
pytests.py::test_all_squares[5] PASSED                                                                                                              [ 42%]
pytests.py::test_all_squares[6] PASSED                                                                                                              [ 50%]
pytests.py::test_all_odd_squares[0] SKIPPED                                                                                                         [ 57%]
pytests.py::test_all_odd_squares[1] PASSED                                                                                                          [ 64%]
pytests.py::test_all_odd_squares[2] SKIPPED                                                                                                         [ 71%]
pytests.py::test_all_odd_squares[3] PASSED                                                                                                          [ 78%]
pytests.py::test_all_odd_squares[4] SKIPPED                                                                                                         [ 85%]
pytests.py::test_all_odd_squares[5] PASSED                                                                                                          [ 92%]
pytests.py::test_all_odd_squares[6] SKIPPED                                                                                                         [100%]

============================================================== 10 passed, 4 skipped in 0.02s ==============================================================
  

虽然这有效,但并不理想:

  • 它创建总是被跳过的虚拟测试用例
  • 它需要使用过滤后的夹具来给出不同名称的测试函数(odd_square) 到参数。

我想要的是例如Afilter_fixture(predicate, fixture)过滤原始灯具对象的函数,可以传递给pytest.mark.parametrize,像这样:

@pytest.mark.parametrize("square", filter_fixture(lambda x: x % 2 == 1, square))
def test_all_odd_squares(square):
    assert square % 2 == 1
    sqrt = square ** 0.5
    assert sqrt == int(sqrt)

这在某种程度上可行吗?


如果您需要一定程度的逻辑来确定将哪些参数应用于每个测试,您可能需要考虑使用pytest_generate_tests hook. https://docs.pytest.org/en/latest/how-to/parametrize.html#basic-pytest-generate-tests-example

钩子函数pytest_generate_tests每个收集的测试都会被调用。这metafuncargument 允许您动态参数化每个单独的测试用例。重写您的示例以使用pytest_generate_tests可能看起来像这样:

def pytest_generate_tests(metafunc):
    square_parameters = (x**2 for x in range(7))
    if 'square' in metafunc.fixturenames:
        metafunc.parametrize("square", square_parameters)
    if 'odd_square' in metafunc.fixturenames:
        odd_square_parameters = (x for x in square_parameters if x % 2 == 1)
        metafunc.parametrize("odd_square", odd_square_parameters)

def test_all_squares(square):
    sqrt = square ** 0.5
    assert sqrt == int(sqrt)

def test_all_odd_squares(odd_square):
    assert odd_square % 2 == 1
    sqrt = odd_square ** 0.5
    assert sqrt == int(sqrt)

这会导致运行以下测试用例:

$ pytest -v pytests.py 
=========== test session starts ===========
…
collected 10 items

pytests.py::test_all_squares[0] PASSED                                                                                                                                                                                     [ 10%]
pytests.py::test_all_squares[1] PASSED                                                                                                                                                                                     [ 20%]
pytests.py::test_all_squares[4] PASSED                                                                                                                                                                                     [ 30%]
pytests.py::test_all_squares[9] PASSED                                                                                                                                                                                     [ 40%]
pytests.py::test_all_squares[16] PASSED                                                                                                                                                                                    [ 50%]
pytests.py::test_all_squares[25] PASSED                                                                                                                                                                                    [ 60%]
pytests.py::test_all_squares[36] PASSED                                                                                                                                                                                    [ 70%]
pytests.py::test_all_odd_squares[1] PASSED                                                                                                                                                                                 [ 80%]
pytests.py::test_all_odd_squares[9] PASSED                                                                                                                                                                                 [ 90%]
pytests.py::test_all_odd_squares[25] PASSED                                                                                                                                                                                [100%]

=========== 10 passed in 0.03s ===========

请注意,我的示例中的测试 ID 与您的略有不同。但是,您可以使用以下方法提供显式测试标识符ìds的论证metafunc.parametrize.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

过滤 pytest 夹具 的相关文章

随机推荐