首页 > 解决方案 > 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 运行,如果测试 2 通过然后测试 3 运行。

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

标签: pythonpython-2.7pytest

解决方案


我猜您正在寻找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_stop并且test_instance_delete仅在test_instance_start成功时运行,否则跳过。但是,由于test_instance_delete不依赖于test_instance_stop,所以无论后者的测试结果如何,前者都会执行。多次运行示例测试类以验证所需的行为。


推荐阅读