Pytest中如何控制增量测试用例

How to control the incremental test case in Pytest

@pytest.mark.incremental
class Test_aws():

    def test_case1(self):
        ----- some code here ----
        result = someMethodTogetResult
        assert result[0] == True
        orderID = result[1]

    def test_case2(self):
        result = someMethodTogetResult # can be only perform once test case 1 run successfully.
        assert result == True

    def test_deleteOrder_R53HostZonePrivate(self):
        result = someMethodTogetResult
        assert result[0] == True

当前行为是,如果测试 1 通过,则测试 2 运行s,如果测试 2 通过,则测试 3 运行s。

我需要的是: 如果test_case 3 应该是运行 如果test_case 1 通过。 test_case 2 不应改变任何行为。有什么想法吗?

我猜您正在寻找 pytest-dependency,它允许设置测试之间的条件 运行 依赖关系。示例:

import random
import pytest


class TestAWS:

    @pytest.mark.dependency
    def test_instance_start(self):
        assert random.choice((True, False))

    @pytest.mark.dependency(depends=['TestAWS::test_instance_start'])
    def test_instance_stop(self):
        assert random.choice((True, False))

    @pytest.mark.dependency(depends=['TestAWS::test_instance_start'])
    def test_instance_delete(self):
        assert random.choice((True, False))

test_instance_stoptest_instance_delete 仅当 test_instance_start 成功时才会 运行,否则跳过。但是,由于test_instance_delete不依赖于test_instance_stop,所以无论后者的测试结果如何,前者都会执行。 运行 示例测试 class 几次以验证所需的行为。

补充 , another option is to use pytest-steps 以执行增量测试。如果您希望在步骤之间共享某种增量 state/intermediate 结果,这尤其可以帮助您。

但是它没有像 pytest-dependency 那样实现高级依赖机制,因此请使用更适合您目标的包。

使用 pytest-steps,hoefling 的例子会写成:

import random
from pytest_steps import test_steps, depends_on

def step_instance_start():
    assert random.choice((True, False))

@depends_on(step_instance_start)
def step_instance_stop():
    assert random.choice((True, False))

@depends_on(step_instance_start)
def step_instance_delete():
    assert random.choice((True, False))

@test_steps(step_instance_start, step_instance_stop, step_instance_delete)
def test_suite(test_step):
    # Execute the step
    test_step()

编辑:有一个新的 'generator' 模式使它更容易:

import random
from pytest_steps import test_steps, optional_step

@test_steps('step_instance_start', 'step_instance_stop', 'step_instance_delete')
def test_suite():
    # First step (Start)
    assert random.choice((True, False))
    yield

    # Second step (Stop)
    with optional_step('step_instance_stop') as stop_step:
        assert random.choice((True, False))
    yield stop_step

    # Third step (Delete)
    with optional_step('step_instance_delete') as delete_step:
        assert random.choice((True, False))
    yield delete_step

查看 documentation 了解详情。 (顺便说一下,我是这个包的作者;))

您可以使用pytest-ordering package to order your tests using pytest mark. The author of the package explains the usage here

示例:

@pytest.mark.first
def test_first():
    pass

@pytest.mark.second
def test_2():
    pass


@pytest.mark.order5
def test_5():
    pass