• pytest文档54-Hooks函数terminal打印测试结果(pytest_report_teststatus)


    前言

    使用命令行执行pytest用例的时候,会在 terminal 终端打印整个用例的测试结果:

    • .代表通过的用例
    • F代表失败的用例
    • E代表异常的用例
      如果我们不喜欢这种报告结果,可以通过 pytest_report_teststatus 钩子函数改变测试报告的内容,接下来试试吧.改成√,把F改成x,这样更直观。

    pytest_report_teststatus

    pytest_report_teststatus(report, config): 返回各个测试阶段的result, 可以用when属性来区分不同阶段。

    • when=='setup' 用例的前置操作
    • when=='call' 用例的执行
    • when=='teardown' 用例的后置操作

    运行案例test_x.py

    import pytest
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    def test_01():
        a = "hello"
        b = "hello"
        assert a == b
    
    
    def test_02():
        a = "hello"
        b = "hello world"
        assert a == b
    
    
    def test_03():
        a = "hello"
        b = "hello world"
        assert a in b
    
    
    def test_04():
        a = "hello"
        b = "hello world"
        assert a not in b
    

    命令行执行pytest test_x.py --tb=line

    >pytest test_x.py --tb=line
    ============================= test session starts =============================
    
    collected 4 items
    
    test_x.py .F.F                                                           [100%]
    
    ================================== FAILURES ===================================
    D:	est_x.py:13: AssertionError: assert 'hello' == 'hello world'
    D:	est_x.py:25: AssertionError: assert 'hello' not in 'hello world'
    ===================== 2 failed, 2 passed in 0.07 seconds ======================
    

    运行的结果是.和F,我们希望改成√和x,在conftest.py文件写钩子函数

    # conftest.py
    import pytest
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    def pytest_report_teststatus(report, config):
        '''turn . into √,turn F into x'''
        if report.when == 'call' and report.failed:
            return (report.outcome, 'x', 'failed')
        if report.when == 'call' and report.passed:
            return (report.outcome, '√', 'passed')
    

    重新运行pytest test_x.py --tb=line

    >pytest test_x.py --tb=line
    collected 4 items
    
    test_x.py √x√x                                                           [100%]
    
    ================================== FAILURES ===================================
    D:softkecheng202004xuexi	est_x.py:13: AssertionError: assert 'hello' == 'hello world'
    D:softkecheng202004xuexi	est_x.py:25: AssertionError: assert 'hello' not in 'hello world'
    ===================== 2 failed, 2 passed in 0.07 seconds ======================
    

    关于Error异常

    前面这篇https://www.cnblogs.com/yoyoketang/p/12609871.html讲到关于测试用例的执行结果,
    当 setup 出现异常的时候,用例才会Error,于是可以通过report.when == 'setup' 判断到前置操作的结果

    # test_x.py
    import pytest
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    @pytest.fixture()
    def login():
        print("前置操作:准备数据")
        assert 1 == 2   # 前置出现异常
        yield
        print("后置操作:清理数据")
    
    
    def test_01(login):
        a = "hello"
        b = "hello"
        assert a == b
    
    
    def test_02():
        a = "hello"
        b = "hello world"
        assert a == b
    
    
    def test_03():
        a = "hello"
        b = "hello world"
        assert a in b
    
    
    def test_04():
        a = "hello"
        b = "hello world"
        assert a not in b
    
    

    运行结果

    >pytest test_x.py --tb=line
    ============================= test session starts =============================
    
    collected 4 items
    
    test_x.py Ex√x                                                           [100%]
    
    =================================== ERRORS ====================================
    __________________________ ERROR at setup of test_01 __________________________
    E   assert 1 == 2
    ---------------------------- Captured stdout setup ----------------------------
    前置操作:准备数据
    ================================== FAILURES ===================================
    D:softkecheng202004xuexi	est_x.py:21: AssertionError: assert 'hello' == 'hello world'
    D:softkecheng202004xuexi	est_x.py:33: AssertionError: assert 'hello' not in 'hello world'
    ================= 2 failed, 1 passed, 1 error in 0.09 seconds =================
    

    当前置失败的时候,改成0

    # conftest.py
    import pytest
    # 作者-上海悠悠 QQ交流群:717225969
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    
    def pytest_report_teststatus(report, config):
        '''turn . into √,turn F into x, turn E into 0'''
        if report.when == 'call' and report.failed:
            return (report.outcome, 'x', 'failed')
        if report.when == 'call' and report.passed:
            return (report.outcome, '√', 'passed')
        if report.when == 'setup' and report.failed:
            return (report.outcome, '0', 'error')
    

    于是控制台的结果,就可以改了

    >pytest test_x.py --tb=line
    ============================= test session starts =============================
    
    collected 4 items
    
    test_x.py 0x√x                                                           [100%]
    
    ================================== FAILURES ===================================
    D:softkecheng202004xuexi	est_x.py:7: assert 1 == 2
    D:softkecheng202004xuexi	est_x.py:21: AssertionError: assert 'hello' == 'hello world'
    D:softkecheng202004xuexi	est_x.py:33: AssertionError: assert 'hello' not in 'hello world'
    ===================== 3 failed, 1 passed in 0.07 seconds ======================
    

    skip的用例可以通过report.skiped获取到,可以这样写

    if report.skipped:
            return (report.outcome, '/', 'skipped')
    

    report相关的属性

    report相关的属性,参考以下
    '_from_json',
    '_get_verbose_word',
    '_to_json',
    'caplog',
    'capstderr',
    'capstdout',
    'count_towards_summary',
    'duration',
    'failed',
    'from_item_and_call',
    'fspath',
    'get_sections',
    'head_line',
    'keywords',
    'location',
    'longrepr',
    'longreprtext',
    'nodeid',
    'outcome',
    'passed',
    'sections',
    'skipped',
    'toterminal',
    'user_properties',
    'when'

  • 相关阅读:
    (转)JVM参数的说明、简单记法和GC调优策略
    深度学习论文翻译解析(十二):Fast R-CNN
    深度学习论文翻译解析(十一):OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks
    vue中组建的创建和使用
    CountDownLatch的理解和使用
    java多线程并发编程中对一些概念和关键字的理解
    spring中访问变量的用法
    mysql中group by优化
    vue中实现标题的国际化
    mysql中的覆盖索引,二级索引和索引排序
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/13631185.html
Copyright © 2020-2023  润新知