Android unit testing

  1.  Android unit testing  
  2.  More_about_testing  

 Android unit testing

see Android Hello World

Android tests example

  • See android tests example in our geo2tag project on Github
    • playground/AndroidHelloTests/
bac1ca@bac1ca:~/programming/repo/geo2tag/playground/AndroidHelloTests$ ls -1

Example classes

  • class MyAndroidAppActivity in MyAndroidAppActivity/com/example/myandroid
public class MyAndroidAppActivity extends Activity{
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    public static final String TITLE = "Test title...";
  • test class MyAndroidAppActivityTest in MyAndroidAppActivityTest/com/example/myandroid/test
    • we test the existence of MyAndroidAppActivity and the title of MyAndroidAppActivity
public class MyAndroidAppActivityTest extends ActivityInstrumentationTestCase {

    public MyAndroidAppActivityTest() {
        super("com.example.myandroid", MyAndroidAppActivity.class);
    public void testControlsCreated() {
    	Activity activity = getActivity();
    public void testTitleGood() {
    	Activity activity = getActivity();
    	assertEquals(activity.getTitle(), MyAndroidAppActivity.TITLE);

    public void testTitleBad() {
    	Activity activity = getActivity();
    	assertEquals(activity.getTitle(), MyAndroidAppActivity.TITLE + 1);

Run test

  • Go to MyAndroidAppActivityTest project and run ant test target
ant run-tests
  • result
     [echo] Running tests ...
     [exec] Can't dispatch DDM chunk 46454154: no handler defined
     [exec] Can't dispatch DDM chunk 4d505251: no handler defined
     [exec] com.example.myandroid.test.MyAndroidAppActivityTest:.
     [exec] Failure in testTitleBad:
     [exec] junit.framework.AssertionFailedError: expected: but was:
     [exec] 	at com.example.myandroid.test.MyAndroidAppActivityTest.testTitleBad(
     [exec] 	at java.lang.reflect.Method.invokeNative(Native Method)
     [exec] 	at android.test.InstrumentationTestCase.runMethod(
     [exec] 	at android.test.InstrumentationTestCase.runTest(
     [exec] 	at android.test.AndroidTestRunner.runTest(
     [exec] 	at android.test.AndroidTestRunner.runTest(
     [exec] 	at android.test.InstrumentationTestRunner.onStart(
     [exec] 	at$
     [exec] ..
     [exec] Test results for InstrumentationTestRunner=..F..
     [exec] Time: 6.438
     [exec] FAILURES!!!
     [exec] Tests run: 4,  Failures: 1,  Errors: 0

More about testing

ShareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on VKEmail this to someone

Android Hello World!

Required software:

export JAVA_HOME=/<path_to_jdk>/
export PATH=${PATH}:/<path_to_jdk>/bin/
export ANT_HOME=<path_to_ant>/
export PATH=${PATH}:<path_to_ant>/bin/
export PATH=${PATH}:<path_to_sdk>/tools/
export PATH=${PATH}:<path_to_sdk>/platform-tools/
$ android

In the Android SDK and AVD Manager, choose Available Packages in the left panel.
In the right panel, expand the Android Repository list to display the components available for installation.
Select at least one platform to install, and click Install Selected. If you aren’t sure which platform to install, use the latest version.

In command line:

android create project 
--target 1 
--name MyAndroidApp 
--path ./MyAndroidAppProject 
--activity MyAndroidAppActivity 
--package com.example.myandroid
  • Create debugging apk
cd ./MyAndroidAppProject 
ant debug
  • Install apk on your android device or AVD
adb install bin/MyAndroidApp-debug.apk
ShareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on VKEmail this to someone