首页 > 解决方案 > 像googletest一样制作pytest输出?

问题描述

我正在使用 PyTest 进行 python 代码测试。由于我使用 googletest 进行 C++ 代码测试,因此我喜欢 googletest 的输出格式。

我想知道,是否可以像 googletest 一样制作 pytest 输出?pytest 输出行太长,而 googletest 很短:

// pytest 示例:

(base) zz@home% pytest test_rle_v2.py
================================================================================== test session starts ===================================================================================
platform linux -- Python 3.8.1, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/zz/work/test/learn-hp/.hypothesis/examples')
rootdir: /home/zz/work/test/learn-hp
plugins: env-0.6.2, hypothesis-4.38.0
collected 1 item                                                                                                                                                                         

test_rle_v2.py .                                                                                                                                                                   [100%]

=================================================================================== 1 passed in 0.46s ====================================================================================

// 谷歌测试示例

(base) zz@home% ./test_version 
[==========] Running 5 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 5 tests from VERSION
[ RUN      ] VERSION.str
[       OK ] VERSION.str (0 ms)
[ RUN      ] VERSION.parts
[       OK ] VERSION.parts (0 ms)
[ RUN      ] VERSION.metadata
[       OK ] VERSION.metadata (1 ms)
[ RUN      ] VERSION.atLeast
[       OK ] VERSION.atLeast (0 ms)
[ RUN      ] VERSION.hasFeature
[       OK ] VERSION.hasFeature (0 ms)
[----------] 5 tests from VERSION (1 ms total)

[----------] Global test environment tear-down
[==========] 5 tests from 1 test suite ran. (1 ms total)
[  PASSED  ] 5 tests.

标签: pythontestingpytestgoogletest

解决方案


经过几个小时的搜索和尝试,我找到了conftest.py我需要的文件。在 conftest.py 中,人们可以覆盖默认的 pytest 函数,即通过提供钩子。

以下是 WIP 示例:

# conftest.py

import os
import random

def pytest_runtest_call(item):
    item.add_report_section("call", "custom", " [ Run      ]  " + str(item))

def pytest_report_teststatus(report, config):
    #print(">>> outcome:", report.outcome)

    if report.when == 'call':
        # line = f' [ Run      ]  {report.nodeid}'
        # report.sections.append(('ChrisZZ', line))
        if (report.outcome == 'failed'):
            line = f' [   FAILED ]  {report.nodeid}'
            report.sections.append(('failed due to', line))

    if report.when == 'teardown':
        if (report.outcome == 'passed'):
            line = f' [       OK ]  {report.nodeid}'
            report.sections.append(('ChrisZZ', line))

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    reports = terminalreporter.getreports('')
    content = os.linesep.join(text for report in reports for secname, text in report.sections)
    if content:
        terminalreporter.ensure_newline()
        #terminalreporter.section('', sep=' ', green=True, bold=True)
        #terminalreporter.section('My custom section2', sep='------]', green=True, bold=True, fullwidth=None)
        terminalreporter.line(content)


推荐阅读