Software development involves process and people. The people include both technical and nontechnical stakeholders, but because the process is primarily technical, it can create a large gap between technical and nontechnical stakeholders.
Bridging this gap requires a collaborative approach that uses natural language to encourage communication and collaboration between technical and nontechnical stakeholders. This is precisely what behavior-driven development (BDD) aims for: to bring a point of understanding among developers, testers and business stakeholders.
As a software engineer, I’ve been implementing BDD using one of the most popular tools, Cucumber. Cucumber helps business and technical teams work together by collaborating on executable specifications. The BDD specifications also double as automated tests. Using the Gherkin framework, these specifications are written collaboratively, aligning the team with living documentation of your system’s work.
In this article, I will explain five simple steps for integrating Cucumber testing into your Android application.
Why Use Cucumber For UI Testing
- It supports various programming languages, including all Java Virtual Machine (JVM) languages.
- It integrates seamlessly with the Espresso framework for user interface (UI) testing.
- It enables anyone to write plain-text descriptions of the desired behavior in any spoken language and run automated tests using these descriptions. Its plain-language parser, Gherkin, facilitates this, as it specifies expected software behaviors in a clear and logical language understandable to customers, stakeholders, managers, developers, quality assurance (QA) testers and others.
- It provides excellent documentation about your application.
- It can run automated acceptance tests using BDD.
Set Up Android Studio for Testing With Cucumber
Let’s dive into setting up Android Studio for Cucumber Tests.
Prerequisites
Before you start, make sure you have the Android Studio integrated development environment (IDE) installed.
You can also consider installing the following plug-ins from the Android Studio marketplace:
- Cucumber for Kotlin and Android: This plug-in from Finanteq enables Cucumber support with step definitions written in Kotlin. It allows running Cucumber scenarios as Android instrumented tests directly from the IDE.
- Gherkin and Cucumber for Java by JetBrains: These add support for the Gherkin language, which the Cucumber testing tool uses, and provides coding assistance for step definitions.
1. Create an Android Studio Project With Dependencies
Create a new Android Studio Project in your IDE, or use an existing one. Next, add the Cucumber dependencies.
In your app-level module build.gradle
file, add the following dependencies:
2. Create Your Instrumentation Runner
In app/src/androidTest/java/com/your/app/
, create a custom instrumentation runner named CucumberTestInstrumentation.java
. Add this class to your build.gradle
under android
> defaultConfig
:
You have successfully set up Android Studio for Cucumber, so you can now proceed to the exciting part.
3. Given, When, And and Then
Gherkin is a domain-specific language that describes the implementation of a feature step by step using nontechnical terms. It uses the keywords Given
, When
, And
and Then
to explain the steps. These steps can be written in any human-spoken language, such as English, Arabic or Luo.
Here is an example Gherkin feature scenario written in English that I will use in this project:
Create an assets
directory in app/src/androidTest/assets
and add a folder called features
. This is where you will add your feature file containing the above step definitions written in English.
Add a new .feature
file called login.feature
and add the feature steps above.
4. Implement the Scenario Steps Using the Espresso Framework
In app/src/androidTest/java/com/your/app/
, create a Kotlin class called LoginSteps
. This is where you will write your tests to implement the steps in login.feature
.
Here is a code snippet of the implementation of the steps:
5. Provide Cucumber Options
Before running your tests, you must provide the packages containing the step definitions and glue them to your steps.
In app/src/androidTest/java/com/your/app
, create a folder called test
and add a new Kotlin class.
Finally, you can run your test, but first, confirm your project structure looks like this:
Run Tests
To run your tests:
- Open Edit Configuration.
- Click + on the left panel and select
Android Instrumented Tests
. - Write the name to match the feature’s name, so it’s easy to remember. In this case, that’s
Ability of the customer to login
. Then click Run or OK to run or debug it later from the IDE toolbar.
Here is the result of the implementation above.
Conclusion
Bridging the gap between technical and nontechnical stakeholders is crucial for effective software development. Behavior-driven development facilitates collaboration and communication in natural language.
Tools like Cucumber help implement BDD using executable specifications as automated tests, allowing teams to create clear, shared documentation. Integrating Cucumber into your Android application development can enhance team alignment and streamline development.
For more insight, access my Hechio BDD reference project or the cucumber/cucumber-android project, which provides Android support for Cucumber-JVM.
The post Implement Behavior-Driven Development in Android With Cucumber appeared first on The New Stack.
Leave a Reply