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

Ric*_*d C 2 python python-behave

我正在构建一个 Py Behave 测试框架,并且有许多场景,其中以前的“何时”步骤变成了“给定”步骤

EG 在一种情况下

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in 
Run Code Online (Sandbox Code Playgroud)

但在其他场景下

Given a user is on the logon page
And they login with credentials <user>
Run Code Online (Sandbox Code Playgroud)

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

 @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
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以避免将所有这些步骤写两次,但能够将何时定义为给定?

小智 5

您可以使用behavior中提供的@step装饰器

场景一

Given a user has is on the logon page 
When they login with credentials <user>
Then the user logs in
Run Code Online (Sandbox Code Playgroud)

场景二

Given a user is on the logon page
And they login with credentials <user>
Run Code Online (Sandbox Code Playgroud)

解决方案:

@step('they login with credentials {user}')
 def step_impl(context):
    Do login code
Run Code Online (Sandbox Code Playgroud)

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