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

Te-*_*ers 10 python pytest

假设我有这些测试功能:

def test_function_one():
    assert # etc...

def test_function_two():
    # should only run if test_function_one passes
    assert # etc.
Run Code Online (Sandbox Code Playgroud)

如果test_function_one通过,我怎样才能确保test_function_two只运行(我希望它可能)?

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

Jak*_*ner 5

您可以将pytest插件称为pytest-dependency

代码如下所示:

import pytest
import pytest_dependency

@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
Run Code Online (Sandbox Code Playgroud)

  • 这确实为我跳过了“test_function_two”,但它*在“test_function_one”通过时也会跳过* (2认同)
  • @Gulzar我遇到了同样的问题,并意识到您还需要装饰所依赖的函数,即使它不依赖任何东西。在此示例中,确保“test_function_one”上有“@pytest.mark.dependency()”。 (2认同)

Mar*_*hio -1

我想这就是你想要的:

def test_function():
    assert # etc...
    assert # etc...
Run Code Online (Sandbox Code Playgroud)

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