测试只能证明程序有错误, 不能证明程序没有错误.
1. What is Cucumber?
Cucumber is a software tool that supports behavior-driven development. It allows expected software behaviors to be specified in a logical language that customers can understand. The specifications consists of multiple examples, or scenarios. For example:
or
or
Each scenario(story) is a list of steps for Cucumber to work through. Cucumber verifies that the software conforms with the specification and generates a report indicating success or failure for each scenario.
In order for Cucumber to understand the scenarios, they must follow some basic syntax rules, called Gherkin.
2. What is Gherkin?
Gherkin is a set of grammar rules that makes plain text structured enough for Cucumber to understand. The scenario above is written in Gherkin.
Gherkin serves multiple purposes:
-
Unambiguous executable specification
-
Automated testing using Cucumber
-
Document(or story) how the system actually behaves
-
The Cucumber grammar exists in different flavours for many spoken languages so that your team can use the keywords in your own language.
Gherkin documents are stored in .feature
text files and are typically versioned in source control alongside the software.
3. Gherkin Keywords
Each line that isn’t a blank line has to start with a Gherkin keyword, followed by any text you like. The only exceptions are the feature and scenario descriptions.
The primary keywords are:
-
Feature
-
Scenario
-
Given
,When
,Then
,And
,But
for steps (or*
) -
Background
-
Scenario Outline
(orScenario Template
)
There are a few secondary keywords as well:
-
"""
(Doc Strings) -
|
(Data Tables) -
@
(Tags) -
#
(Comments)
4. Automated testing using Cucumber
-
We write Java source code to implement the scenario.
-
We run test scenarios by Cucumber class.
-
Scenario: Get Cucumber report
-
Given we run some test scenarios
-
When completed
-
Then we can get a Cucumber report
-
Of course, the blog is only a small part of Cucumber knowledge, and if you want, you can learn and discover a lot of interesting things from Cucumber.