Pytest使用鉤子方法生成自定義測試報告

使用Pytest測試框架生成測試報告最常用的便是使用pytest-htmlallure-pytest兩款插件了。
pytest-html簡單(支持單html測試報告),allure-pytest則漂亮而強(qiáng)大。
當(dāng)然想要使用自定義模板生成測試報告也非常簡單,簡單實(shí)現(xiàn)步驟如下:

  1. 介入Pytest運(yùn)行流程,運(yùn)行后自動生成HTML測試報告:使用Hooks方法
  2. 拿到運(yùn)行結(jié)果統(tǒng)計數(shù)據(jù):鉤子方法pytest_terminal_summary的terminalreporter對象的stats屬性中
  3. 渲染HTML報告模板生成測試報告:使用Jinjia2

經(jīng)研究,Hooks方法pytest_terminal_summary及運(yùn)行完畢生成命令行總結(jié)中包含的terminalreporter對象的stats屬性中包含我們需要的測試結(jié)果統(tǒng)計。

鉤子運(yùn)行流程可以參考:https://www.cnblogs.com/superhin/p/11733499.html

使用Pytest的對象自省(對象.__dict__dir(對象))我們可以很方便的查看對象有哪些屬性,在conftest.py文件中編寫鉤子函數(shù)如下:

# file: conftest.py
def pytest_terminal_summary(terminalreporter, exitstatus, config):
    from pprint import pprint
    pprint(terminalreporter.__dict__)  # Python自省,輸出terminalreporter對象的屬性字典

編寫一些實(shí)例用例,運(yùn)行后屏幕輸出的terminalreporter對象相關(guān)屬性如下:

{'_already_displayed_warnings': None,
 '_collect_report_last_write': 1629647274.594309,
 '_keyboardinterrupt_memo': None,
 '_known_types': ['failed',
                  'passed',
                  'skipped',
                  'deselected',
                  'xfailed',
                  'xpassed',
                  'warnings',
                  'error'],
 '_main_color': 'red',
 '_numcollected': 7,
 '_progress_nodeids_reported': {'testcases/test_demo.py::test_01',
                                'testcases/test_demo.py::test_02',
                                'testcases/test_demo.py::test_03',
                                'testcases/test_demo.py::test_04',
                                'testcases/test_demo.py::test_05',
                                'testcases/test_demo.py::test_06',
                                'testcases/test_demo2.py::test_02'},
 '_screen_width': 155,
 '_session': <Session pythonProject1 exitstatus=<ExitCode.TESTS_FAILED: 1> testsfailed=2 testscollected=7>,
 '_sessionstarttime': 1629647274.580377,
 '_show_progress_info': 'progress',
 '_showfspath': None,
 '_tests_ran': True,
 '_tw': <_pytest._io.terminalwriter.TerminalWriter object at 0x10766ebb0>,
 'config': <_pytest.config.Config object at 0x106239610>,
 'currentfspath': None,
 'hasmarkup': True,
 'isatty': True,
 'reportchars': 'wfE',
 'startdir': local('/Users/superhin/項目/pythonProject1'),
 'startpath': PosixPath('/Users/superhin/項目/pythonProject1'),
 'stats': {'': [<TestReport 'testcases/test_demo.py::test_01' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_01' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_02' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_02' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_03' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_03' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_04' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_05' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_05' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_06' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo.py::test_06' when='teardown' outcome='passed'>,
                <TestReport 'testcases/test_demo2.py::test_02' when='setup' outcome='passed'>,
                <TestReport 'testcases/test_demo2.py::test_02' when='teardown' outcome='passed'>],
           'failed': [<TestReport 'testcases/test_demo.py::test_02' when='call' outcome='failed'>,
                      <TestReport 'testcases/test_demo.py::test_03' when='call' outcome='failed'>],
           'passed': [<TestReport 'testcases/test_demo.py::test_01' when='call' outcome='passed'>,
                      <TestReport 'testcases/test_demo2.py::test_02' when='call' outcome='passed'>],
           'skipped': [<TestReport 'testcases/test_demo.py::test_04' when='setup' outcome='skipped'>],
           'xfailed': [<TestReport 'testcases/test_demo.py::test_05' when='call' outcome='skipped'>],
           'xpassed': [<TestReport 'testcases/test_demo.py::test_06' when='call' outcome='passed'>]}}

可以看到stats屬性為字典格式,其中包含了setup/teardown狀態(tài),以及各種狀態(tài)(passed,failed,skipped,xfailed,xpassed)等用例結(jié)果(TestReport)列表。
我進(jìn)一步打印一個TestReport對向

# file: conftest.py
def pytest_terminal_summary(terminalreporter, exitstatus, config):
    from pprint import pprint
    # pprint(terminalreporter.__dict__)  # Python自省,輸出terminalreporter對象的屬性字典
    pprint(terminalreporter.stats['passed'][0].__dict__)  # 第一個通過用例的TestReport對象屬性

打印結(jié)果如下:

{'duration': 0.00029346700000010273,
 'extra': [],
 'keywords': {'pythonProject1': 1, 'test_01': 1, 'testcases/test_demo.py': 1},
 'location': ('testcases/test_demo.py', 11, 'test_01'),
 'longrepr': None,
 'nodeid': 'testcases/test_demo.py::test_01',
 'outcome': 'passed',
 'sections': [('Captured stdout call', 'test01\n')],
 'user_properties': [],
 'when': 'call'}

我們自己定義報告模板,使用Jinjia2,將stats屬性中的統(tǒng)計數(shù)據(jù)渲染生成自定義報告,完整代碼如下:

Jinja2官方使用文檔參考:http://doc.yonyoucloud.com/doc/jinja2-docs-cn/index.html

# file: conftest.py
from jinja2 import Template

html_tpl = '''<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
    <style>
      table {border-spacing: 0;}
      th {background-color: #ccc}
      td {padding: 5px; border: 1px solid #ccc;}
    </style>
</head>
<body>
    <h2>{{title}}</h2>
    <table>
        <tr> <th>函數(shù)名</th> <th>用例id</th> <th>狀態(tài)</td> <th>用例輸出</th> <th>執(zhí)行時間</th> </tr>
        {% for item in results %}
        <tr>
            <td>{{item.location[2]}}</td>
            <td>{{item.nodeid}}</td>
            <td>{{item.outcome.strip()}}</td>
            <td>{% if item.sections %}{{item.sections[0][1].strip()}}{% endif %}</td>
            <td>{{item.duration}}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>'''

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    results = []
    for status in ['passed', 'failed', 'skipped', 'xfailed', 'xpassed']:
        if status in terminalreporter.stats:
            results.extend(terminalreporter.stats[status])

    html = Template(html_tpl).render(title='測試報告', results=results)
    with open('report.html', 'w', encoding='utf-8') as f:
        f.write(html)

在命令行運(yùn)行pytest生成的測試報告report.html示例如下


Pytest自定義測試報告
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容