Automated test of cucumber (a)
https://www.jianshu.com/p/3857f2c3a8d4
Brief introduction
cucumber is BDD (Behavior-driven development, behavior-driven development) is a test automation product. It uses natural language to describe the test, so that non-programmers can understand them. Gherkin is a simple natural language syntax for this test, and Cucumber is a tool that they can perform. About BDD are interested in self-understanding. Attached cucumber official website link , which also has information about the BDD.
on cucumber essentially regular expression matching method using natural language, and then perform the corresponding sequence according to the purpose of the test.
This article is basically just the official website of porters, some summary information, best Tell me what network documentation.
Gherkin
Gherkin is a simple natural language grammar test.
A complete test is composed of a plurality of step, step that is the smallest unit, how to reuse step is very critical issue. More step to form a Scenario, namely a complete test case. More Scenario composed of a Feature, that is a group of related test case.
Keyword
- Feature
- Example(or Scenario)
- Given,When,Then,And,But(steps)
- Background
- Scenario Outline (or Scenario Template)
- Examples (or Scenarios)
A simple example
Feature: Is it friday yet?
this is a descriptions
Everybody want to know when it's Friday
Scenario: Sunday isn't Friday Given today is Sunday When I ask whether it's Friday yet Then I should be told "Nope" Scenario: Friday is Friday Given today is Friday When I ask whether it's Friday yet Then I should be told "TGIF"
Feature
Feature is the beginning of all the tests. Followed by a description of the text, indicating that the test file is doing.
description
text description is a description of scalability can be followed Feature, Example, Background, Scenario, Scenario Outline below.
Example和Scenario
Example Scenario and a synonym is a specific test Case, comprising a plurality of step. In general, the Given are made (given an initial condition), the When (what happened), the Then (what is the result) thereof.
Steps
cucubmer step is a minimum unit, each step is Given, When, Then, And, Or But beginning. If the content behind the key word is exactly the same, then the cucumber would think these two phrases are repeated, even in front of the keyword is not the same as
Given there is money in my account
Then there is money in my account
This limitation also prompted us to use more precise language to describe
Given my account has a balance of £430
Then my account should have a balance of £430
Given
Given an initial state of the system is generally described in Scenario. Its purpose is to make the system prior to use in a state only has to avoid talking about things on the interaction Given in.
When
When describing an event or action. He may be the interaction between the system and can also be triggered by another system event. cucumber strongly recommended that each Scenario When there is only one, when you feel the need to add more When the time is often the need to split the signal into a plurality of Scenario.
Then
Then describe the desired output or result. Then step definition of assertions should be used to compare the expected and actual value, and unit testing almost.
But和And
When there are several Given, When, Then, you can write
Example: Multiple Givens
Given one thing
Given another thing
Given yet another thing
When I open my eyes
Then I should see something
Then I shouldn't see something else
And it may also be used to increase readability and But
Example: Multiple Givens
Given one thing
And another thing
And yet another thing
When I open my eyes
Then I should see something
But I shouldn't see something else
step definition in personally do not recommend the use of @And and @But, using only @ Given, @ When, @ Then, this language is more clear, and But because And can be used in the following Given, When, Then of.
Background
When there are multiple Scenario in the same Feature Given there are the same, you can use the Background Given these drawn together. So this time each Scenario Feature in operation, will first run a Background. Each Feature in only one Background.
tips
- Do not set the complex state, unless the state is actually a customer needs to know
- Keeping background brief, because when reading Scenario, is the need to remember the background. Preferably not more than 4 lines
- A feature can have only one background, if you need a different background for different programs, we need to split into different feature
Scenario Outline
Scenario Outline namely Scenario templates can also be written Scenario Template. It can run the same Scenario times.
Scenario: eat 5 out of 12
Given there are 12 cucumbers When I eat 5 cucumbers Then I should have 7 cucumbers Scenario: eat 5 out of 20 Given there are 20 cucumbers When I eat 5 cucumbers Then I should have 15 cucumbers
Available Scenario Outline simplified
Scenario Outline: eating
Given there are <start> cucumbers When I eat <eat> cucumbers Then I should have <left> cucumbers Examples: | start | eat | left | | 12 | 5 | 7 | | 20 | 5 | 15 |
DataTable
Gherkin can pass List, Map, called DataTable, specific examples of view on the github document
dialect
Gherkin supports a variety of dialects, corresponding to the keyword conversion to see the document
State share
Different variables defined in step, can achieve the purpose of shared context variables by member variables java class. As an example of today and actualAnswer.
The state will make the leak scenarios become fragile and difficult to maintain. For more information see the document
Note
Gherkin using the # symbol as a comment
step definition
step definition refers to the expression corresponding to one or more of the java method Gherkin step n. The following example is an example corresponding to the above step definition
public class FridayYet {
private String today; private String actualAnswer; @Given("^today is (.*)$") public void todayIsSunday(String day) { this.today = day; } @When("^I ask whether it's Friday yet$") public void iAskWhetherItsFridayYet() { if (this.today.equals("Friday")) { this.actualAnswer = "TGIF"; } else { this.actualAnswer = "Nope"; } } @Then("^I should be told \"(.*)\"$") public void iShouldBeToldNope(String expectedAnswer) { Assert.assertEquals(expectedAnswer, actualAnswer); } }
Alternative text
Use / text instead. Matching the following Examples I have {int} cucumber (s) in my belly and I have {int} cucumber (s) in my stomach
I have {int} cucumber(s) in my belly/stomach
Before version 3.0.1, you can not escape /, it is always interpreted as an alternative text, with post-3.0.1 \ / Escape /
attached github version change history
tag
tag provides two roles
- And providing @before @after hooks (tagged-hooks)
- Runs the specified tag only with a runtime embodiment
having characteristics inherited tag, i.e., on the marker tag Feature, Scenario under this Feature, step inherits the tag.
@simpleDemo
Feature: Is it friday yet?
@RunWith(Cucumber.class)
@CucumberOptions(tags = "@simpleDemo")// 指定只运行代用@simpleDemo的 public class CucumberTest { }
@Before("@simpleDemo")
public void beforeOperation() { } @After("@simpleDemo") public void afterOperation() { }
Tested two write tag, will take effect two
combined Background specific sequence of operation is before tag -> Background -> Scenario -> after tag
- 2 points above the official website is presented, personally I think that there is a third point role: When generating a report disaggregated role
update at 2019 Nian 03 Yue 25 Ri
tips: cucumber.api.spring.SpringTransactionHooks (cucmber-spring package) provides a spring hook @txn transaction provides rollback functionality.
IDE plug-ins
IDEA Cucumber for Java
report
Specific did not see, we are interested can inform themselves about
Automated test of cucumber (d)