【问题标题】:How can I change variables for the next step in pytest-bdd?如何更改 pytest-bdd 中下一步的变量?
【发布时间】:2021-10-30 02:51:42
【问题描述】:

我想将 x 从一个步骤更改为另一个步骤,而不添加其他功能

Feature: Calculator
    Scenario Outline: simple sum
        Given the x is 1
        And the x is <x>

    Examples:
        | x |
        | 5 |
from pytest_bdd import given, when, then, scenario, parsers
import logging
info = logging.getLogger('test').info

@scenario("test.feature", "simple sum")
def test_outlined():
    pass

@given(parsers.parse("the x is {x}"))
@given("the x is <x>")
def f(request, x):
    info("x=%s" % x)

输出:

2021-08-31 14:23:30 INFO x=1
2021-08-31 14:23:30 INFO x=1

想要的输出:

2021-08-31 14:23:30 INFO x=1
2021-08-31 14:23:30 INFO x=5

非常感谢,请帮忙

【问题讨论】:

  • 您可以尽可能使用场景大纲表。见this。

标签: python pytest fixtures gherkin pytest-bdd


【解决方案1】:

你必须在你的功能文件中这样写

Feature: Calculator
Scenario Outline: simple sum
    Given the x is 1
    And the x is 5
    And the x is 9

在你的步骤定义文件中

from pytest_bdd import given, when, then, scenario, parsers
import logging
info = logging.getLogger('test').info

@scenario("test.feature", "simple sum")
def test_outlined():
    pass

@given(parsers.parse("the x is {x}"))
def f(request, x):
    info("x=%s" % x)

【讨论】:

  • 感谢您的回答,但这样输出为:2021-10-21 09:23:38 INFO x= 2021-10-21 09:23:38 INFO x = 2021-10-21 09:23:38 INFO x= 我想要更多:2021-10-21 09:23:38 INFO x=1 2021-10-21 09:23:38 INFO x=5 2021-10-21 09:23:38 信息 x=9
  • @goncaloandrade 我已经更新了答案。请检查一下
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 2021-11-30
  • 2021-07-27
  • 2017-08-08
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多