Behave的精美HTML格式化工具
项目描述
HTML精美格式化工具,用于Behave
- 灵感来源于 jest-html-reporter
- 使用项目 dominate 生成页面
该项目在Fedora杂志上有所介绍
- https://fedoramagazine.org/automation-through-accessibility/
- 我们团队(Red Hat DesktopQE)自动化堆栈的完整技术解决方案 https://modehnal.github.io/
安装
python3 -m pip install behave-html-pretty-formatter
用法
要使用 Behave,请在您的项目文件夹(或家目录)中创建一个 behave.ini
文件,并包含以下内容
# -- FILE: behave.ini
# Define ALIAS for PrettyHTMLFormatter.
[behave.formatters]
html-pretty = behave_html_pretty_formatter:PrettyHTMLFormatter
# Optional configuration of PrettyHTMLFormatter
# also possible to use "behave ... -D behave.formatter.html-pretty.{setting}={value}".
[behave.userdata]
behave.formatter.html-pretty.title_string = Test Suite Reporter
# Example usecase, print {before/after}_scenarios as steps with attached data.
behave.formatter.html-pretty.pseudo_steps = false
# Structure of the result html page readable(pretty) or condensed.
behave.formatter.html-pretty.pretty_output = true
# The '%' must be escaped in ini format.
behave.formatter.html-pretty.date_format = %%d-%%m-%%Y %%H:%%M:%%S
# Defines if the summary is expanded upon start.
behave.formatter.html-pretty.show_summary = false
# Defines if the user is interested in what steps are not executed.
behave.formatter.html-pretty.show_unexecuted_steps = true
# Define what to collapse by default, possible values:
# "auto" - show everything except embeds (default)
# "all" - hide everything
# comma separated list - specify subset of "scenario,embed,table,text"
# "none" - show everything, even embeds
behave.formatter.html-pretty.collapse = auto
# Defines if the user wants to see previous attempts when using auto retry.
# Auto retry https://github.com/behave/behave/blob/main/behave/contrib/scenario_autoretry.py
behave.formatter.html-pretty.show_retry_attempts = true
# Override global summary visibility
# "auto" - show global summary if more than one feature executed (default)
# "true" - show global summary
# "false" - hide global summary
behave.formatter.html-pretty.global_summary = auto
# Following will be formatted in summary section as "tester: worker1".
behave.additional-info.tester = super_worker
# Can be used multiple times.
behave.additional-info.location = super_awesome_lab
或者,从 behave >= v1.2.7.dev3 开始,您可以将相同的配置放入 pyproject.toml
中,如下所示
[tool.behave.formatters]
"html-pretty" = "behave_html_pretty_formatter:PrettyHTMLFormatter"
# Optional configuration of PrettyHTMLFormatter
[tool.behave.userdata]
"behave.formatter.html-pretty.title_string" = "Test Suite Reporter"
"behave.formatter.html-pretty.pseudo_steps" = false
"behave.formatter.html-pretty.pretty_output" = true
"behave.formatter.html-pretty.date_format" = "%%d-%%m-%%Y %%H:%%M:%%S"
"behave.formatter.html-pretty.show_summary" = false
"behave.formatter.html-pretty.collapse" = "auto"
"behave.formatter.html-pretty.show_unexecuted_steps" = true
"behave.formatter.html-pretty.global_summary" = "auto"
"behave.additional-info.tester" = "super_worker"
"behave.additional-info.location" = "super_awesome_lab"
然后通过运行 Behave 并使用 -f
/--format
选项来使用格式化器,例如
behave -f help
behave -f html-pretty
behave -f html-pretty -o behave-report.html
您可以在 behave 文档 中找到有关 behave 和用户自定义格式化器的信息。
HTML 美化器具有高对比度选项
您可以通过切换按钮在不同的对比度之间切换。
暗黑模式
样式表遵循浏览器的暗黑主题,因此将背景转换为暗色并调整颜色为较暗的色调。
默认隐藏摘要
要更改设置,请使用 .ini 文件。
behave.formatter.html-pretty.show_summary = true
开发
MIME 类型
查看 常见 MIME 类型,其中一些对我们很有用。
将编码转换为 base64
data_base64 = base64.b64encode(open("/path/to/image", "rb").read())
data_encoded = data_base64.decode("utf-8").replace("\n", "")
数据插入到 html 中的格式
# Format
"data:<mime_type>;<encoding>,<data_encoded>"
# Example
f"data:image/png;base64,{data_encoded}"
您可以使用的定义好的函数
使用格式化器函数的基本设置
for formatter in context._runner.formatters:
if formatter.name == "html-pretty":
context.formatter = formatter
设置图标
icon_data = f"data:image/svg+xml;base64,{data_encoded}"
context.formatter.set_icon(icon=icon_data)
示例用法 - 一个指示测试是否在 X11 或 Wayland 下运行的指示器
设置 HTML 页面的标题
context.formatter.set_title(title="Test Suite Reporter")
- 这也可以从 behave.ini 文件中配置
behave.formatter.html-pretty.title_string = 测试套件报告器
为 HTML 页面添加自定义 head 元素
context.formatter.add_html_head_element('<script src="https://example.js.org/my_js_lib.min.js"></script>')
示例用法 - 导入自定义 JS 库(例如 plotly.js)。
HTML 报告中的注释步骤
用作信息面板来描述或提供有关页面内容的详细信息。您需要定义自己的步骤,在该步骤中设置注释步骤的标志。
@step('Commentary')
def commentary_step(context):
# Get the correct step to override.
scenario = context.formatter.current_scenario
step = scenario.current_step
# Override the step, this will prevent the decorator to be generated and only the text will show.
step.commentary_override = True
功能文件示例用法
@commentary
Scenario: Example of commentary usage.
* Start application "zenity" via command "zenity --about"
* Commentary
"""
This field is generated from decorator 'Commentary'
Where you insert text and override step to not print its decorator.
The text will get printed and will be seen, as you can see, haha.
"""
* Left click "About" "radio button"
结果可以在图像示例中查看。
将数据嵌入到报告中
基本嵌入设置 - 将嵌入函数保存到上下文中
这是为了创建快捷方式 context.embed()
。有多种方法可以实现这一点。
如果您有基本设置,您可以简单地这样定义
context.embed = context.formatter.embed
如果您没有基本设置,正确的方法是以下代码
for formatter in context._runner.formatters:
if formatter.name == "html-pretty":
context.embed = formatter.embed
您还可以定义自定义的嵌入函数,当 behave 调用没有 html-pretty
格式化器时(例如,用于调试目的)执行其他操作,并且只有当格式化器存在时,才使用 formatter.embed
函数覆盖它。
def _embed(mime_type, data, caption):
if "text" in mime_type:
# Do your logging here, for example:
print(f"{caption}: {data}")
context.embed = _embed
# This requires Basic setup
if hasattr(context, "formatter"):
context.embed = context.formatter.embed
嵌入图片
context.embed(mime_type="image/png", data="/path/to/image.png", caption="Screenshot")
嵌入视频
context.embed(mime_type="video/webm", data="/path/to/video.webm", caption="Video")
图片和视频示例
嵌入 plotly.js 图表示例
# This have to be done once per report, e.g. in `before_all()`.
context.formatter.add_html_head_element('<script src="https://cdn.plot.ly/plotly-2.35.2.min.js" charset="utf-8"></script>')
# Example graph
graph_js = \
"""
TESTER = document.getElementById('tester');
TESTER.innerHTML = '';
Plotly.newPlot( TESTER, [{
x: [1, 2, 3, 4, 5],
y: [1, 2, 4, 8, 16] }], {
margin: { t: 0 } } );
"""
# Embed the wrapping `<div>` element together with JS code.
context.formatter.embed(
data=f'<div id="tester" style="width:600px;height:250px;"></div><script>{graph_js}</script>',
mime_type="text/html",
caption="Example Graph",
compress=False,
)
# Embed the same JS code gzip compressed (large graphs).
graph_js_min = graph_js.replace("\n", "").replace("tester", "compressed_tester").strip()
context.formatter.embed(
data=f'<div id="compressed_tester" style="width:600px;height:250px;" onclick="{graph_js_min}">Click me to load the graph.</div>',
mime_type="text/html",
caption="Example Compressed Graph",
compress=True,
)
注意:如果使用 compress=True
,则解压缩由javascript完成,并且浏览器不会执行解压缩的脚本(需要额外的 onclick
回调)。
定义 MIME 类型及其对应接受的数据
这些都是我们日常使用的示例,如果需要,我们可以定义更多。
mime_type="video/webm", data="/path/to/video.webm" or data="<base64_encoded_video>"
mime_type="image/png", data="/path/to/image.png" or data="<base64_encoded_image>"
mime_type="text/plain", data="<string>"
mime_type="text/html", data="<string>" # data string is pasted as raw HTML (not escaped)
mime_type="text/markdown", data="<string>" # data string is converted using markdown pip module
mime_type="link", data="list(<link>, <label>)"
您可以简单地将 data=data_encoded
设置为在编码为 base64部分中描述的生成内容,格式化器将根据 MIME 类型生成适当的 格式,或者您可以直接使用 data="/path/to/file"
,格式化器将尝试转换它。
embed()
函数返回一个对象,可以通过 set_data()
和 set_fail_only()
方法保存和稍后修改。如果您想嵌入仍在处理中的数据(例如在步骤中启动的后台进程的输出等),则可以使用此方法。
伪步骤
如果测试套件使用了 before_scenario()
和 after_scenario()
,并且您希望它们在HTML报告中作为步骤显示(例如,将嵌入步骤与标准步骤分开),在 behave.ini 文件中配置切换 behave.formatter.html-pretty.pseudo_steps = true
即可解决问题,同时需要在 before_scenario()
的末尾调用 context.html_formatter.before_scenario_finish(status)
(对于 after_scenario()
类似)。状态是 "passed", "failed", "skipped"
之一。该函数将设置伪步骤的颜色类,并记录伪步骤的持续时间。
# Example use in features.environment.py
def before_scenario(context, scenario):
...
# This requires to have html_formatter set by code above.
if error_found:
context.embed("text", str(error_found), "Error Message")
context.html_formatter.before_scenario_finish("failed")
raise error_found
else:
context.html_formatter.before_scenario_finish("passed")
def after_scenario(context, scenario):
...
if error_found:
context.embed("text", str(error_found), "Error Message")
context.html_formatter.after_scenario_finish("failed")
raise error_found
else:
context.html_formatter.after_scenario_finish("passed")
贡献
您想帮助改进此软件吗?请在我们公开的bug跟踪器中创建一个问题,或直接提交pull请求。
我们使用 tox 来运行lint和测试,例如。
tox
tox -l
tox -e flake8
对于代码格式化,我们使用 black,您可以通过我们的Tox设置来运行它,例如。
tox -e black
如果您需要更改CSS或JavaScript代码:首先编辑常规文件,然后按如下方式生成压缩版本
tox -e minify
图像示例
漂亮的HTML格式化器
高对比度漂亮的HTML格式化器
美化 HTML 格式化器,展开和折叠步骤
高对比度漂亮的HTML格式化器,展开和折叠步骤
项目详情
下载文件
下载适用于您的平台文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。
源分发
构建分发
散列 for behave_html_pretty_formatter-1.12-py3-none-any.whl
算法 | 散列摘要 | |
---|---|---|
SHA256 | 326e5f2e59e1178598aa3ee47683e816a75c3b78b37403112f55d40bf865dcf6 |
|
MD5 | 0fbd9e95eff3d0d49db02f73c4650dcb |
|
BLAKE2b-256 | 3509f01cd39856421217d0a7473de00eaf70533cc2149637e509699d38e7643e |