如果一个测试失败了,如何跳过该类中的其余测试? [英] How to skip the rest of tests in the class if one has failed?

查看:111
本文介绍了如果一个测试失败了,如何跳过该类中的其余测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jenkins,Python,Selenium2(webdriver)和Py.test框架为网络测试创建测试用例.

I'm creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks.

到目前为止,我正在按照以下结构组织测试:

So far I'm organizing my tests in the following structure:

每个测试用例,每个 test_方法是一个测试步骤.

当一切正常时,此设置可以很好地工作,但是当一个步骤崩溃时,其余的测试步骤"就会发疯.我可以借助teardown_class()将故障包含在类(测试用例)中,但是我正在研究如何改进它.

This setup works GREAT when everything is working fine, however when one step crashes the rest of the "Test Steps" go crazy. I'm able to contain the failure inside the Class (Test Case) with the help of teardown_class(), however I'm looking into how to improve this.

我需要的是,如果其中一个失败,则以某种方式跳过(或xfail)一个类中的其余test_方法,以使其余测试用例不运行并标记为FAILED(因为那样误报)

What I need is somehow skip(or xfail) the rest of the test_ methods within one class if one of them has failed, so that the rest of the test cases are not run and marked as FAILED (since that would be false positive)

谢谢!

更新:我不在寻找答案或这是不正确的做法",因为这样称呼是非常有争议的. (每个测试类都是独立的,就足够了.)

UPDATE: I'm not looking or the answer "it's bad practice" since calling it that way is very arguable. (each Test Class is independent - and that should be enough).

更新2:不能在每个测试方法中添加"if"条件-这是很多重复的工作.我正在寻找的(也许)有人知道如何使用钩子到类方法.

UPDATE 2: Putting "if" condition in each test method is not an option - is a LOT of repeated work. What I'm looking for is (maybe) somebody knows how to use the hooks to the class methods.

推荐答案

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

I like the general "test-step" idea. I'd term it as "incremental" testing and it makes most sense in functional testing scenarios IMHO.

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

Here is a an implementation that doesn't depend on internal details of pytest (except for the official hook extensions). Copy this into your 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":

If you now have a "test_step.py" like this:

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原因):

then running it looks like this (using -rx to report on xfail reasons):

(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",因为跳过是针对错误的环境或缺少依赖项,错误的解释器版本.

I am using "xfail" here because skips are rather for wrong environments or missing dependencies, wrong interpreter versions.

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

Note that neither your example nor my example would directly work with distributed testing. For this, the pytest-xdist plugin needs to grow a way to define groups/classes to be sent whole-sale to one testing slave instead of the current mode which usually sends test functions of a class to different slaves.

这篇关于如果一个测试失败了,如何跳过该类中的其余测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆