2012-05-05 69 views
7

比方说,我有这方面的测试功能:如果另一个测试用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通过(我希望这是可能的)运行?

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

+1

你能解释一下你为什么需要这个?第一个测试是否设置了第二个测试用途?这通常很糟糕。 – loganfsmyth

+1

这通常是一个脆弱测试的标志,这个测试依赖于比测试的单元更多的东西,你确定*你需要这样做吗?它可能(可能)更好地重构测试中的代码和/或代码以避免这种依赖性。 –

+0

@loganfsmyth没有第一个测试没有设置任何东西,但第二个测试*使用第一个测试验证的属性。 –

回答

0

我想这是你想要什么:

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

这符合你的要求,即第二次“测试”(断言)只运行如果第一个“测试”(断言)通过。

1

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

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

1

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

的代码可以是这样的:

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 
相关问题