有没有一种简单的方法可以将一个步骤定义为既给定又何时

Is there an easy way to define a step as being both a Given and a When

我正在构建一个 Py Behave 测试框架,并且有许多场景,其中以前的 When 步骤变成了 Given

E.G 在一个场景中

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in 

但在其他场景下

Given a user is on the logon page
And they login with credentials <user>

在我的步骤中,这将显示为

 @given('they login with credentials {user}')
 def step_impl(context):
    Do login code

 @when('they login with credentials {user}')
 def step_impl(context):
    Do login code

有没有一种方法可以避免将所有这些步骤写出两次,但又能够将 Whens 定义为 Givens?

你可以使用behave中提供的@step装饰器

场景一

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in

场景二

Given a user is on the logon page
And they login with credentials <user>

解决方案:

@step('they login with credentials {user}')
 def step_impl(context):
    Do login code

参考:https://github.com/behave/behave/issues/550