Pytest:如何跳过在课堂测试,其余如果一个已经失败了? [英] Pytest: how to skip the rest of tests in the class if one has failed?

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

问题描述

我创建测试用例使用詹金斯,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:

每个测试用例每个 测试_ 是< STRONG>测试步骤即可。

此安装程序时一切正常,但是当一步崩溃测试步骤的其余部分发疯的伟大工程。我能够包含类(测试用例)与 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)的测试一个类中_ 方法的其余部分如果其中一个出现故障,这样的测试案例,其余不运行,并标记为失败(因为这将是假阳性)

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:把如果,在每个测试方法的条件是不是一种选择 - 是重复的工作很多。我正在寻找的是(也许)有人知道如何使用挂钩来类的方法。

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的内部细节(除官方钩扩展)的实现:

Here is a an implementation that doesn't depend on internal details of pytest (except for the official hook extensions):

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

在这里,因为跳跃是相当的错误环境或缺少的依赖关系,错间preTER版本。

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.

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

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