BDD Cucumber Android With Espresso

sam nursanto
TelkomDev
Published in
3 min readAug 10, 2022
BDD Cucumber Espresso

In my previous article, we have talked about BDD and how to create feature file, the question is how that file can guide us as a android developer, let’s discuss that, BTW if you need full example for this project you can see here.

Setting up your project 🖥

First of course you have to create an empty project in android studio, then you have to add plugin Gherkin and cucumber for java those two plugins use for coding assistance for step definitions and why i use cucumber for java even when i write my code with kotlin, because that dedicated run/debug configuration. After that, don’t forget to restart your android studio.

Next, create an assets directory in androidTest and add one more directory in it, called features, this directory use for add your .features file. In your androidTest/java/com.<domain>.<app> add a new package called tests this package will be your directory for instrument test, your android file explorer will look like below.

android studio file explorer

Oke after you set your directory, now you have to add code below in your app/build.gradle

app/build.gradle

testInstrumentationRunner use for run the instument test, you have to change the package up to your package name, don’t forget to add .tests because we have made the package before for the purpose of test and the .ExampleAppAndroidTestRunner is a class that we will create later, the sourceSets need to declared a path for UI tests directory (cucumber feature), and don’t forget to add cucumber library, last for setting up this all let’s create a class .ExampleAppAndroidTestRunner in androidTest/java/com.<domain>.<app>.testsdirectory like below.

Import feature file and implement step definition

Move your .features file to androidTest/assets/features, you can use the example here. If you open the Login.feature, you can see yellow mark in all steps, it will be disappear when we implement the steps, so let’s create a class called LoginSteps in androidTest/java/com.<domain>.<app>.tests like below, if you never know about espresso, maybe this article will have you.

LoginSteps.kt

In the above example we didn’t initialize the view id, that’s because we haven’t created the view yet, so let’s create a view, after that before we continue to write the logic code, this is the right time to talk about TDD.

TDD (Test Driven Development)

I don’t want to demonstration the unit test here, because the TDD is not about testing, but start thinking about the behavior of your class and how to interact with it, until here i think you understand the connection between BDD and TDD where BDD is about ensuring that functionality works as the user expects, as you know we already have a scenario and example in the .features file it can make it easier to do TDD.

BDD & TDD — source

Run the test

Oke let’s say you have create view and logic and you have test your functions so it’s running well, last don’t forget to add view id in our steps class, then you can run your instrument test by right click the arrow like below, the choose run .

run instrument test

Result ☺️

Thank you for reading this article to the end, if you like this article you can give me a clap 👏, and if you have any question we can discuss in the response section, thank you 👍

Result instumentation test

--

--

sam nursanto
TelkomDev

Developer android, who curious about all tech