Unit Testing for Android Application Logic

Wanting to write some tests for my application, I came across Android testing pages . After quite a long reading, it quickly became apparent that the only thing I could break out of it was information on how to test the interface / actions. I really want to test my logic just by ant test , preferably even without a device. At this point, I should mention that I am not using Eclipse, and it’s pretty sad that 99% of the Java resources on Android suggest that people do this.

In any case, trying to achieve anything at all, I played with the textbook as much as I could. He asks that the tests directory is at the same level as src . Of course, even if each of their pages assumes that the test project is a completely separate object. While in the top-level project directory, I ran android create test-project -m /path/to/my/project/ -n MyProjectTest -p tests . It is worth noting that they are very inconsistent with the way they want things to be set up as can be seen on this issue . When visiting the directory, I see the default test file. Here is where the problems begin.

As far as I understand, testing is performed as follows: build application, install; go for tests, build tests, install; run tests from the test catalog with ant test or run them directly with adb shell am instrument . This works great. However, I have no desire to test activity, but only logic (which does not have access to any representations / actions).

Changing the default test for the AndroidTestClass extension seems to have worked for a while. The tests ran, but there were caveats: cleaning the tests with ant clean also cleared the project directory ( ../tests ), so it took forever to create tests in a clean environment (which is necessary because ant debug seems awful to detect changes), but it worked and I was happy.

A little later, I get java.lang.VerifyError only in my test class. By breaking down on Google and folding around, it came down to something wrong with external libraries or something wrong with my course. I do not use external .jar , so it is probably my way.

Anyway, here's my question: what is the logic of the “Right Way” to unit test in Android apps with JUnit? I can not find any resources about this: all resources are intended either for testing parts of the user interface, or for unit testing of ordinary applications.

How can I unit test use only my logic? You do not even need to start the device for this, given that I do not need to use any parts of Android. Where can I post tests? What do I need to change to run ant test in my project directory to run them?

+4
source share
2 answers

A long mess later, and I succeeded.

First of all, I ignored creating any project using adb .

Imagine my package is called com.foo.bar . I created tests in src/com/foo/bar/tests . There, write your regular JUnit tests. Make sure you put package com.foo.bar.tests in your test classes. Here is an example class:

 package com.foo.bar.tests; import com.foo.bar.baz.Foo; import org.junit.*; import junit.framework.*; import static org.junit.Assert.*; public class MyTests extends TestCase { public void testSomething() { Foo testFoo= new Foo(); assertEqual(testFoo.getBar(), 1); } } 

Next will be a test with ant test . To do this, add the following to your AndroidManifest.xml immediately before the last closing bracket ( </manifest> ):

 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.foo.bar" android:label="Tests for com.foo.bar"/> 

You also need

 <uses-library android:name="android.test.runner" /> 

added to the <application> section.

I can’t understand what needs to be changed in order to test locally and not on the device, but this is a 75% answer and hopefully will be useful for someone like me in the future.

EDIT: You can simply hide the original ant test behavior by adding the following last import to build.xml below:

  <target name="test" > <junit> <!-- all your junit stuff here --> </junit> </target> 
+2
source

Check out Robolectric - it avoids much of Google’s nightmare test solution.

Good Square article on Resurrecting Testing for Android .

+1
source

All Articles