【问题标题】:How to pass different values for same parameter in Example using pytest bdd for scenario statement如何在示例中使用 pytest bdd 为场景语句传递相同参数的不同值
【发布时间】:2020-12-29 20:12:10
【问题描述】:

这是我的功能文件

Scenario Outline: Test different value for same parameter
 Examples:
 | app     | app1     |
 | instagram| facebook |

Given <app> is installed on my device
And <app1> is installed on my device
@given("<app> is installed on my device")
def app_installation(app):
    install_app(app)

到目前为止,我不能在同一步骤中使用 app2 值,我必须使用 app1 参数复制 app_installation

有没有办法可以使用示例中的任何参数,其值可以映射到app

【问题讨论】:

  • 按照您的示例,您是否希望在继续下一步之前安装 1 个场景,其中 instagram 和 facebook(以及 whatsapp 和等)都已安装?
  • 是的,我想测试一些需要同时安装的东西

标签: python python-3.x bdd feature-file pytest-bdd


【解决方案1】:

由于 pytest-bdd 正在读取您的示例表并为每个表条目创建一个夹具,您可以动态加载表数据。这可以通过传递示例表列的标题名称而不是值来完成,然后使用 pytest request 夹具来检索实际的表值。

Scenario Outline: Test different value for same parameter
 Examples:
 | app     | app1     |
 | instagram| facebook |

Given app is installed on my device
And app1 is installed on my device
# IMPORTANT:
#    The step is parsed by `parsers.parse` and not by using
#    `<>`, therefore in the `app` variable will be the column
#    name (app, app1, app2, ...) of the examples table instead
#    of the actual value (instagram, facebook, ...).
@given(parsers.parse("Given {app} is installed on my device"))
def app_installed(request, app):
    app_to_install = request.getfixturevalue(app)
    # Install app ...

注意:你也可以在你的功能文件中使用&lt;app&gt;,但是你必须在调用request.getfixturevalue(app)之前删除@given步骤中的尖括号

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2021-08-31
    • 2023-01-12
    • 2021-03-14
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多