英文:
How to rename the title of the HTML Report generated by pytest-html plug in?
问题
我正在使用pytest-html
插件生成HTML报告。我通过在命令行中输入"pytest --html=report.html"
来执行pytest文件。
因此,生成的HTML报告的名称和标题是report.html。我想要更改生成报告的标题。
请告诉我如何做到这一点?
英文:
I am generating html report using pytest-html
plugin. I'm executing the pytest file by giving "pytest --html=report.html"
in command line.
So the name and title of the html report generated is report.html. I want to change the title of the generated report.
Please let me know, how to do that?
答案1
得分: 4
自v2.1.0版本开始,此插件提供了一个名为“pytest_html_report_title”的hook,用于在将标题添加到报告之前调用。您可以将以下代码添加到conftest.py中:
def pytest_html_report_title(report):
report.title = '您的标题!'
这也在插件的用户指南中有解释。
英文:
Since v2.1.0, this plugin exposes a hook called before adding the title to the report. You can add this to conftest.py:
def pytest_html_report_title(report):
report.title = 'your title!'
This is also explained in the plugin's User Guide.
答案2
得分: 2
在测试文件夹中创建一个名为conftest.py的文件。
这个文件用于配置pytest。
将以下代码放入其中:
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([html.h1("一个好标题")])
如果你需要更改HTML报告文件的名称,你可以尝试类似以下的代码:
# @pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
# 移除环境部分
config._metadata = None
if not os.path.exists('reports'):
os.makedirs('reports')
config.option.htmlpath = 'reports/' + datetime.now().strftime("%d-%m-%Y %H-%M-%S") + ".html"
我的示例会将report.html文件放在一个名为reports的文件夹中,并以日期命名,而不是使用静态名称。
英文:
create conftest.py file in the same folder of the test.
this file is used to configure pytest.
put this snippet inside
def pytest_html_results_summary(prefix, summary, postfix):
prefix.extend([html.h1("A GOOD TITLE")])
if you need to change the html report file name you can try something like this
# @pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
# to remove environment section
config._metadata = None
if not os.path.exists('reports'):
os.makedirs('reports')
config.option.htmlpath = 'reports/' + datetime.now().strftime("%d-%m-%Y %H-%M-%S") + ".html"
my example will put the report.html file in a folder called reports naming with a date instead of a static name
答案3
得分: 1
根据我在代码中看到的情况,目前没有办法仅更改报告的标题,因为它目前是硬编码的:
html.h1(os.path.basename(self.logfile))
因此,报告标题将始终是报告文件名。我刚刚向项目提交了一个合并请求,以添加一个新的钩子,允许更改标题而不更改文件名,我们将看看它是否被接受。
英文:
From what I see in the code there is no mean to change only the report's title yet, it is for now hardcoded as
html.h1(os.path.basename(self.logfile))
So the report title will always be the report file name. I've just pushed a merge request to the project to add a new hook to allow the change of the title without changing the file name, we will see if it is accepted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论