我使用 pytest 运行程序从我的自动化测试框架(Selenium 和 RestAPI 测试)中获取结果输出。我使用 pytest-html 插件在测试结束时生成单页 html 结果文件。我使用以下命令(从活动的 VirtEnv 会话)启动测试运行。
python -m pytest -v --html="api_name_test.html" --self-contained-html
(它有点复杂,因为我使用 powershell 脚本来运行它并提供带有日期时间戳的结果文件名并在完成时通过电子邮件发送文件,但它本质上是上面的命令)
当生成报告并打开此报告 html 时,我发现所有未通过的测试都已扩展。我想让它默认折叠所有行(失败、XFailed、错误等)。
我的项目在目录根目录中包含一个 conftest.py 文件和一个 pytest.ini 文件,我在其中指定测试脚本的目录
在我最简单的项目中的 conftest.py 文件中,我有一个可选的钩子来获取测试的目标 url 并将其放入报告摘要中:
import pytest
from py._xmlgen import html
import os
import rootdir_ref
import simplejson
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix):
theRootDir = os.path.dirname(rootdir_ref.__file__)
credentials_path = os.path.join(theRootDir, 'TestDataFiles', 'API_Credentials.txt')
target_url = simplejson.load(open(credentials_path)).get('base_url')
prefix.extend([html.p("Testing against URL: " + target_url)])
Github 页面提到显示查询可用于折叠具有各种结果的行,但没有提及此信息的输入位置。 https://github.com/pytest-dev/pytest-html
“默认情况下,结果表中的所有行都将展开,除了那些已通过的行。可以使用查询参数自定义此行为:?collapsed=Passed,XFailed,Skipped”
目前我不确定该?collapsed=...
行是否进入命令行,或者 conftest 作为钩子,或者我是否需要编辑 pytest-html 插件附带的默认 style.css 或 main.js?(另外我不熟悉css,只知道少量的html)。我假设它作为钩子进入 conftest.py 文件,但并不真正了解如何应用它。