Unit tests in Android Studio 0.8 - android

BREAKING NEWS: Android Studio 1.1.0 now has built-in support for unit testing. See more here: https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support
At first I know that there are very, very, very much similar questions (even here on SO). Most of them are terribly outdated so I'm asking a fresh new question which should be up to date for Android Studio 0.8.x.
I know there are some libraries which work somehow, but in almost each case I had no evidence that the information were up to date. I know there is e.g. roboelectric, there are also some deprecated Jake Wharton like gradle-android-test-plugin or double-espresso, I also found the library RoboSpock and Deckard. But none of them seems to have any Android Studio integration.
After long reseach I found this two bugs in all implementations:
The classpath is broken and needs to been hacked to get junit running
The junit tests cannot been debugged
What I'm looking for:
I want to automatically test an algorithm (which is plain java)
I want to check the integration in my Android app works
I want an integration in Android Studio so that I can jump into the code out of a stacktrace
I want to step thrue the test code
I found also somewhere a nice hack which run the tests in gradle as an external task and pushed the results into AS so that the results could been displayed, but unfortunately I cannot find that link again (and if I remember correctly it did also not work for me).
Do you guys have some fresh references? Does it work for you?

You have to edit the .iml file that Android Studio generates to point to your test-classes directory and also to fix the Stub error from Junit. There is currently no work around for this.
Gradlectric is a sample that uses the Robolectric Gradle Plugin to run unit tests in Android Studio.

Here you miss one example project setup https://github.com/nenick/android-gradle-template
But none of them seems to have any Android Studio integration.
This project is maintained for android studio
The classpath is broken and needs to been hacked to get junit running
gradle scripts will fix the classpath
The junit tests cannot been debugged
tests a running inside AS so they are easy to debug

This project of mine does not cover all your requests, but a fair lot of them.
It covers a default Java test case, and some tests interacting with Views.
The root project uses Travis CI, which uses gradle connectedCheck to run the tests. To be able to debug the tests, you can just create a test run configuration in AS and run it in debugging mode.
Note that this does not use any special Android testing frameworks like robolectric.

You can find a fork of Jake Wharton's gradle-android-test-plugin here. This is compatible with AS 0.6 and is regularly maintained by Robolectric. You can look at this project to set up Robolectric in Android Studio with help of this plugin. You can achieve fair bit of functionality you mentioned with the help of robolectric-gradle-plugin. You can also successfully debug your test code using AS.

Related

Android: Code coverage tool using Android Studio

I have written test cases for iOS and there we have
XcodeCoverage
for finding out the code coverage. Now, I wish to do same for my android test cases. Is there any tool compatible with Android Studio itself?Any suggestions/ reference links would be helpful.
using Android Studio 1.4 (and probably 1.1+), you have the ability to get the coverage while running your tests without additional plugins.
If you can't see the Run 'All Tests' with coverage button, you can find out why here.
You can use JaCoCo plugin, which is fairly new, but in general does the job more than enough. You can get more details about it here : Android test code coverage with JaCoCo Gradle plugin

How to run unit tests with IDEA+Gradle+Android

I've written some unit tests in my Android projects with Robolectric, and some of them even don't use Android.
I've tried first this: robolectric-gradle-plugin, following this template.
The result is that I've faced this bug in IDEA, that I reported myself.
Then I've tried another possibility: the gradle-android-test-plugin, which separates tests in another submodule, that is a regular Java project. I've tried to follow this other template. I've open this bug report.
Then I've faced again with other bug.
What recommendations can you provide me, that does work?
Finally I've had to upgrade IDEA to 14 EAP, and know it works ok.
gradle-android-test-plugin seems to work better than robolectric plugin, as I can run JUnit and ScalaTest tests easily. I've had to install with a script, the non-published com.novoda:gradle-android-test-plugin:0.9.9-SNAPSHOT.
It's a hell the combination of versions of:
Android Build Tools
Gradle
com.android.tools.build:gradle
IDEA
robolectric
robolectric plugin
....
Any change in one of them, can make your project fail.
Luckily, by using Gradle you can fix the versions of every component, and do Continous integration.

Best way to run fast JUnit tests in Android project in Android Studio

I have some plain old Java classes and simple JUnit tests in my Android Studio project. They currently live inside my Android Module. If I run them as Android tests they pass fine, but they're really slow as it requires launching or connecting to the emulator.
If I try to run one as a JUnit test, I hit the !!! JUnit version 3.8 or later expected JUnit version problem. This answer explains how to workaround that issue. Unfortunately, I then hit the java.lang.NoClassDefFoundError: junit/textui/ResultPrinter, which I can't figure out how to fix.
I then tried to move my JUnit tests into a separate Java module that depends on the Android Module. Everything compiles fine. I added a new configuration to launch the JUnit tests (:MyJUnitTests:test), but the tests fail with package com.myproject.util does not exist. It looks like maybe the classpath isn't including the classes from the dependent Android Module.
Does anybody know how to fix this classpath issue? I've looked at lots of related answers and none of them seem to work for me. Or is it a just a bad idea to try and keep my JUnit tests in their own Module?
I can get the JUnit tests to run fast and pass if I move my plain Java classes and their JUnit tests into a completely separate Module (e.g. here), and reverse the dependency so the Android Module depends on the Java Module. Is that the preferred solution?
The basic problem is that Android Framework classes don't work well outside the context of Android OS, so any code that has dependencies on Framework classes doesn't run in a regular JUnit environment, as you've found.
Your solution of trying to move the JUnit tests into a separate module won't work because you can't have a plain Java module depend on an Android module. Android Gradle modules don't act like Java modules, because Android builds are much more complex, and because the end result of an Android module build will be an APK or an AAR, which other module types won't understand.
If you can move your plain Java classes and unit tests into a plain Java module and have the Android modules depend on that, it will be the best approach that will get the most support from official features in the IDE. It will also have an architectural benefit in that it will keep those classes free from Android abstractions, making it more portable, and enforcing a separation of business logic from UI or storage or other things more device-specific.
If that's difficult for you to do, then a lot of developers in your shoes go with Robolectric, which is a test harness allowing code depending on many parts of the Android Framework to run in a JUnit environment without an emulator or device. It's not officially supported by Google, but Google is aware that it's widely used and tries hard to not wantonly break it.

Maven Android Project with Test Suite

I'm trying to set up a project with dependencies on ActionBarSherlock (4.0 RC1) and ViewPagerIndicator, I'm currently using Eclipse IDE, and I would like to be able to build and run an Android test project for this project. I had tell Eclipse to ignore the proguard and generate-sources goals to get it to stop giving me errors, it seems that was the solution for it, but I'm having a hard time confirming if that is still the correct solution. I've had moderate success with ABS thus far, I was able to deploy a package to the emulator that had an action bar, and Eclipse seems to know about the classes and resources in ABS. However, it doesn't know about the themes (like Theme.Sherlock.Light) and displays an error. I'm okay with that, except it means I can't run using Eclipse. But it doesn't seem to work for ViewPagerIndicator. And I keep running into random errors and it feels like I'm just running around in a circle.
So I tried adding a test project (again..), so that I can test the project as I build it, to make sure it meets all my specifications at each step. But I can't seem to get that to work. If I could just get these 3 things and how to run the tests I should be able to debug other problems that are arising, but as it is now I can't even confirm where the problems lie. Most of my researching has yielded partial answers and out-of-date solutions.
Using:
ActionBarSherlock 4.0 RC1
ViewPagerIndicator 2.2.3
android-maven-plugin 3.1.1.99.0.6
Apache Maven 3.0.3
Eclipse Indigo
m2e 1.0.100.20110804-1717
I also posted this question on the maven-android-plugin Google group, and Manfred Moser pointed me to Gaug.es app for Android. It has a set up for building the app with Maven, and has dependencies for ABS, ViewPagerIndicator, RoboGuice, Robotium, and a few others. It is set up to do unit tests with surefire and integration tests with instrumentation.
He also mentioned that he uses IntelliJ IDEA, which I thought I would try for myself (since Eclipse was giving me issues and TextMate is not even an IDE). The Community Edition has been serving well so far,it provides nicer (or easier to understand) integration with ant and maven, so I can specify my "clean package android:deploy" run configuration, for example.
If you're having similar issues, I would recommend taking a look at Gaug.es, it can be a little overbearing and do more than you want or should start off with. I can post the details of what I gathered from Gaug.es for my own app if anyone else is curious, but I thought I would note that my question is currently resolved, and how it was resolved.

Getting code colored with emma in Android project

I have code coverage with Emma for my Android projects and I get a nice html-file. But i kind of think an html-file isn't really good enough. I want to see covered and not covered code in the editor.
Is there a way to use the .em or .ec files are genereated to get Eclipse to color the code?
Thanks in advance
Roland
EclEmma the eclipse plugin gives you colored code coverage.
You just run your tests using this button:
And you get in Eclipse output like this:
Oh and I run my unit tests with Robolectric that is Android but doesn't use Dalvik, uses your native JVM
Perhaps this what you are looking for EclEmma, however it looks like it is now using JaCoCo for code coverage metrics rather than Emma perhaps there is an older version of the plug-in that still works.
I suppose it depends on what you want code coverage from Emma, or visualization in Eclipse.
Try comparing the outputs of JaCoCo and Emma, I would have thought they should give similar if not identical results, and then choose.
Does JaCoCo work against the android platform - I can't say.
According to the ECLEmma team, on http://www.eclemma.org/devdoc/eclemma20.html:
Future support for EMMA and EclEmma 1.x Maintenance
From EclEmma 2.0 on EMMA will not be supported any more as a coverage
engine. ...
Beside this we will try to maintain the EMMA based 1.x stream on a
best effort base but with no functional enhancements planned. There
will be a separate download for the 1.x versions.
Per my comment below, ECLEmma 2.0 uses JaCoCo which is currently incompatible with Android due to not supporting off-line instrumentation that can be processed into Dalvik. To use Emma with Eclipse, you need ECLEmma 1.0, so see here: http://www.eclemma.org/installation1x.html

Categories

Resources