如果我在一个类中有多个测试并且前面的测试失败,我如何让它跳过或退出该类而不是测试其余的测试? [英] If I have multiple tests in one class and a preceding test fails, how do I have it skip or exit the class instead of testing the remaining tests?

查看:60
本文介绍了如果我在一个类中有多个测试并且前面的测试失败,我如何让它跳过或退出该类而不是测试其余的测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Python 与 Selenium 和 unittest 结合使用.我在一个班级中有四个测试,因为它们都是相关的单元测试.如果前一个测试失败,我如何让它跳过下一个测试?我已经阅读了 unittest 的跳过方法的所有文档,但没有一个是我所需要的.有没有办法告诉它退出课程?

I'm using Python with Selenium and unittest. I have four tests in one class since they're all related unit tests. How do I have it skip the next test(s) if the preceding test fails? I've read up on all the documentation for unittest's skip methods, but none of it is exactly what I need. Is there a way to tell it to exit the class?

这是我的代码目前的样子:

Here is a jist of what my code currently looks like:

def test_dealer_search_id_contains(self):
    try:
        LoginPage.login(self, ULV.AA_USERNAME, ULV.AA_PASSWORD)
    except TimeoutException:
        add_test_result(3, 5, "This test failed attempting to login with the user's credentials.")
        add_test_result(4, 2, 'This test is blocked due to failure of a preceding test.')
        self.fail(msg='Dealer: Search - Test failure.')

def test_dealer_search_id_exact(self):
    code for this test here

我如何让它跳过剩余的测试或一起退出课程?

How do I have it skip the remaining test or exit the class all together?

感谢您提供的任何帮助.

I appreciate any help that can be provided.

我使用 PyTest 作为我的运行程序,并且在这些之后我确实有其他测试需要继续运行,只是不在此类或 .py 文件中.

I am using PyTest as my runner and I do have additional tests after these that will need to continue to run, just not in this class or .py file.

所以在这种情况下的测试都取决于先前的测试通过,否则它会因与上述测试相同的缺陷而失败.然而,我的主要困境是我不知道如何告诉它跳过这些测试并继续 PyTest 已经选择的其余测试.我查看了 PyTest 的跳过处理,它与 unittest 几乎相同.如果没有必要,我试图避免在逻辑检查中对测试失败进行编码,但这似乎是唯一的解决方案.

So the tests in this case all depend on the prior test passing, otherwise it would fail for the same defect as the above test. However, my main dilemma is I'm not sure how to tell it to skip those tests and continue with the rest of the tests that PyTest has picked up. I looked at PyTest's skip handling and it's pretty much identical to unittest. I'm trying to avoid coding in a logic check for test failures if it's unnecessary, but it appears that may be the only solution.

推荐答案

我想我找到了可以解决这个问题的方法.它在 py.test 框架中称为 incremental testing:https://pytest.org/latest/example/simple.html#incremental-testing-test-steps

I think i found what will solve this problem. It's called incremental testing in py.test framework: https://pytest.org/latest/example/simple.html#incremental-testing-test-steps

我在本地进行了测试 - 工作得很好!

I tested locally - worked like a charm!

使用与 py.test 文档中相同的 conftest.py.测试文件略有不同:

Used same conftest.py as in the py.test docs. Slightly different test file:

# content of test_incremental.py

import pytest

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


class TestAnotherStuff:
    def test_normal(self):
        assert 1

<小时>

mac:[incremental_testing]: py.test -v test_incremental.py 
============================================================================== test session starts ===============================================================================
platform darwin -- Python 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/dmitry.tokarev/Repos/playground/incremental_testing, inifile: 
plugins: hidecaptured-0.1.2, instafail-0.3.0
collected 5 items 

test_incremental.py::TestUserHandling::test_login PASSED
test_incremental.py::TestUserHandling::test_modification FAILED
test_incremental.py::TestUserHandling::test_deletion xfail
test_incremental.py::TestUserHandling::test_foo xfail
test_incremental.py::TestAnotherStuff::test_normal PASSED

==================================================================================== FAILURES ====================================================================================
_______________________________________________________________________ TestUserHandling.test_modification _______________________________________________________________________

self = <test_incremental.TestUserHandling instance at 0x103b7a5a8>

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

test_incremental.py:10: AssertionError
================================================================= 1 failed, 2 passed, 2 xfailed in 0.02 seconds ==================================================================

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

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