我有一个黄瓜步骤,如下所示:
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
}
但是我的第二个黄瓜步骤(“我为用户输入正确的凭据”)与第一步定义匹配,仅在凭据中添加了“正确”一词。
答案 0 :(得分:2)
几个答案表明了一种必要的方法,该方法被认为是BDD中的反模式。相反,我强烈建议您使用自然语言或商务语言对小黄瓜遵循声明式方法。如果您实际上正在测试登录功能,我建议使用类似的方法
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
此方法的一个强大优点是,通过使数据/凭据处理程序环境具有感知性,可以轻松地在不同的环境中重用测试套件。
答案 1 :(得分:1)
第一个规则应更改为
@When("I enter the (\\S+) for the user$")
在这里,\S+
匹配1个或多个非空白字符。如果不能有非空白字符,请使用\S*
。
要匹配两个“单词”,您可以使用
@When("I enter the (\\S+\\s+\\S+) for the user$")
请注意,您可以使用量词来控制“单词”的数量,例如这将匹配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$")
答案 2 :(得分:1)
有两种方法可以改善这些步骤并避免使用正则表达式。
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
这种方法消除了黄瓜的所有复杂性,并将其放置在您要用来创建用户的辅助方法中。
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相比,代码在处理复杂性方面要好得多。
自从Cucumber被命名之前,我就一直在求知,现在我求助时从不使用正则表达式或方案大纲。您也不需要。
答案 3 :(得分:-1)