pytest文档 86 - 如何在用例运行后判断测试结果(exitstatus)是通过还是失败?

前言

当用例之前完成后,如何判断pytest执行用例的情况是成功还是失败呢?
pytest 提供了一个 exitstatus 退出状态标记用例执行情况,可以在 pytest_terminal_summary 中 捕获pytest执行状态

pytest.main() 执行结果

pytest 从 5.x 版本后,执行 pytest.main() 可以获取到运行退出状态

运行退出状态,就是控制台上打印的最后一句:Process finished with exit code 0

相关的ExitCode 类 在源码中可以看到,是枚举类型

  • OK: 0,表示用例全部通过
  • TESTS_FAILED:1,表示测试用例有失败的情况
  • INTERRUPTED: 2,执行过程被中断
  • INTERNAL_ERROR:3,内部错误
  • USAGE_ERROR :4,用法错误
  • NO_TESTS_COLLECTED: 5, 没有收集到测试用例
class ExitCode(enum.IntEnum):
    """Encodes the valid exit codes by pytest.

    Currently users and plugins may supply other exit codes as well.

    .. versionadded:: 5.0
    """

    #: Tests passed.
    OK = 0
    #: Tests failed.
    TESTS_FAILED = 1
    #: pytest was interrupted.
    INTERRUPTED = 2
    #: An internal error got in the way.
    INTERNAL_ERROR = 3
    #: pytest was misused.
    USAGE_ERROR = 4
    #: pytest couldn't find tests.
    NO_TESTS_COLLECTED = 5

运行通过返回状态:0

使用示例

# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/

import pytest


def test_x3():
    print("222222")


if __name__ == '__main__':
    res = pytest.main(['test_x3.py'])
    print(f"pytest 执行状态:{res}")

命令行执行

python test_x3.py

运行结果:pytest 执行状态:0

用例有运行失败返回状态:1

# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/

import pytest


def test_x3():
    print("222222")
    assert 1 == 2


if __name__ == '__main__':
    res = pytest.main(['test_x3.py'])

命令行执行

python test_x3.py

运行结果:pytest 执行状态:1

执行过程中强制中断用例(命令行快捷键ctr+c),返回状态:2

D:\code_21_ke\api_环境切换\test_x3.py:9: KeyboardInterrupt
(to show a full traceback on KeyboardInterrupt use --full-trace)
=============== 1 passed in 2.87s =============
pytest 执行状态:2

用例收集时候就报错了,返回状态:4

collected 0 items                                                                                                       

============== no tests ran in 0.03s ===============
ERROR: file not found: test_x3x.py

pytest 执行状态:4

没有收集到测试用例返回状态:5

collected 0 items                                                                                                       

======= no tests ran in 0.06s =========
pytest 执行状态:5

pytest_terminal_summary 中获取退出状态

pytest_terminal_summary 钩子中有三个参数

  • terminalreporter 报告汇总
  • exitstatus
  • config

运行用例代码
test_x3.py

def test_x3():
    print("222222")
    assert 1==2

在conftest中通过 pytest_terminal_summary 钩子获取用例状态

# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/


def pytest_terminal_summary(terminalreporter, exitstatus, config): # noqa
    """收集测试结果
        terminalreporter: 报告汇总
        exitstatus: pytest 退出状态 0:成功
        config 全局 Config 对象
    """
    print(exitstatus)   # ExitCode.TESTS_FAILED
    print(f"当前执行的状态:{exitstatus}")  # 1

这里使用 pytest 命令去执行

pytest test_x3.py

运行结果

test_x3.py:6: AssertionError
----------------- Captured stdout setup --------------
读取命令行参数或ini test
---------------- Captured stdout call ----------------
222222
ExitCode.TESTS_FAILED
当前执行的状态:1
==============short test summary info ===========
FAILED test_x3.py::test_x3 - assert 1 == 2
======== 1 failed in 0.23s =

通过 exitstatus 获取到用例执行状态,就可以判断用例是否成功

# 作者-上海悠悠 wx:283340479
# blog地址 https://www.cnblogs.com/yoyoketang/


def pytest_terminal_summary(terminalreporter, exitstatus, config): # noqa
    """收集测试结果
        terminalreporter: 报告汇总
        exitstatus: pytest 退出状态 0:成功
        config 全局 Config 对象
    """
    print(exitstatus)
    print(f"当前执行的状态:{exitstatus}")
    if exitstatus == 0:
        print("用例执行 100% pass!")
        # do something ....
    else:
        print(f"用例执行失败,失败原因:{exitstatus}")
        # do something ....

根据判断结果,成功发送给对应的人,发送失败给对应的人。
网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)

posted @ 2023-05-12 14:20  上海-悠悠  阅读(618)  评论(0编辑  收藏  举报