logo图片
Cucumber教程

Cucumber Gherkin语言

Cucumber Gherkin语言

正如我们在Cucumber测试中学到的那样,创建了特征文件带有可执行的测试脚本。编写这些可执行测试脚本的语言称为 Gherkin 语言。
基本上,Gherkin 是一种用于解释和执行测试脚本的纯英文文本语言。

当 Cucumber 支持英文文本时,为什么我们需要单独的 Gherkin 语言?

Cucumber 支持纯英文文本已经讨论过多次,那么为什么我们需要单独的 Gherkin 语言。答案在于BDD(行为驱动开发)的概念。
众所周知,BDD涉及各种软件准备测试脚本时的行为开发可能性。这些可以是与开发相关的可能性或与业务相关的可能性。为了实现这些可能性,我们在开发测试脚本时需要来自不同社区的成员,如测试人员、开发人员、产品所有者和项目经理。
由于这些成员不属于同一社区;因此,几乎不可能每个人都使用共同语言。因此,测试脚本的概念处于高风险中。为了降低这种风险,开发了 Gherkin。
Gherkin 在纯英文文本中提供了一组通用的关键字,可供来自不同社区的成员使用,并且可以从测试脚本中获得相同的输出.
Gherkin 提供以下特定关键字来编写功能文件中的常见测试脚本:
Feature Scenario Given When Then But And Background Gherkin Language

Feature

Cucumber的每个特征文件测试以 feature 关键字开始。它是要测试的独立单元或功能。例如登录功能、支付转账功能、注册功能等。
示例:
Feature: Login 
Scenario: Login verification
Given user navigates to the website lidihuo.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
    

Scenario

每个功能都包含测试该功能所需的测试次数。每个测试都被命名为一个场景。
例如,功能登录功能可以包含两个场景,第一个用于登录成功,第二个用于登录失败 .
Scenario: Successful Login with Valid entries
Given user navigates to the website lidihuo.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
    

Scenario: Unsuccessful Login with Invalid entries
Given user navigates to the website lidihuo.com
And user logs in through Login Window by using Username as "USER" and Password as "1234erty"
But user entered wrong username and password
Then login must be unsuccessful.
    

Given

这个关键字是指测试的前置条件。例如,要访问任何 Web 应用程序,第一个要求或前提是导航其主页,因为我们可以从主页导航到其他链接,例如 signup、login 等
示例:
Feature: Login 
Scenario: Login verification
Given user navigates to the website lidihuo.com
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
    

When

通常指的是用户要执行的操作。
示例:
Scenario: Successful Login with Valid entries
Given user navigates to the website lidihuo.com
When User Navigate to Login Page 
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
    
当用户用"nikita.sharma27@gmail.com"填充"注册电子邮件文本框"。

Then

此关键字指的是上一步或即将进行的操作的结果。
示例:
Scenario: Successful Login with Valid entries
Given user navigates to the website lidihuo.com
When User Navigate to Login Page 
And user logs in through Login Window by using Username as "USER" and Password as "PASSWORD"
Then login must be successful.
    

But

此关键字用于添加否定条件。
示例:
Scenario: Unsuccessful Login with Invalid entries
Given user navigates to the website lidihuo.com
And user logs in through Login Window by using Username as "USER" and Password as "1234erty"
But user entered wrong password
Then login must be unsuccessful.
    

And

此关键字用于在您的步骤中添加更多条件。
示例:
Given User is on Home Page
And Login Link is displayed
When User Navigates to Login form
And User enters email and Password
Then Login Successfully will be displayed 
And Logout Link should be displayed
    

Background

此关键字用于定义功能文件中所有测试通用的步骤。例如,导航到主页、点击登录、输入用户名和密码、点击提交按钮是几乎所有 Web 应用程序中的常见步骤。
昵称: 邮箱:
Copyright © 2020 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4