Comprehensive Guide to Using Cucumber‑JVM for Android SDK Automated Testing
This article provides a detailed tutorial on using Cucumber-JVM for Android SDK automated testing, covering framework basics, Gherkin syntax, step definitions, parameter handling, tag usage, Maven setup, and execution methods with code examples.
Preface
Recently our automation testing project for an advertising SDK required a new execution plan and framework optimization, so we recorded the problems encountered and their solutions.
Introduce Cucumber‑JVM framework
Multi‑port distribution with Anyproxy
Appium multi‑port device startup
Distributed execution and task dispatch in Cucumber
Maven configuration issues
Pipeline integration of Cucumber
About Cucumber‑JVM
Project Background
The automation solution targets an Android SDK; we chose a Java‑based BDD framework (Cucumber‑JVM) because the team is familiar with Java and the Gherkin natural‑language format helps describe complex SDK tracking fields.
Features of Cucumber‑JVM
Cucumber supports Behaviour‑Driven Development (BDD) and parses .feature and .txt files using the Gherkin language.
Implemented in Ruby, it can run scripts written in Java, .NET, Python, etc.
Supports more than 30 languages for textual descriptions.
Integrates easily with TestNG, Selenium, Spring, Ruby on Rails and other mainstream test frameworks.
Our SDK project uses the Java implementation: Cucumber‑JVM.
Cucumber‑JVM Keywords
The core syntax is provided by Gherkin; each non‑empty line must start with a reserved keyword.
Feature
Scenario
Given, When, Then, And, But
Background
Scenario Outline
Examples
Additional keywords include """ (doc strings), | (table separator), @ (tags) and # (comments).
Feature
A .feature file contains a collection of scenarios. It has three basic elements: the Feature: keyword, a name on the same line, and an optional multi‑line description.
Feature: this is a feature
# detailed description of the feature file
I am a description, blablabla...
Scenario: just a demo
Given the cow weighs 450 kg
When we calculate the feeding requirementsScenario
A scenario is a concrete test case consisting of steps. Recommended 3‑5 steps per scenario to keep it readable.
Initialize context
Describe the process
Describe the expected outcome
Given
Describes preconditions such as data preparation or environment setup.
When
Describes an event or action.
Then
Describes the expected result or assertion.
Background
Executed before every scenario in the feature file; typically used for common initialization (e.g., clearing browser cache, uninstalling previous app).
Scenario Outline
Used when many scenarios share the same steps but differ in input/output values.
Scenario Outline: login example
Given open
login page
When input username
And input password
, click login
Then successfully enter
page
Examples:
| page | username | password | result |
| 奇虎 | test1 | 123456 | 奇虎欢迎你 |
| 百度 | test2 | 123456 | 百度欢迎你 |Step Implementation
Each scenario step must have a corresponding Java method annotated with the matching Gherkin pattern.
# Scenario description
Scenario: This is a testcase
Given I have 10 dollor in my wallet @Given("I have (\\d+) dollor in my wallet")
public void I_have_dollor_in_my_wallet(int money){
System.out.println(String.format("money: %n", money));
}Parameters can be captured as integers, strings, booleans, lists, or data tables using regular expressions.
@Given("I have \"([^\"]*)\" dollor in my wallet")
public void I_have_dollor_in_my_wallet(String money){
// implementation
} @Then("^返回检查结果为 (true|false)$")
public void verify(boolean expected){
// implementation
}Tag Usage
Tags allow selective execution of scenarios, e.g., by ad platform, device manufacturer, or ad type.
@360
Feature: this is a feature
# detailed description
@native
Scenario: just a demo
Given the cow weighs 450 kg
When we calculate the feeding requirements
@video
Scenario: another demo
Given the cow weighs 450 kg
When we calculate the feeding requirementsRun only native ads:
java cucumber.api.cli.Main --tags @native your_featuresRun everything except native ads:
java cucumber.api.cli.Main --tags ~@native your_featuresRunning Tests by Line Number or Scenario Name
Execute specific lines:
java cucumber.api.cli.Main E:/codes/cucumber/demo/src/test/features/first_example.feature:12:21Execute by scenario name:
java cucumber.api.cli.Main --name "这是一个原生广告请求有table的例子" featuresInstallation and Examples
Method 1: Maven Project
Initialize environment (install Maven, create directory, run mvn archetype:generate ).
Add Cucumber‑JVM dependencies to pom.xml .
Execute feature files using command line:
# Simple command
java -cp "./jars/*;." cucumber.api.cli.Main -d path/features/test.feature
# Pretty output
java -cp "./jars/*;." cucumber.api.cli.Main -p pretty features -g step_definitions
# HTML report
java -cp "./jars/*;." cucumber.api.cli.Main -p html:output features -g step_definitionsMethod 2: IDEA Project
Install IDEA, configure Java and Maven, install the Cucumber‑JVM plugin, create a .feature file (keywords are highlighted), add step definition classes, and run via IDEA configuration.
Reference: https://skyao.gitbooks.io/learning-cucumber/content/practice/pass_parameters.html https://docs.cucumber.io/
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.