TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Frontend Development / Software Testing

Implement Behavior-Driven Development in Android With Cucumber

By using human language, behavior-driven development (BDD) bridges the gaps between technical and nontechnical people in software development.
Nov 8th, 2024 7:32am by
Featued image for: Implement Behavior-Driven Development in Android With Cucumber
Featured image by Anna Jakutajc-Wojtalik for Unsplash+.

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

  1. It supports various programming languages, including all Java Virtual Machine (JVM) languages.
  2. It integrates seamlessly with the Espresso framework for user interface (UI) testing.
  3. 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.
  4. It provides excellent documentation about your application.
  5. 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:

Example project structure

Run Tests

To run your tests:

  1. Open Edit Configuration.
  2. Click + on the left panel and select Android Instrumented Tests.
  3. 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.

Run/Debug configurations UI

Here is the result of the implementation above.

Result of Run/Debug UI

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.

Want to dive deeper into Android development? Check out Andela’s tutorial (also written by Stephen Henry) on Android ML face detection with Camera X.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.