如果一个失败了,如何跳过课堂上的其余测试?

How to skip the rest of tests in the class if one has failed?(如果一个失败了,如何跳过课堂上的其余测试?)
本文介绍了如果一个失败了,如何跳过课堂上的其余测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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:

每个Class都是Test Case,每个test_方法都是Test Step.

当一切正常时,此设置效果很好,但是当一个步骤崩溃时,其余的测试步骤"就会变得疯狂.在 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_ 方法,以便其余测试用例不会运行并标记为失败(因为那会是误报)

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

build conda package from local python package(从本地 python 包构建 conda 包)
How can I see all packages that depend on a certain package with PIP?(如何使用 PIP 查看依赖于某个包的所有包?)
How to organize multiple python files into a single module without it behaving like a package?(如何将多个 python 文件组织到一个模块中而不像一个包一样?)
Check if requirements are up to date(检查要求是否是最新的)
How to upload new versions of project to PyPI with twine?(如何使用 twine 将新版本的项目上传到 PyPI?)
Why #egg=foo when pip-installing from git repo(为什么从 git repo 进行 pip 安装时 #egg=foo)