您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如果一个测试失败,如何跳过该类中的其余测试?

如果一个测试失败,如何跳过该类中的其余测试?

我喜欢一般的“测试步骤”构想。我将其称为“增量”测试,在功能测试方案恕我直言中最有意义。

这是一个不依赖pytest内部细节的实现(官方钩子扩展除外)。复制到您的conftest.py

import pytest

def pytest_runtest_makereport(item, call):
    if "incremental" in item.keywords:
        if call.excinfo is not None:
            parent = item.parent
            parent._prevIoUsFailed = item

def pytest_runtest_setup(item):
    prevIoUsFailed = getattr(item.parent, "_prevIoUsFailed", None)
    if prevIoUsFailed is not None:
        pytest.xfail("prevIoUs test Failed (%s)" % prevIoUsFailed.name)

如果您现在有一个像这样的“ test_step.py”:

import pytest

@pytest.mark.incremental
class TestUserHandling:
    def test_login(self):
        pass
    def test_modification(self):
        assert 0
    def test_deletion(self):
        pass

然后运行,如下所示(使用-rx报告xfail原因):

(1)hpk@t2:~/p/pytest/doc/en/example/teststep$ py.test -rx
============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.0.dev17
plugins: xdist, bugzilla, cache, oejskit, cli, pep8, cov, timeout
collected 3 items

test_step.py .Fx

=================================== FAILURES ===================================
______________________ TestUserHandling.test_modification ______________________

self = <test_step.TestUserHandling instance at 0x1e0d9e0>

    def test_modification(self):
>       assert 0
E       assert 0

test_step.py:8: AssertionError
=========================== short test summary info ============================
XFAIL test_step.py::TestUserHandling::()::test_deletion
  reason: prevIoUs test Failed (test_modification)
================ 1 Failed, 1 passed, 1 xFailed in 0.02 seconds =================

在这里使用“ xfail”,因为跳过是针对错误的环境或缺少依赖项,错误的解释器版本。

编辑:请注意,您的示例和我的示例都无法直接用于分布式测试。为此,pytest- xdist插件需要发展一种方法来定义要整体出售给一个测试从属的组/类,而不是通常将类的测试功能发送给不同从属的当前模式。

其他 2022/1/1 18:45:00 有465人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶