如果另一个失败,pytest可以运行测试吗?

时间:2018-11-29 18:21:47

标签: python pytest

我遇到的情况是可以对整个对象进行非常快速的验证检查,并且如果该检查通过,则可以保证该对象是健康的。如果失败,则需要使用耗时的检查来找出有问题的方面。

我希望有这样的东西: “ @ pytest.mark.dependency(depends = [” test_a“]))”,除了仅在成功时运行之外,它只会在失败时运行。

1 个答案:

答案 0 :(得分:2)

正如您正确指出的那样,pytest-dependency无法处理您的案件,因为它跳过了针对失败而非成功的测试。但是,通过对该插件进行一些自定义,您可以获得所需的结果。示例:

# conftest.py

import pytest
from pytest_dependency import DependencyManager


def pytest_collection_modifyitems(session, config, items):
    modules = (item.getparent(pytest.Module) for item in items)
    marked_modules = {m for m in modules if m.get_closest_marker('depend_on_failures')}
    for module in marked_modules:
        module.dependencyManager = FailureDepManager()


class FailureDepManager(DependencyManager):

    def checkDepend(self, depends, item):
        for i in depends:
            if i in self.results:
                if self.results[i].isSuccess():
                    pytest.skip('%s depends on failures in %s' % (item.name, i))
                    break

FailureDepManagerpytest-dependency的{​​{1}}的自定义版本,仅当依赖成功(具有结果DependencyManagerpassed时才跳过依赖测试) 。可悲的是,此行为只能在每个模块的基础上触发,因为这是插件的当前限制(有关详细信息,请参见this question)。用法示例:

XPASS

由于模块级别上的标记import pytest pytestmark = pytest.mark.depend_on_failures @pytest.mark.dependency() @pytest.mark.xfail(reason='simulate failing test') def test_foo(): assert False @pytest.mark.dependency(depends=['test_foo']) def test_bar(): assert True ,如果depend_on_failures失败,test_bar现在将运行:

test_foo
相关问题