0

我正在使用 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 ===================================================================================================

为什么会发生,我该如何解决?

4

1 回答 1

2

The problem is that the name of the tests contains the test parameter, e.g. test_a[val1] etc., and pytest-dependency cannot find a test named test_a. To solve this you can just add the name to the dependency marker - in this case pytest-dependency ignores the real test name and uses this one:

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')

Now the test is executed if all of the test_a tests succeed.

于 2021-01-13T16:30:41.753 回答