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

2024-04-16 10:40:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

例如在一个场景中

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

有没有一种方法可以避免把所有这些步骤写两遍,但是能够把什么时候定义为给定的?你知道吗


Tags: theisondefstepwithpage场景
1条回答
网友
1楼 · 发布于 2024-04-16 10:40:47

您可以使用behave中提供的@step decorator

情景一

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

相关问题 更多 >