pytest:无需重命名文件或函数即可在报告中添加测试描述(长测试名称)的最佳方法 [英] pytest: Best way to add test description (Long test name) in the report with out renaming the files or functions

查看:17
本文介绍了pytest:无需重命名文件或函数即可在报告中添加测试描述(长测试名称)的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,pytest 在 pytest 报告中使用测试函数名或测试文件名

By default pytest use test function names or test files names in pytest reports

有没有最好的方法在报告中添加测试描述(长测试名称)而不使用 pytest 重命名文件或函数?

is there any Best way to add test description (Long test name) in the report with out renaming the files or functions using pytest?

我们可以通过在运行时更新测试用例名称来做到这一点吗?

Can we do this by updating the testcase name at run-time like ?

  1. request.node.name

request.node.name = "Very Very Very Very Very long long long long name name name name"

  1. 测试名称后的描述

def test_ok():
"""Very Very Very Very Very long long long long name name name name"""
    print("ok")

推荐答案

使用 pytest_runtest_makereport 钩子,每次测试可以调整报告的名称.(注意钩子必须放在插件中,或者conftest.py)

# conftest.py

import pytest

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()

    test_fn = item.obj
    docstring = getattr(test_fn, '__doc__')
    if docstring:
        report.nodeid = docstring


# test_it.py

def test_ok():
    """Very Very Very Very Very long long long long name name name name"""
    print("ok")

这将产生类似于:

tests/test_stuff.py::test_ok 
Very Very Very Very Very long long long long name name name name <- tests/test_stuff.py PASSED [100%]

参见 "hookwrapper:在其他钩子周围执行",了解有关 outcome = yieldoutcome.get_result() 业务的更多信息.

See "hookwrapper: executing around other hooks" for more info on the outcome = yield and outcome.get_result() business.

这篇关于pytest:无需重命名文件或函数即可在报告中添加测试描述(长测试名称)的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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