pytest插件探索——pytest-html

简介

pytest-html是一个用来生成html格式的测试报告的插件。

注:pytest默认生成的是Junit风格的report,可以跟Jenkins集成或者用一些第三方插件进行渲染生成html,另一个比较好的选择是使用allure, allure生成report稍显繁琐,不过确实漂亮!

GitHub地址:https://github.com/pytest-dev/pytest-html.git

源码解析:

pytest_html/plugin.py:

源码部分比较多,这里截取跟pytest hook function相关的代码片段分析。

class HTMLReport(object):
    # skip some codes snipt here ...    
    def pytest_runtest_logreport(self, report):
        if report.passed:
            self.append_passed(report)
        elif report.failed:
            self.append_failed(report)
        elif report.skipped:
            self.append_skipped(report)
        else:
            self.append_other(report)

    def pytest_collectreport(self, report):
        if report.failed:
            self.append_failed(report)

    def pytest_sessionstart(self, session):
        self.suite_start_time = time.time()

    def pytest_sessionfinish(self, session):
        report_content = self._generate_report(session)
        self._save_report(report_content)

    def pytest_terminal_summary(self, terminalreporter):
        terminalreporter.write_sep('-', 'generated html file: {0}'.format(
            self.logfile))

pytest_runtest_logreport, hook function, process a test setup/call/teardown report relating to the respective phase of executing a test.

在测试setup/call/teardown的报告生成阶段将各种result的测试用例归类统计。

pytest_collectreport, hook function, collector finished collecting.

todo:这块代码逻辑没看懂,为何要在collectreport阶段再判断一次report.failed?

pytest_sessionstart, hook function, called after the Session object has been created and before performing collection and entering the run test loop.

在测试session生成但尚未进入测试用例收集和执行的的时候记录一下测试套的开始时间。

pytest_seesionfinish, hook function, called after whole test run finished, right before returning the exit status to the system.

在测试执行完毕尚未返回exit code的时候进行report的生成和写入操作。

pytest_terminal_summary, hook function, add a section to terminal summary reporting.

在terminal的summary部分增加了html文件的生成位置信息。

你可能感兴趣的:(pytest插件探索——pytest-html)