Pytest自定義測試報告


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

  1. 介入Pytest運行流程,運行后自動生成HTML測試報告:使用Hooks方法
  2. 拿到運行結果統計數據:鈎子方法pytest_terminal_summary的terminalreporter對象的stats屬性中
  3. 渲染HTML報告模板生成測試報告:使用Jinjia2

經研究,Hooks方法pytest_terminal_summary及運行完畢生成命令行總結中包含的terminalreporter對象的stats屬性中包含我們需要的測試結果統計。

鈎子運行流程可以參考:https://www.cnblogs.com/superhin/p/11733499.html

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

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

編寫一些實例用例,運行后屏幕輸出的terminalreporter對象相關屬性如下:

{'_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狀態,以及各種狀態(passed,failed,skipped,xfailed,xpassed)等用例結果(TestReport)列表。
我進一步打印一個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對象屬性

打印結果如下:

{'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屬性中的統計數據渲染生成自定義報告,完整代碼如下:

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>函數名</th> <th>用例id</th> <th>狀態</td> <th>用例輸出</th> <th>執行時間</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)

在命令行運行pytest生成的測試報告report.html示例如下
Pytest自定義測試報告


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM