如何从测试方法向报告添加自定义日志输出?

时间:2018-12-25 08:32:50

标签: pytest pytest-html

我需要将自定义消息添加到html报告中。 我可以从pytest_runtest_makereport挂钩中执行此操作,但我想从测试方法中添加一些数据。

conftest.py:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call':
        extra.append(pytest_html.extras.html('<p>step 1</p>'))
        extra.append(pytest_html.extras.html('<p>step 2</p>'))
        extra.append(pytest_html.extras.html('<p>step 3</p>'))
        extra.append(pytest_html.extras.html('<p>step 4</p>'))
        extra.append(pytest_html.extras.html('<p>step 5</p>'))
        report.extra = extra



 @mark.first_feature
    class FirstFeatureTests:

        report = ''

        def appendStringToReport(self, report, message):
            report += "<p> " + message + " </p>"

    @mark.sanity
    @mark.regression
    @mark.example
    def test_1_first_feature(self, init_environment):
        self.appendStringToReport(self.report, "step 1")
        self.appendStringToReport(self.report, "step 2")
        self.appendStringToReport(self.report, "step 3")
        self.appendStringToReport(self.report, "step 4")
        assert True

如何将测试类中的“报告”字符串传递给pytest_runtest_makereport挂钩?

我将不胜感激, 谢谢。

0 个答案:

没有答案