繁体   English   中英

如果 py.test 的另一个测试失败,我如何跳过测试?

[英]How can I skip a test if another test fails with py.test?

假设我有这些测试功能:

def test_function_one():
    assert # etc...

def test_function_two():
    # should only run if test_function_one passes
    assert # etc.

如何确保 test_function_two 仅在 test_function_one 通过时运行(我希望这是可能的)?

编辑:我需要这个,因为测试二正在使用测试一验证的属性。

您可以使用名为pytest-dependency 的 pytest插件。

代码可能如下所示:

import pytest

@pytest.mark.dependency()   #First test have to have mark too
def test_function_one():
    assert 0, "Deliberate fail"

@pytest.mark.dependency(depends=["test_function_one"])
def test_function_two():
    pass   #but will be skipped because first function failed

我认为你的解决方案是模拟 test1 设置的值。

理想情况下,测试应该是独立的,因此尝试模拟该值,以便您可以随时运行 test2,事实上,您还应该模拟(模拟)脏值,以便您可以查看 test2 在收到意外数据时的行为。

我认为这就是你想要的:

def test_function():
    assert # etc...
    assert # etc...

这满足您的要求,即第二个“测试”(断言)仅在第一个“测试”(断言)通过时运行。

我正在使用名为pytest-dependency 的 pytest插件。

添加到上述内容 - 如果您在测试类中使用测试 - 您必须将测试类名称添加到函数 test name

例如:

import pytest


class TestFoo:

    @pytest.mark.dependency()
    def test_A(self):
        assert False

    @pytest.mark.dependency(depends=['TestFoo::test_A'])
    def test_B(self):
        assert True

因此,如果 test_A 失败 - test_B 将不会运行。 如果 test_A 通过 - test_B 将运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM