Should I use Android Instrumentation to write unit test? - android

I have read some articles about unit testing in Android including the official documentations and wrote some demos, it is complicated and slow because it must run on a device or emulator. Should I continue using it or find another option?

Usually a good test should run on the target device. Thats why my answer is: yes you should continue using it. If you test on an emulator or something like that there could be some little differences that would cause some different behaviour than on the real device (espacially if you use some hardware components).
If you use a real device there is also the side benefit that the test runs faster than on the emulator.

You're looking for Robolectric.
Robolectric is a unit test framework that de-fangs the Android SDK jar so you can test-drive the development of your Android app. Tests run inside the JVM on your workstation in seconds.
It is not complete yet (e.g. Support Library Action Bar is not supported) but it will save you a lot of time anyway. Besides you can add what's missing by developing shadows yourself.

My alternative looks like this:
I have refactored my android app into
app (gui using the libs),
android-independant-lib (service-interfaces, dto-s, businesslogic without database bindings)
android-dependent-lib (android specific implementation of the of the service-interfaces and database binding)
for the android-independant-lib i have created "normal" junit-tests
for the android-specific parts i currently have no automatted tests.

Related

Android unit test case automation: Robolectric library vs Android Testing framework

Wondering which one is the better choice to write unit test cases for android apps and libraries: Using Robolectric library or sticking with Android Testing framework. I want to run test suite at commandline and want it be independent of need of configuring emulator or letting a device attached with build machine. Does anyone of you run a comparative analysis on both of these or something better? Your experiences will be great help me to decide on the better solution.
I use a tiered system, where I prefer earlier tiers where possible:
Pure unit tests. I try to make as much code as possible fully independent of Android APIs, and then use "pure" unit tests which can run on any JVM. These tests are the fastest, and it helps keep code that has no need to be Android-specific portable.
Robolectric-supported unit tests. Where my code has only small dependencies on Android APIs, that can be satisfied by Robolectric shadows, I test it with Robolectric. There is a little more setup time for Robolectric compared to pure tests, but it's still faster than starting/running on an emulator.
Android framework tests. Where Robolectric doesn't cut it - either because the shadows don't exist, or because I'm heavily using Android APIs (and therefore want to test against the Real Thing) - I write test that run on the emulator/device with the default framework.
The point of the tiers is to keep things as simple as possible, which keeps the full suite faster and helps promote cleaner code.
I worked on both, what i found is :-
1) Robolectric do not support API 19, it's mention in its document -
http://robolectric.org/eclipse-quick-start/. It's a great
disadvantage of it.
2) Robolectric run on JVM not on DVM. So we can not
detect that on that particular time GPS is enable in device or not
etc. We can only pass our pre-decided value for it.
3) Code writing in Robolectric is complex than junit specially for
fragment there are lot of complexity and issues.
4) Robolectric needs external jar and configuration and for junit test
we do not need any external library.
5) Robolectric is faster because it runs on JVM but this have
disadvantage too, we can not see UI on our device, what screen code
is executing.
For Android, i like jUnit test.

Test Driven Development For Android

Can we use JUnit for test driven development in Android ? If not, is there an alternative similar to JUnit ?
I searched a bit on google and also read a SO post Android Test Driven Development
It looks like Android was never made with TDD in mind. I wanted to be sure before I begin learning TDD and doing Android development at the same time.
I think you can completely rely on Robolectric for running your tests in JVM. You get to use JUnit4 for testing your POJOs and Robolectric provides you the support for testing the Android components.
I am also a beginner in TDD for Android Development. I find Robolectric really useful for test driving my code.
This video will tell you almost everything it provides you for unit testing the Android code.
UPDATE:
With the Android studio support and the new Android ecosystem, now unit testing can be done as a first class practice. Refer http://developer.android.com/training/testing/unit-testing/local-unit-tests.html for more details.
There are couple of good approaches to test drive the android code. The most effective ones I have found so far is using MVVM(model-view-viewmodel) or MVP(model-view-presenter) approach, where the business logic as well as presentation logic is decoupled from the view and can be easily be unit tested.
Yeah we can use JUnit for test driven development. To initiate with you can refer to following
link : http://developer.android.com/tools/testing/testing_android.html#JUnit
Following the documentation we can use the junit.framework to have unit testing done.
Here is a bit of explanation of the problem space: http://www.techwell.com/2013/04/applying-test-driven-development-android-development
The conclusion is you should use Robolectric. Unfortunately Robolectric is slow, and if you follow TDD even on a pragmatic level, you will end up with a couple of hundreds of test, that will run for couple of 10 seconds. And that is not you want to do with TDD. TDD test package should run at most in seconds.
My advise is:
Create wrapper classes around Android classes that simply call the Android class.
Write your app logic in pure Java.
Use Junit (or TestNG or whatever pleases you) to test you model/business logic
Use occasionally Robolectric for the wrapper classes (probably you won't have to use)
(You can write integration tests that use multiple classes, Robolectric, etc. but only run it on a separate Continous Integration server eg. hourly)
With this approach your logic will be more portable as well.

Testing android applications

I'm new to testing.I've developed a application,now i need to test.I googled about testing for some time,learnt ,what different types of testing are there in general.I wrote few test cases.
Three things,i would like to know,
Is there any different types of testing for android,if yes,can you give me some links which could help me to refer.
How do generally a android user test his apps,Will he uses test frame works or generally write test cases and testing that on real phone to see how they are performing.
Is there any sample test cases written document which will give me some basic idea.
For integration testing I use Robotium. It is a nice convenient layer on top of the build in instrumentation testing. These Tests need to be running in an emulator or on a real device. It is recommended in both cases to have an extra test project (producing an additional APK) that depends on the project under test.
Personally I like to partition my app so I have one or more libraries that do not depend on Android specific classes and can therefore be tested in a regular JVM using JUnit.
There is a third way to test and that is by mocking the android classes and have the tests run in a JVM. I have not yet used it but I hear Roboelectric is a framework that allows for this kind of testing.
Android Monkey tool can be a handy little tool. I find it handy the pseudo random fashion is handy for generating unusual use cases.
http://developer.android.com/guide/developing/tools/monkey.html

Is there any frameworks for integration testing of Android apps which can drive emulator/device beyond one app

I found couple of difference frameworks which can test Android apps, but all of them are limited on testing of just one app (because mainly they use Instrumentation)
My app contains a service which could be called by other apps and I want to automate testing of this too. So, I would like to be able to write some tests which automate UI in other apps.
Have you seen anything, except MonkeyRunner? I looked at it, but the API is quite poor.
Take a look at Sikuli IDE it's easy enough to use and is based on Python.
You can bascially run integration tests using it, (kind of like Selenium for desktops).
There is also Selenium Android Driver if you want to run automaton tests from the Android WebView!
You are right, a bare monkeyrunner is perhaps not enough, but if you combine it with other tools perhaps you can find your way. For example, AndroidViewClient is a library that adds the ability of:
finding Views by ID, very much like you normally do in your Android Activity (using ViewClient.findViewById())
obtaining the value for almost any of the Views properties (using for example View.isChecked() or View.mText())
sending touch events to the Views by simply invoking View.touch()
More details an a complete example can be found at http://dtmilano.blogspot.com/2012/02/monkeyrunner-interacting-with-views.html

BDD Android UI testing framework?

I'm looking at using Frank for UI testing our iOS application(s). Is there a similar library with support for Android? I'm currently using Robotium, but thought it would be nice to be able to specify tests in the same format across both platforms.
Would it be possible to get something like JBehave to run on Android?
As scompt.com said. The project is now officially open sourced and is called Calabash.
I wrote a short blog post about it:
http://blog.lesspainful.com/2012/03/07/Calabash-Android/
Calabash is also available on iOS and with a bit of work and two similar apps you can run the same feature on iOS and Android.
You can read more general stuff about Calabash here:
http://blog.lesspainful.com/2012/03/07/Calabash/
The guys from lesspainful.com have open sourced some of their special sauce on github. It lets you do cucumber testing on android in the emulator and local devices. Because it's the same code that they're running for their service, you should be able to test your features on multiple devices if you sign up.
Could you use Cuke4Duke to drive Robotium? Or use JRuby/Cucumber to drive Robotium?
No, robotium is a BDD on dvm not jvm thus no driver exists with jvm frameworks such as jbehave, etc
I've recently started using www.LessPainful.com - there are some limitations (you can't write your own steps) and it's quite an early stage company, but so far the testing has at least proved useful, especially with different screen sizes, plus they've automated some good things like orientation (I think they use motors on real phones!).
(for reference on iOS we're using Frank - https://github.com/moredip/frank- and on WP7 we've written our own - https://github.com/Expensify/WindowsPhoneTestFramework)
I've been able to open-source my work in this area:
https://bitbucket.org/proxama/windows_android_cucumber_runner
https://bitbucket.org/proxama/android_cucumdroid
It allows you to write your own features and steps to exercise the UI automator. This means it runs all on the device.
It's probably not as easy to use as it could be but the Windows gui app is designed to wrap the ANT tasks that make it actually run.
I have tested my app using MonkeyTalk. It may help you. https://www.cloudmonkeymobile.com/monkeytalk

Categories

Resources