Your first Android Kotlin test

Diego Torres Milano
4 min readJun 16, 2017

--

The best part, you don’t have to know Kotlin as the test will be automatically generated for you.

Recently, Kotlin was made an official language on Android.

It’s very easy to get started using Kotlin as it works side by side with Java and C++ on Android.
However, there are new syntax and new language features to learn, even more, when we talk about testing and creating tests for your application UI.
You can gradually introduce Kotlin code to your project as it can coexist with existing code and Android libraries.

So, what’s better than start incrementally adding Kotlin to your workflow starting by the tests?
And what is even better is that CulebraTester v0.9.x introduces Kotlin code generation as can be seen in the previous screenshot.
You can start learning Kotlin now by taking a look at the generated code.

Getting started

The first thing you have to do is to install CulebraTester.

As it is now in Open Beta you have to follow the Opt-in URL on Play store. For reasons too long to explain in this article, it is composed of two APK’s, the main application and its instrumentation. You have to install both. However, if when you start the main application it detects the instrumentation is not installed you will have the chance to be redirected to Play touching the corresponding button.

Ready, set, go…

Connect your device to ADB, using USB or IP. We need this to start the instrumentation as I’m showing you in a moment.

Your device should appear when you run

$ adb devices
List of devices attached
00b0e6ef101c1234 device

Start CulebraTester and you will find the URL to connect to from your desktop browser

Starting CulebraTester application

Open this URL in your browser and, as the instrumentation is not yet running, you will be prompted to copy and paste the command that will start it

Instrumentation is not running

The command provided will look something like this

curl -s http://192.168.2.250:9999/Assets/runinstrumentation.sh | bash

Copy and paste it into your terminal as a single line. When run, it will start the instrumentation, causing CulebraTester Activity to exit, so you will see it disappear from your device’s screen, although it will continue running in the background as a Service.

com.dtmilano.android.uiautomatorhelper.UiAutomatorHelper:

this will remain on your terminal for the duration of the session.

Press DONE and you can start interacting with your device.

Select Kotlin as the Code Generation language, and under the SOURCE CODE tab press START to begin the code generation.

Interact with the device representation and when done press STOP to finish. The generated code will appear in the SOURCE CODE tab.

In my case, I launched and interacted a bit with Calculator to create a very simple test. After Calculator starts, the screen is cleared, in case there is previous content, by long pressing CLR. Always remember that a good test should be independent of the previous state. Then, it continues by entering 3+1=, and verifies the result is 4.

Here is the generated Kotlin code that you can SAVE or copy and paste to your Android Studio 3.0 project in the androidTest directory, adapt the package name and run.

You will see the same actions reproduced on the connected device and finally if everything goes well the green bar shows up and the test passed message appears.

That’s it. Your first Kotlin test even before you know how to do it.
Hope this help you get started. More detailed articles are coming.

--

--

Diego Torres Milano

Geek, Android System Engineer, Linux advocate and published author