Kotlin Integration Testing using Gherkin for KMP and KMM

android speaks gherkin

To start with, everyone nowadays are trying to go “Multi Platform”, with a separation between the view layer and the logic layer being more and more expanded.

This causes some concerns when testing stuff. using a cross-platform brings a unique challenge. How can anyone be sure that the logic layer is working correctly across all these platforms?

As a proposal, I think Gherkin can provide a solution to this problem. in the following is a proposal on how to architecture the application and do integration testing using mockito, JUnit, Cucumber without touching the view model of the app.

Enabling the support

to enable fast and good support we need 2 plugins. the first is Gherkin and the second is Cucumber for kotlin. You can enable those from the plugins directory in Android Studio.

Adding folders for features

First, flip the view into the project mode.

We will create our tests in the test directory in our sources. to make the separation clear we shall use a folder named cucumber while the unit test is in unit folder and finally, and helper stuff shall live in the utils folder.

next we shall create an assets folder in src/test, and inside we shall create a folder named features to hold our scenarios.

Adding the configs and source sets

Now if we started writing a scenario we will get an undefined step error. to link the feature with the implementation we should add a custom sourceset in the gradle src/app/build.gradle like:

Notice we have here the test directory, this is because we want to test on JVM not an emulator.

Next we should add the cucumber dependencies, they are as of now

Notice again that these are test decencies not an androidTest ones.

Writing tests

I will create a test to test our test pipeline 😆

And the steps file is

When running this, the result is:

Looks good to me!

Happy testing.

Software Developer at Yes-Soft.de

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store