Skip to content
loading...

Android testing help

github logo ・1 min read  

last week I wrote this post:

the advice was very useful and now almost all my repos have test on the most essential components, with one exception Ara-android I have been try to write unit test in that repo, but I am having trouble passing the Activity value to functions via the test, any tips?

twitter logo DISCUSS (4)
markdown guide
 

@fultonbrowne Sorry for the bad news, but testing on Android is hard

Part of the answer is that the mockK library allows you to mock things

GitHub logo mockk / mockk

mocking library for Kotlin

But the real problem is that the Android SDK was never designed to allows you to unit test easily. So you have the so-called Burito Design Pattern where you business logic is inside Activity or Fragment

And basically as soon as your business logic lives inside an Android component, you are basically fucked up for writing unit tests.

Google pushes you instead to write UX tests, which also sucks because they are hard to write, slow and flaky and usually tell you little when they fail.

Google Testing Blog: Just say NO to more end to end tests

So the real solution in my opinion is to extract your business logic outside of Android components

To this I use the Functional Core - Imperative Shell approach

More links here
gist.github.com/kbilsted/abdc01785...

 

thanks for the advice! your post is going to be very usefully :}

 

Jean-Michel is right : Android Test is difficult and you have to extract your business logic outside of Android to not be too coupled with the framework.

To complete, I suggest you to check this Google Codelabs : it covers the basics of running and writing tests for Android. You can do the next two Codelabs after that to complete what you learned in the first one (this is all about testing, DI, mock, ...).

Thanks I have extracted as much logic outside the activity as possible but one of the main components of my app NEEDS activity context. completely testing this part will get me to around 65% code coverage (and my project is BIG). I cant find a way to pass the an activity to the method, any tips?

Classic DEV Post from Jun 12 '19

What are your favorite Linux utility/productivity tools?

I love Linux and one thing I love about it is that you can set it up just right for yourself to make...

FultonB profile image
I am an full stack developer trying to change the world with code.