pytest: Best way to add test description (Long test name) in the report with out renaming the files or functions

别说谁变了你拦得住时间么 提交于 2021-02-19 04:18:32

问题


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

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. Description after test-name
def test_ok():
"""Very Very Very Very Very long long long long name name name name"""
    print("ok")

回答1:


Using the pytest_runtest_makereport hook, the reported name can be adjusted for each test. (Note that hooks must be placed within a plugin, or a 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")

This will produce output similar to:

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%]

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



来源:https://stackoverflow.com/questions/59996968/pytest-best-way-to-add-test-description-long-test-name-in-the-report-with-out

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!