假设我有这些测试功能:
def test_function_one():
assert # etc...
def test_function_two():
# should only run if test_function_one passes
assert # etc.
如果test_function_one通过,我怎样才能确保test_function_two只运行(我希望它可能)?
修改 我需要这个,因为测试二是使用测试验证的属性。
答案 0 :(得分:5)
您可以使用名为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
答案 1 :(得分:1)
我认为你的解决方案是模拟test1设置的值。
理想情况下,测试应该是独立的,因此请尝试模拟该值,以便随时可以运行test2,实际上,您还应该模拟(模拟)脏值,以便了解test2在接收到意外数据时的行为方式。
答案 2 :(得分:0)
我认为这就是你想要的:
def test_function():
assert # etc...
assert # etc...
这符合您的要求,即第二个“测试”(断言)仅在第一个“测试”(断言)通过时才会运行。
答案 3 :(得分:0)
我正在使用名为pytest-dependency的pytest插件。
添加上述内容-如果要在测试类中使用测试,则必须将测试类名称添加到功能测试名称中。
例如:
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将运行。