如果特定测试失败,则其余pytest测试失败 [英] Fail remaining pytest tests if a specific one fails

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

问题描述

因此,我有一个目录,其中包含一堆用python编写的具有正确语法的测试,以确保它们按顺序运行.

So I have a directory filled with a bunch of tests written in python with the proper syntax to make sure they run in order.

因此,假设我有一个测试,如果测试失败,则当前调用pytest.exit('Exit Message').问题在于,以XML生成的测试输出仅记录其前面的测试.我希望整个套件都可以运行,但是如果上述测试失败,则会报告为失败.

So let's say I have a test which if fails, currently calls pytest.exit('Exit Message'). The problem with this is that the generated test output in XML only records the tests preceding it. I would prefer that the whole suite is run but are reported as failed if the test mentioned above fails.

我想到的解决方案是设置一个环境变量,以防其失败,然后在以下测试中检查该环境变量.问题是使用Jenkins运行它时,未检测到环境变量集,如果存在,我希望使用本机解决方案.

A solution I thought of was setting an environment variable in case it fails and then checking that environment variable in the following tests. The issue is that running it with Jenkins, the environment variable set isn't detected and I would prefer a native solution if it exists.

我所拥有的是:

def test_check_connection(self):
    ...
    if Failed:
        pytest.exit('No connectivity')

推荐答案

我不确定我是否正确理解了您的问题.如果要在第一个失败的测试用例之后停止pytest,请使用-x选项

I'm not sure I understand your problem correctly. If you want pytest to stop after the first failing test case use the -x option

pytest -x ...

如果您想运行所有测试并想知道jenkins是否有任何失败,请检查pytest应用程序的退出代码:

If you want to run all tests and want to know if there were any failures from jenkins check the exit code of the pytest app:

运行pytest可能会导致六个不同的退出代码:

Running pytest can result in six different exit codes:

退出代码0:所有测试均已收集并成功通过

Exit code 0: All tests were collected and passed successfully

退出代码1:已收集并运行测试,但其中一些测试失败

Exit code 1: Tests were collected and run but some of the tests failed

退出代码2:测试执行被用户中断

Exit code 2: Test execution was interrupted by the user

退出代码3:执行测试时发生内部错误

Exit code 3: Internal error happened while executing tests

退出代码4:pytest命令行使用错误

Exit code 4: pytest command line usage error

退出代码5:未收集测试

Exit code 5: No tests were collected

这篇关于如果特定测试失败,则其余pytest测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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