Test automation with Cucumber-JVM
-
Upload
alan-parkinson -
Category
Technology
-
view
19.018 -
download
10
description
Transcript of Test automation with Cucumber-JVM
![Page 1: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/1.jpg)
Boston Java Meetup, 13/06/2013
Test Automation with Cucumber-JVMAlan Parkinson
@alan_parkinson
![Page 2: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/2.jpg)
Cucumber is a tool that executes plain-text functional descriptions as automated tests
• Tests written in business-facing text Supports over 40 spoken languages This test is called Gherkin
• Automation written in target language Ruby, Java, Scala, Groovy, etc.. .net, python, PHP
![Page 3: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/3.jpg)
Behaviour Driven Development
![Page 4: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/4.jpg)
• Program does not do what it is intended
• Fixed in development/QA
• Program does what is intended
• Unstated, misunderstood or tacit requirements
IMPLEMENTATION DEFECT
REQUIREMENTS DEFECT
Software - What can go wrong?
![Page 5: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/5.jpg)
A woman asks her husband, a programmer, to go shopping.
She tells him “please, go to the nearby grocery store to buy some bread. Also, if they have eggs, buy 6”
![Page 6: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/6.jpg)
50-60%
of issues identified by testers can be chalked down as
Requirements Defects
100-200%
more expensive to fix than other defects because
the code is already written
![Page 7: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/7.jpg)
How does BDD help?• All stakeholders involved in the discussion drives out clear and
concise specifications centred around business value.
• Write specifications in natural language so everyone is on the same page
• Turn the specification into acceptance tests that guarantees the software does as it is intended
• Use those tests as the software evolves to guarantee that it continues to work as intended.
• Specifications are documentation – each scenario describes how the system is being used
![Page 8: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/8.jpg)
Cucumber, The Origins• Cucumber
Ruby Born 2007-2008
• Cuke4Duke First seen in 2008 JRuby
• Cucumber-JVM Released April 2012 Java, Groovy, Clojure, Scala, Ioke, Jython, Rhino (JavaScript) and JRuby
![Page 9: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/9.jpg)
Maven Dependencies
cucumber-java
cucumber-junit
cucumber-picocontaine
r
cucumber-guice
cucumber-spring
![Page 10: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/10.jpg)
Maven Dependencies<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${cucumber.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
</dependency>
![Page 11: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/11.jpg)
Scenarios - Given When Then
Given an owner with a registered pet called “Sly“
When the owner registers a new pet called “Sly“
Then the user should be warned that the pet already exists
![Page 12: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/12.jpg)
Writing Scenario's in Cucumber• Scenarios are organized together into features
• Each feature is represented as a plain text file.
• Feature files must have the “.feature”
extension
• Each feature can contain many scenarios
![Page 13: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/13.jpg)
JUnit Runner
import org. junit . runner.RunWith;import cucumber. junit .Cucumber;
@RunWith (Cucumber. c lass )publ ic c lass Runner IT {}
![Page 14: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/14.jpg)
JUnit Runner 2• Body of the class should be empty
No @Test annotated methods No @Before or @After methods
• Arguments can be provided with an additional annotation
![Page 15: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/15.jpg)
JUnit Runner - Arguments
@RunWith (Cucumber. c lass )@Cucumber. Opti ons( features = "src/test/resources/features“)publ ic c lass Runner IT {}
![Page 16: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/16.jpg)
Step Definitions• A single Method representing a Step from a Feature file
• The Method is annotated with information about the step it represents
• Cucumber generates “Code Snippet's” for Step Definition it can’t find
![Page 17: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/17.jpg)
Step Annotationsimport cucumber.annotation.en.*;
Each Gherkin step keyword has a Annotation
@When("^we want to stop traffi c$")
public void methodName() { … }
![Page 18: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/18.jpg)
Passing and Failing• Each step has it own result state
Passed Pending Skipped Failed
• Individual step result state combined determine the Scenarios success or failure
• Steps can be failed on exceptions or JUnit Assertion failures
![Page 19: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/19.jpg)
Regex LessonCucumber uses Regular Expressions to match steps to definitions.
@When("^we want to stop traffic$")
^ Matches the starting position
$ Matches the ending position
![Page 20: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/20.jpg)
Capturing Arguments@Given("^an owner with a pet called \"Sly\"$")
public void an_owner_with_a_pet_called() {
…
}
@Given("^an owner with a pet called \"([^\"]*)\"$")
public void an_owner_with_a_pet_called(String name) {
…
}
![Page 21: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/21.jpg)
Capturing Arguments 2• The pair of brackets is a capture group
• Everything captured is passed to method parameter
• Cucumber will automatically convert to the method parameter type
• Equal number of method parameters and capture groups
![Page 22: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/22.jpg)
Type Conversion• If the captured string is not in the correct format for type conversion an error will be thrown
• Use regular expressions to capture the correct format string for the type
Note: Escape regular expression characters with backslash
![Page 23: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/23.jpg)
Typical Capture Groups(.+) Good for capturing strings and dates
\"([^\"]*)\" Captures a string within Double quotes
(\\d) Capture decimal numbers only
(\\d+\\.\\d+) Capture floating point numbers
(true|false) Captures a Boolean value
![Page 24: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/24.jpg)
Date Formats
@Given ("^today is (.+)$")
public void today_is(Date date) {
….
}
Dates are recognized if it’s a format from the DateFormat class
![Page 25: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/25.jpg)
Date FormatsSHORT is completely numeric, such as 12.13.52 or 3:30pm
MEDIUM is longer, such as Jan 12, 1952
LONG is longer, such as January 12, 1952 or 3:30:32pm
FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
![Page 26: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/26.jpg)
Date Formatsimport cucumber.DateFormat;
@Given ("^today is (.+)$")
public void today_is(
@DateFormat ("yyyy-MM-dd") Date date) {
….
}
![Page 27: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/27.jpg)
Tag use cases• Grouping Scenarios together
Identify slow running test that shouldn’t be run frequently Identify tests that require a particular library or server
![Page 29: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/29.jpg)
Running Scenarios without a TagCucumber can exclude Scenarios with a particular tag by inserting the tilde character before the tag
For the following command will run all Scenarios without the WebDriver tag
@Cucumber.Options(
tags = {“~@WebDriver”}, … )
![Page 30: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/30.jpg)
Tag expressions – Logical ORSeparate a list of tags by commas for a Logical OR tag expression
@Cucumber.Options(
tags = {“@WebDriver,@email”}, … )
![Page 31: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/31.jpg)
Tag expressions – Logical AND Specifying multiple tag arguments creates a logical AND between each tag expression.
@Cucumber.Options(
tags = {“@WebDriver”, ”@email”}, … )
![Page 32: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/32.jpg)
Tag expressions – AND ORRun scenarios that are tagged with @WebDriver or @email but not @slow
@Cucumber.Options(
tags = {“@WebDriver,@email”, “~@slow”}, … )
![Page 33: Test automation with Cucumber-JVM](https://reader036.fdocuments.us/reader036/viewer/2022081511/5553ab31b4c905d9448b484c/html5/thumbnails/33.jpg)
Further information
Official website: cukes.info
Github: github.com/cucumber/cucumber-jvm
Hindsight Software: hindsighttesting.com