扩展 Cucumber 步骤
Extending a Cucumber step
我的 Cucumber 步骤如下所示:
When I enter the credentials for the user
还有一个说
When I enter the correct credentials for the user
对应的步骤定义为:
@When("I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
@When("I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
但是我的第二个黄瓜步骤 ("I enter the correct credentials for the user") 与第一步定义匹配,只是在凭据中添加了单词 "correct"。
- 我该如何解决这个问题?
- 我是正则表达式的新手。是否可以从 'When' 步骤中排除 "correct" 部分,这样我就可以有一个基本步骤,可以是 'extended' 和 "correct" 部分?
请执行以下步骤定义,让我们知道是否适合您。
@When("^I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
@When("^I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
无参数
带参数
两个元字符(^、$)被称为锚点,因为它们用于连接每个
正则表达式的结尾到他们表示的字符串的开头和结尾
比赛开始。
第一条规则应更改为
@When("I enter the (\S+) for the user$")
这里,\S+
匹配1个或多个非空白字符。如果没有非空白字符,请使用 \S*
.
要匹配两个 "words",您可以使用
@When("I enter the (\S+\s+\S+) for the user$")
请注意,您可以使用量词控制 "words" 的数量,例如这将匹配 2 或 3 个词:
@When("I enter the (\S+(?:\s+\S+){1,2}) for the user$")
要匹配 2 个或更多单词:
@When("I enter the (\S+(?:\s+\S+){1,}) for the user$")
@When("I enter the (\S+(?:\s+\S+)+) for the user$")
您可以通过多种方法改进这些步骤并避免使用正则表达式。
1) 让用户知道其凭据并让步骤向用户询问凭据
所以你会
Given I am a user
@user = create_user # method creates a user with credentials
end
When `I enter the users credentials` do
fill_in username: @user.username
fill_in password: @user.password
end
When `I enter the wrong credentials for the user` do
fill_in username: @user.username
fill_in password: @user.bad_password # or perhaps just bad_password
end
这种方法消除了 Cucumber 的所有复杂性,并将其置于您调用以创建用户的辅助方法中。
2) 为您的步骤定义提供更多参数
When 'I enter the credentials user: (\S+) password: (\S+) do |username, password|
fill_in username: username
fill_in password: password
end
When 'I enter the bad credentials user: (\S+) password: (\S+) do |username, password|
fill_in username: username
fill_in password: password
end
我非常喜欢第一种方法,您应该保持功能和场景超级简单,并将复杂性降低到代码中。代码比黄瓜更擅长处理复杂性。
我在 Cucumber 命名之前就开始使用 cuking,现在我在使用 cuke 时从不使用正则表达式或场景大纲。你也不需要。
几个答案表明了一种命令式方法,它被认为是 BDD 中的反模式。相反,我强烈建议您使用自然语言或商业语言对 Gherkin 进行声明式处理。如果您实际上是在测试登录功能,我建议您这样做:
When an authorised user enters their credentials
或基于角色
When an Administrator is authorised
如果登录实际上是被测功能的先决条件,那么诸如:
Given an authorised user
或
Given an authorised Administrator
这些可以使用凭据管理器进行备份。
... = ExpectedData.credentialsFor("@authorised");
标签应该代表特征,而不是预期数据的身份,要从包含如下内容的测试数据数据库或 csv 中检索:
@admin, administrator, password
@authorised, user, password
@unauthorised, user, wrong
所有测试数据输入都应使用相同的方法,例如:
Given a Cash Customer
Given a Credit Customer
Given a Customer with an overdue account
这种方法的一个很大的好处是,通过使 data/credential 处理程序环境感知,可以很容易地在不同的环境中重用测试套件。
我的 Cucumber 步骤如下所示:
When I enter the credentials for the user
还有一个说
When I enter the correct credentials for the user
对应的步骤定义为:
@When("I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
@When("I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
但是我的第二个黄瓜步骤 ("I enter the correct credentials for the user") 与第一步定义匹配,只是在凭据中添加了单词 "correct"。
- 我该如何解决这个问题?
- 我是正则表达式的新手。是否可以从 'When' 步骤中排除 "correct" 部分,这样我就可以有一个基本步骤,可以是 'extended' 和 "correct" 部分?
请执行以下步骤定义,让我们知道是否适合您。
@When("^I enter the ([^\"]*) for the user$")
public void stepDefinition(String cred){
//code
}
@When("^I enter the correct ([^\"]*) for the user$")
public void otherStepDefinition(String cred){
//other code
}
无参数
带参数
两个元字符(^、$)被称为锚点,因为它们用于连接每个 正则表达式的结尾到他们表示的字符串的开头和结尾 比赛开始。
第一条规则应更改为
@When("I enter the (\S+) for the user$")
这里,\S+
匹配1个或多个非空白字符。如果没有非空白字符,请使用 \S*
.
要匹配两个 "words",您可以使用
@When("I enter the (\S+\s+\S+) for the user$")
请注意,您可以使用量词控制 "words" 的数量,例如这将匹配 2 或 3 个词:
@When("I enter the (\S+(?:\s+\S+){1,2}) for the user$")
要匹配 2 个或更多单词:
@When("I enter the (\S+(?:\s+\S+){1,}) for the user$")
@When("I enter the (\S+(?:\s+\S+)+) for the user$")
您可以通过多种方法改进这些步骤并避免使用正则表达式。
1) 让用户知道其凭据并让步骤向用户询问凭据
所以你会
Given I am a user
@user = create_user # method creates a user with credentials
end
When `I enter the users credentials` do
fill_in username: @user.username
fill_in password: @user.password
end
When `I enter the wrong credentials for the user` do
fill_in username: @user.username
fill_in password: @user.bad_password # or perhaps just bad_password
end
这种方法消除了 Cucumber 的所有复杂性,并将其置于您调用以创建用户的辅助方法中。
2) 为您的步骤定义提供更多参数
When 'I enter the credentials user: (\S+) password: (\S+) do |username, password|
fill_in username: username
fill_in password: password
end
When 'I enter the bad credentials user: (\S+) password: (\S+) do |username, password|
fill_in username: username
fill_in password: password
end
我非常喜欢第一种方法,您应该保持功能和场景超级简单,并将复杂性降低到代码中。代码比黄瓜更擅长处理复杂性。
我在 Cucumber 命名之前就开始使用 cuking,现在我在使用 cuke 时从不使用正则表达式或场景大纲。你也不需要。
几个答案表明了一种命令式方法,它被认为是 BDD 中的反模式。相反,我强烈建议您使用自然语言或商业语言对 Gherkin 进行声明式处理。如果您实际上是在测试登录功能,我建议您这样做:
When an authorised user enters their credentials
或基于角色
When an Administrator is authorised
如果登录实际上是被测功能的先决条件,那么诸如:
Given an authorised user
或
Given an authorised Administrator
这些可以使用凭据管理器进行备份。
... = ExpectedData.credentialsFor("@authorised");
标签应该代表特征,而不是预期数据的身份,要从包含如下内容的测试数据数据库或 csv 中检索:
@admin, administrator, password
@authorised, user, password
@unauthorised, user, wrong
所有测试数据输入都应使用相同的方法,例如:
Given a Cash Customer
Given a Credit Customer
Given a Customer with an overdue account
这种方法的一个很大的好处是,通过使 data/credential 处理程序环境感知,可以很容易地在不同的环境中重用测试套件。