在pytestdependency中,是否有一种方法仅在多个参数化测试用例全部通过时执行测试?

2024-04-28 12:51:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pytest中运行了一系列测试,这些测试依赖于类中运行的一系列测试。我正在使用pytest依赖项在另一个模块中运行一些其他测试,但前提是该依赖项类中的所有测试都通过

这是一组测试,我必须通过,才能继续进行其余的测试。类中有两个方法:

@pytest.mark.dependency()
class TestThis:
    """Test steady state of network."""

    @pytest.mark.parametrize("device", params_this)
    def test_that(self, device: str) -> None:
        do something
        assert xyz == abc

    @pytest.mark.parametrize("device", params_that)
    def test_this_as_well(self, device: str) -> None:
        do something
        assert xyz == abc

现在,当我在接下来的测试中只添加一个依赖项标记时,它会按预期工作。如果TestThis::test_中的任何测试失败,则跳过其余测试

@pytest.mark.dependency(
    depends=instances(

            "tests/test_xyz.py::TestThis::test_that",
            params_this,
        ),
    scope="session",
)
class TestEverything:
    """Class to test everything."""

但是,当我添加两个类似于下面的依赖项标记时,即使依赖项中的一个或多个测试失败,测试也会照常进行。这是一种意想不到的行为

@pytest.mark.dependency(
    depends=instances(

            "tests/test_xyz.py::TestThis::test_that",
            params_this,
        ),
    scope="session",
)
@pytest.mark.dependency(
    depends=instances(

            "tests/test_xyz.py::TestThis::test_this_as_well",
            params_that,
        ),
    scope="session",
)
class TestEverything:
    """Class to test everything."""

寻找这个问题的可能解决方案,因为我无法将这两个依赖项方法组合成一个,以便测试套件的其余部分使用


Tags: instancespytestthatpytestdevicetestsparams