📅  最后修改于: 2023-12-03 15:00:54.781000             🧑  作者: Mango
Gherkin是一种基于行为驱动开发(BDD)的DSL(领域特定语言)。它的目的是使用自然语言编写易于理解的系统行为规范,以便在软件开发过程中为所有各方参与者(开发者、测试人员、业务专家等)建立通用的交流桥梁。Gherkin语法也是cucumber测试框架的基础。
Gherkin语法以关键字为基础,其中包括:Feature、Background、Scenario、Scenario Outline、Examples、Given、When、Then、And、But。下面是一个简单的Gherkin语句示例:
Feature: User Login
As a registered user
I want to login into my account
So that I can access my dashboard
Background:
Given I am on the login page
Scenario: Successful login
When I enter my valid credentials
And I click on Login button
Then I should be redirected to dashboard page
Scenario: Unsuccessful login
When I enter my invalid credentials
And I click on Login button
Then I should see an error message
其中的关键字含义如下:
使用Gherkin编写测试用例,有两种主要方式:
cucumber
框架中提供的Gherkin方法编写测试用例。示例代码如下:Feature: User Login
As a registered user
I want to login into my account
So that I can access my dashboard
Background:
Given I am on the login page
Scenario: Successful login
When I enter my valid credentials
And I click on Login button
Then I should be redirected to dashboard page
Scenario: Unsuccessful login
When I enter my invalid credentials
And I click on Login button
Then I should see an error message
cucumber
命令行执行测试用例。示例代码如下:Feature: User Login
As a registered user
I want to login into my account
So that I can access my dashboard
Background:
Given I am on the login page
Scenario: Successful login
When I enter my valid credentials
And I click on Login button
Then I should be redirected to dashboard page
Scenario: Unsuccessful login
When I enter my invalid credentials
And I click on Login button
Then I should see an error message
执行cucumber
命令时,会搜索项目目录中的.feature
文件,并自动执行其中的测试用例。
Gherkin语法是BDD开发的核心,它能够有效地激励所有各方参与者,避免团队间的语言障碍和误解,实现高效的系统开发。如果你是一个软件工程师,它也能够提高你的测试用例编写效率,让你的测试工作更加高效、快速、结构化。