如何将特定于场景的参数传递给 pytest bdd after_scenario hook?

问题描述

我正在使用 pytest bdd 来执行 bdd 场景。示例:

     INFO  Starting development server...
 ERROR  Error: Rule can only have one resource source (provided resource and test + include + exclude) in {
   "exclude": [
           null
             ],"use": [
              {
      "loader": "C:...\\node_modules\\@vue\\cli-plugin-babel\\node_modules\\cache-loader\\dis
t\\cjs.js","options": {
        "cacheDirectory": "C:...\\node_modules\\.cache\\babel-loader","cacheIdentifier": "2aa78f64"
             },"ident": "clonedRuleSet-38[0].rules[0].use[0]"
             },{
      "loader": "C:...\\node_modules\\babel-loader\\lib\\index.js","options": "undefined","ident": "undefined"
              }
              ]
              }

在场景中,我创建条目,然后查询并验证响应代码/输出。 无论场景是否成功,我都希望能够清理我创建的条目(因此插入另一行“然后我清理我的数据”不会实现我的目标)。

根据 https://pytest-bdd.readthedocs.io/en/latest/ - 有一个 Scenario Outline: entry with special character Given I am an authenticated user When I request entry information with <entryName> name Then I expect a 200 response code And I check the output list contains valid data Examples: | entryName | | #@ | 钩子应该允许我执行清理,无论场景是否成功。

  1. 我应该在哪里实施它? (我尝试将它放入 test_my_scenario.py 但它没有被执行,根据 https://github.com/pytest-dev/pytest-bdd/issues/174 看起来它应该在 conftest 中?)
  2. 如何将特定于场景的参数传递给钩子?与 pytest_bdd_after_scenario(request,feature,scenario) 不同,它在签名中没有 func_args。那么如何清理我在场景中创建的特定条目?

解决方法

  1. 是的,您应该将 <DataTemplate> <StackPanel> <Label Content="{Binding TestPropertyValue,ElementName=InnerTest}" Background="Cyan"> <b:Interaction.Behaviors> <tb:TestBehavior x:Name="InnerTest" TestPropertyValue="{Binding ItemTestViewModelValue,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}" /> </b:Interaction.Behaviors> </Label> <Label Content="{Binding ItemTestViewModelValue,Mode=OneWay}" Background="Lime" /> </StackPanel> </DataTemplate> 钩子放在 pytest_bdd_after_scenario(request,feature,scenario) 中,以便它对所有测试用例执行。

  2. 您可以创建一个 pytest 夹具并将清理所需的所有数据存储在其中,然后通过调用 conftest.py

    pytest_bdd_after_scenario(request,scenario) 中检索该夹具

示例:

request.getfixturevalue(name_of_fixture)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...