我正在采取Gherkin / Cucumber的第一步。
我一直在阅读有关语法以及何时/如何通过官方文档正确使用每个标记,但是我想知道是否有一种合适的方法来对在#34内反复使用的某些语句进行分组;当"以类似于分组的方式进行分级"给予" "背景"中的陈述部?
示例:
Feature: Wanting to reduce code duplications
Background:
Given I have done some set up
And I have done some more set up
@Scenario_1
Scenario: An example scenario
# Some Givens defined in Background
Given a unique step is taken
# When
When I perform some action that will be repeated
And I perform another action that will be repeated
And I perform a non-repeated task
# Then
Then something will be expected at the end
@Scenario_2
Scenario: An second example scenario
# Some Givens defined in Background
Given a different unique step is taken
# When
When I perform some action that will be repeated
And I perform another action that will be repeated
And I perform a different non-repeated task
# Then
Then something different will be expected at the end
有什么方法可以抽象或分组"当"语句:
When I perform some action that will be repeated
And I perform another action that will be repeated
进入他们自己的部分,例如"背景"?
答案 0 :(得分:1)
首先,请记住Gherkin应该是BDD,意思是行为驱动开发。你陈述步骤的方式不是行为,而是行动。 When I perform some action that will be repeated
未描述系统或用户的任何行为。
对于重复的任务,生成描述行为的When
,然后在Java代码中定义所需的所有重复步骤。
示例:
Given I go to a website
When when I click on username and typ 'User1'
And when I click on password and typ 'welcome123'
And when I click on the login button
Then the dashboard is shown
And I see that there is a proper header
基本上,在这种情况下,您正在用小黄瓜描述一个物理测试用例。
您真正想要实现的目标是:
Given I navigate to a website
When I login
Then the dashboard is shown by system
您可以通过以下方式详细说明步骤:
Given I navigate to website 'http://www.google.com/' in browser 'Chrome'
When I login as user 'User1'
Then the dashboard is shown by system
通过这种方式,您可以立即知道测试的内容,使用的主要数据是什么,您可以快速查看它是否符合要求。
因此,在您的情况下,尝试减少小黄瓜中的步骤描述,您的重复项将自动减少。