python - 像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.
解决方案
经过几个小时的搜索和尝试,我找到了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)
推荐阅读
- css - GatsbyJS + TypographyJS:如何让 Typography 优先?
- ruby-on-rails - 如何获取数据库中单选按钮的选中值?
- python - 替换熊猫列中的值
- julia - 如何修复 UndefVarError 错误:* 未定义?
- mysql - 在每个更新表上创建行条目版本
- c++ - Direct2D 使用图层仅更新绘图的一部分
- python - Python UnitTest - 如何访问 subTests 消息而无需手动编写它们?
- c# - 无法拦截蓝牙耳机媒体按钮事件
- mysql - 我如何验证夜班员工的 logdatetime
- postgresql - 当运行 CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE; pgAdmin 与服务器的连接丢失