Python 在使用pytest时,如何组织来自多个测试函数的步骤?

Python 在使用pytest时,如何组织来自多个测试函数的步骤?,python,pytest,Python,Pytest,我有三个测试用例,其中两个依赖于第三个。也就是说,测试test\u inner\u 1和test\u inner\u 2彼此独立,但如果test\u outher失败,它们的执行就没有意义。如果测试test\u outher通过,则应运行这两个测试,如果test\u outher失败,则应跳过这两个测试 pytest手册 提供了一些简单的示例,说明如何使用实现增量测试 测试步骤。我正试图将这种方法应用到我的处境和工作中 要实现类似的功能: conftest.py的内容: import pytes

我有三个测试用例,其中两个依赖于第三个。也就是说,测试
test\u inner\u 1
test\u inner\u 2
彼此独立,但如果
test\u outher
失败,它们的执行就没有意义。如果测试
test\u outher
通过,则应运行这两个测试,如果
test\u outher
失败,则应跳过这两个测试

pytest
手册 提供了一些简单的示例,说明如何使用实现增量测试 测试步骤。我正试图将这种方法应用到我的处境和工作中 要实现类似的功能:

conftest.py的内容:

import pytest

def pytest_runtest_makereport(item, call):
    if "incremental" in item.keywords:
        if call.excinfo is not None:
            parent = item.parent
            parent._previousfailed = item


def pytest_runtest_setup(item):
    if "incremental" in item.keywords:
        previousfailed = getattr(item.parent, "_previousfailed", None)
        if previousfailed is not None:
            pytest.xfail("previous test failed (%s)" % previousfailed.name)
import pytest

@pytest.mark.incremental
class TestUserHandling:
    def test_outher(self):
        assert 0

    class TestInner:
        def test_inner_1(self):
            assert 0

        def test_inner_2(self):
            pass
测试内容\u示例.py:

import pytest

def pytest_runtest_makereport(item, call):
    if "incremental" in item.keywords:
        if call.excinfo is not None:
            parent = item.parent
            parent._previousfailed = item


def pytest_runtest_setup(item):
    if "incremental" in item.keywords:
        previousfailed = getattr(item.parent, "_previousfailed", None)
        if previousfailed is not None:
            pytest.xfail("previous test failed (%s)" % previousfailed.name)
import pytest

@pytest.mark.incremental
class TestUserHandling:
    def test_outher(self):
        assert 0

    class TestInner:
        def test_inner_1(self):
            assert 0

        def test_inner_2(self):
            pass
不幸的是,我得到了输出

==============================================2失败,1在0.03秒内通过==========================

而期望得到输出

=========================1失败,2 X在0.03秒内失败==========================


如何更正
conftest.py
以获得所需的行为?

有一个名为pytest的插件,它完成了本例中所需的操作

您的代码可以如下所示:

import pytest
import pytest_dependency

@pytest.mark.dependency()
def test_outher():
   assert 0

@pytest.mark.dependency(depends=["test_outher"])
def test_inner_1():
    assert 0

@pytest.mark.dependency(depends=["test_outher"])
def test_inner_2():
    pass
输出为:

=================================== FAILURES ===================================
_________________________________ test_outher __________________________________

@pytest.mark.dependency()
def test_outher():
>      assert 0
E      assert 0

test_example.py:6: AssertionError
===================== 1 failed, 2 skipped in 0.02 seconds ======================
当然,您可以使用类,但对于本例来说,这不是必需的。如果您需要类的示例,请告诉我。

您也可以使用创建包含测试的测试套件并声明依赖项,它的行为方式相同(如果outher失败,则跳过inner1和inner2):

编辑:新的生成器模式更方便:

from pytest_steps import test_steps, optional_step

@test_steps('step_outher', 'step_inner_1', 'step_inner_2')
def test_suite():
    # Step outher
    assert 1
    yield

    # Step inner 1
    with optional_step('step_inner_1') as step_inner_1:
        assert 0
    yield step_inner_1

    # Step inner 2
    with optional_step('step_inner_2') as step_inner_2:
        assert 0
    yield step_inner_2
传统答案:

from pytest_steps import test_steps, depends_on

def step_outher():
   assert 0

@depends_on(step_outher)
def step_inner_1():
    assert 0

@depends_on(step_outher)
def step_inner_2():
    pass

@test_steps(step_outher, step_inner_1, step_inner_2)
def test_suite(test_step):
    # Execute the step
    test_step()

您还可以使用它在测试步骤之间共享中间结果,有关详细信息,请参阅文档。(顺便说一句,我是作者:)

可能的重复是另一个问题。我需要对两个测试进行分组,使它们都运行/跳过,这取决于通过的其他测试是否失败。这两项测试相互独立。