假设我有一个类似的场景:
Scenario Outline: Example scenario
Given the subprocess is running
When I generate the input
And I add <argument1> to the input
And I add <argument2> to the input
And this input is passed to the subprocess
Then the output should match the <output> for <argument1> and <argument2>
我非常想重用“when”步骤,例如And I add <argument> to the input
,但不想使用示例表,因为我希望在步骤定义/conftest 文件中动态生成固定装置。我目前正在使用@pytest.mark.parametrize
这样的参数化场景大纲:
import pytest
from pytest_bdd import scenario
from functools import partial
from some_lib import test_data, utils
@pytest.fixture(scope='module')
def context():
return {}
scenario = partial(scenario, '../features/example.feature')
@pytest.mark.parametrize(
[argument1, argument2],
[(test_data.TEST_ARGUMENT[1], test_data.TEST_ARGUMENT[2]),],
)
@scenario('Example scenario')
def test_example_scenario(context, argument1, argument2):
pass
我希望能够以某种方式在具有不同参数的相同场景中重用相同的步骤定义,例如
@when('I add <argument> to the input')
def add_argument(context, argument):
context['input'] = utils.add_argument(context['input'], argument)
而不必有两个步骤定义,例如
@when('I add <argument1> to the input')
def add_argument(context, argument1):
context['input'] = utils.add_argument(context['input'], argument1)
@when('I add <argument2> to the input')
def add_argument(context, argument2):
context['input'] = utils.add_argument(context['input'], argument2)
pytest -bdd 文档似乎表明这是可能的,但我无法完全理解如何在不使用示例表的情况下完成此操作。
通常可以重用为它们提供参数的步骤。这允许单一实现和多次使用,因此代码更少。还打开了在单个场景中两次使用相同步骤并使用不同参数的可能性![原文如此] (强调我自己的)
有没有人对我如何做到这一点有任何想法?
一如既往地感谢您的时间!