简体   繁体   English

如果 pytest 中的特定测试失败,如何停止在模块中运行其他测试?

[英]How to stop running other tests in a module if a specific test fails in pytest?

I have a module with tests for pytest.我有一个对 pytest 进行测试的模块。 There are many tests there, and the fist checks if there is a connectivity to a target.那里有很多测试,拳头检查是否有与目标的连接。 If this test fails, there is no reason to wait for all other tests in this particular module to timeout.如果此测试失败,则没有理由等待此特定模块中的所有其他测试超时。 I'd like to stop all tests in the module if this one specific test failed.如果这一特定测试失败,我想停止模块中的所有测试。

I found pytest.exit() , but it's too strong, it terminates the whole pytest run.我找到pytest.exit() ,但它太强大了,它终止了整个 pytest 运行。 I have other modules to work with, so I want to skip/fail quickly all other tests in only one (current) module.我还有其他模块可以使用,所以我想在一个(当前)模块中快速跳过/失败所有其他测试。

Are there a way to do this?有没有办法做到这一点?

You can use @pytest.mark.skipif annotation instead of running another test first您可以使用@pytest.mark.skipif注释而不是先运行另一个测试

def has_connection():
    return True


@pytest.mark.skipif(not has_connection(), reason='No connection')
def test_example(self):
    pass

If all the tests are under class you can add the annotation to the class, this will skip all the tests如果所有测试都在 class 下,您可以将注释添加到 class,这将跳过所有测试

@pytest.mark.skipif(not has_connection(), reason='No connection')
class TestExample:

    def test_example1(self):
        pass

    def test_example2(self):
        pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行单个测试有效,但运行多个测试失败 - Flask 和 Pytest - Running a single test works, but running multiple tests fails - Flask and Pytest pytest_generate_tests 中的 pytest.skip 跳过模块中的所有测试函数而不是特定测试 - pytest.skip within pytest_generate_tests skips all test functions in module instead of specific tests 使用模块共享夹具运行pytest单元测试的PyDev失败 - PyDev running pytest unit test with module-shared fixture fails 如果特定测试失败,则剩余的 pytest 测试失败 - Fail remaining pytest tests if a specific one fails 如果多个测试有特定异常,则停止 pytest 测试 - stop pytest tests if several tests have a specific exception 使用pytest运行单个测试方法失败(未找到) - Running a single test method with pytest fails (not found) 如果并行运行 Pytest,则跳过/排除测试模块 - Skipping/excluding test module if running Pytest in parallel 如何强制pytest运行每个测试(停止跳过未标记为“跳过”的测试)? - How to force pytest to run every test (stop skipping tests not marked skip)? 使用 pytest 的参数化,如果一个测试用例失败,我如何跳过剩余的测试? - Using pytest's parametrize, how can I skip the remaining tests if one test case fails? TransactionTestCase 和 pytest 测试失败 - Tests fails with TransactionTestCase and pytest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM