pytest 依赖跳过参数化测试

pytest dependency skips parameterized tests

我正在使用 pytest 编写测试。我有两个测试,一个依赖于另一个,我为此使用 pytest-dependency==0.5.1。当我有两个相互依赖但都参数化的测试时,会发生一些奇怪的事情——即使独立测试成功,依赖测试也会被跳过。这是我的代码:

import pytest

@pytest.mark.parametrize('par1', ['val1', 'val2', 'val3'])
@pytest.mark.dependency()
def test_a(par1):
    print('hi from test a')
    assert 1 == 1

@pytest.mark.parametrize('par2', ['val21', 'val22', 'val23'])
@pytest.mark.dependency(depends=["test_a"])
def test_b(par2):
    print('hi from test c')

当我 运行 pytest 时,我得到:

pytest --log-cli-level=INFO
================================================================================================= test session starts ==================================================================================================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/username/dev/tests/test
plugins: dependency-0.5.1, mock-3.1.1, anyio-2.0.2, dash-1.16.1, celery-4.4.7, allure-pytest-2.8.21
collected 6 items                                                                                                                                                                                                      

test_something.py::test_a[val1] PASSED                                                                                                                                                                           [ 16%]
test_something.py::test_a[val2] PASSED                                                                                                                                                                           [ 33%]
test_something.py::test_a[val3] PASSED                                                                                                                                                                           [ 50%]
test_something.py::test_b[val21] SKIPPED                                                                                                                                                                         [ 66%]
test_something.py::test_b[val22] SKIPPED                                                                                                                                                                         [ 83%]
test_something.py::test_b[val23] SKIPPED                                                                                                                                                                         [100%]

=============================================================================================   3 passed, 3 skipped in 0.05s =============================================================================================

如果我取消参数化,一切都很好:

 pytest --log-cli-level=INFO
================================================================================================= test session starts ==================================================================================================
platform linux -- Python 3.7.9, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /home/username/dev/tests/test
plugins: dependency-0.5.1, mock-3.1.1, anyio-2.0.2, dash-1.16.1, celery-4.4.7, allure-pytest-2.8.21
collected 2 items                                                                                                                                                                                                      

test_something.py::test_a PASSED                                                                                                                                                                                 [ 50%]
test_something.py::test_b PASSED                                                                                                                                                                                 [100%]

================================================================================================== 2 passed in 0.01s ===================================================================================================

为什么会发生这种情况,我该如何解决?

问题是测试的名称包含测试参数,例如test_a[val1] 等,并且 pytest-dependency 找不到名为 test_a 的测试。要解决这个问题,您只需将名称添加到依赖项标记 - 在这种情况下 pytest-dependency 会忽略真实的测试名称并使用这个名称:

import pytest

@pytest.mark.parametrize('par1', ['val1', 'val2', 'val3'])
@pytest.mark.dependency(name='test_a')
def test_a(par1):
    print('hi from test a')
    assert 1 == 1

@pytest.mark.parametrize('par2', ['val21', 'val22', 'val23'])
@pytest.mark.dependency(depends=['test_a'])
def test_b(par2):
    print('hi from test b')

现在,如果 test_a 测试中的 所有 测试成功,则执行测试。