【问题标题】:Does Cucumber/Gherkin knows something like Gauge concept?Cucumber/Gherkin 知道 Gauge 概念吗?
【发布时间】:2020-03-06 10:06:22
【问题描述】:
我使用 Gauge 有一段时间了,他们有一个 概念 的想法,它被定义为“概念提供了将可重复使用的逻辑步骤组组合成一个单元的能力。A概念通过组合逻辑步骤组来呈现业务意图的摘要”(Gauge conpect documentation)。
这样,我们可以轻松地将多个步骤组合在一起,并在另一个测试用例中将它们作为一个步骤重用。
我想知道,Cucumber/Gherkin 是否有类似的东西?
背景是我有一个 end2end 测试用例,其中包含多个断言,我喜欢解开并创建多个场景。但是我会有多个场景重复的步骤 - 我想将相关的组合在一起,从而最大限度地减少各个场景中的各个步骤。
谢谢:)
【问题讨论】:
标签:
automated-tests
cucumber
gherkin
getgauge
【解决方案1】:
Gauge 是一个编写验收测试的框架。 Gauge 中的规范基本上是一个可以执行的手动测试脚本。因此,重用步骤是有意义的,因为它们倾向于描述低级操作。
另一方面,Cucumber 促进了 BBD,您使用 Gherkin 来捕获系统的行为,而不是测试中的操作。因此,不要编写描述操作的 Login as user "Charles: and create project "Firebird",而是编写 Given Administrator "Charles" created the project "Firebird"。
这是一个相当大的视角转变,但有助于清楚地传达软件应该做什么,而不是应该如何操作。
因此,您通常会避免在 Gherkin 中编写低级操作。相反,您将这些提取到方法中并从您的步骤中调用这些方法。然后,您还可以在其他步骤中重复使用这些方法。
例如,假设我们有两个创建用户的步骤:
Given Administrator "Charles" created the project "Firebird"
And "Jullia" is added to project "Firebird"
@Given("{role} {persona} created the project {project}")
public void persona_with_role_creates_a_project(Role role, Persona persona, Project project){
createRole(role);
createUserForPersona(persona);
addRoleToUserForPersona(persona, role);
loginUserForPersona(persona);
createProject(project);
}
@And("{persona} is added to project {project}")
public void persona_with_role_creates_a_project(Persona persona, Project project){
createUserForPersona(persona);
addUserForPersonaToProject(persona, project);
}
@ParameterType("\"([^"]+)\"")
public Persona persona(String name){
// look up the persona by name from somewhere e.g. a config file
}
ect...
private void createRole(Role role){
// API calls to make a role here
// For test isolation it is important you don't reuse anything between tests.
}
private void createUserForPersona(Persona persona){
// API calls to create a user
// Don't forget to store the credentials for the persona somewhere
}
ect..
请注意,创建用户和项目可能需要大量信息。因此,我们引用一个角色(“Charles”、“Firebird”)作为我们创建的项目类型的模板,而不是在功能文件中拼写所有这些信息。我们可以通过使用参数类型({persona}、{project})将这些提供给步骤定义对象而不是纯字符串。这些将在执行步骤之前将字符串转换为对象。
【解决方案2】:
在 Gherkin 中,您只需为您的一组步骤创建一个步骤定义,然后像在人类语言中一样使用它。下面的 Ruby 示例说明了 Given “a valid user” 在 Given “user is on the login page” 下的重用强>。
Given /^a valid user$/ do
@user = User.create!({
:email => "example@gmail.com",
:password => "password"
})
end
Given /^user is on the login page$/ do
Given "a valid user"
visit signin_url
end
When /^user fills in Username with (.*?)/ do |username|
fill_in "Email", :with => username
end
And /^user fills in Password with (.*?)$/ do |password|
fill_in "Password", :with => password
end
And /^user presses "Login"$/ do
click_button "Sign in"
end
Then /^user should be on the users home page$/ do
assert_equal (getCurrentUrl(),
"https://www.linkedin.com/feed/")
end