pytest那些事01_执行结果内容改造

前言

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

添加用例描述

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

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

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

posted @ 2020-07-13 17:49  SodaAir  阅读(275)  评论(0编辑  收藏  举报