前言

改造pytest命令行执行测试用例时,返回的内容。

添加用例描述

在编写测试用例时,若将测试目的写为测试用例名称,可能会导致用例名称过长,但是使用简短的编号表示用例名称时,从执行结果中又无法知晓具体的测试逻辑,基于此,想要改造测试结果的返回内容,携带上用例描述信息。
在conftest.py中添加代码如下:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    if not report.description:
        report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")
    else:
        report.nodeid = "Description of this test is : " + report.description

效果如下:

test_doc.py::TestDoc::test_01
Description of this test is : this is the first test PASSED                                                                      [ 33%]
test_doc.py::TestDoc::test_02
Description of this test is : this is the second test PASSED                                                                     [ 66%]
test_doc.py::TestDoc::test_03
Description of this test is : None PASSED                                                                                        [100%]

如果有更好的解决方法,欢迎留言

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄