Android unit testing

  1.  Android unit testing  
  2.  More_about_testing  

 Android unit testing

see Android Hello World http://osll.spb.ru/projects/geo2tag/wiki/_android_hello_

Android tests example

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

Example classes

  • class MyAndroidAppActivity in MyAndroidAppActivity/com/example/myandroid
public class MyAndroidAppActivity extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle(TITLE);
    }
    
    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();
    	assertNotNull(activity);
    }
    
    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
run-tests:
     [echo] Running tests ...
     [exec] Can't dispatch DDM chunk 46454154: no handler defined
     [exec] Can't dispatch DDM chunk 4d505251: no handler defined
     [exec] 
     [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(MyAndroidAppActivityTest.java:26)
     [exec] 	at java.lang.reflect.Method.invokeNative(Native Method)
     [exec] 	at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:182)
     [exec] 	at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:172)
     [exec] 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
     [exec] 	at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
     [exec] 	at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:418)
     [exec] 	at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)
     [exec] ..
     [exec] Test results for InstrumentationTestRunner=..F..
     [exec] Time: 6.438
     [exec] 
     [exec] FAILURES!!!
     [exec] Tests run: 4,  Failures: 1,  Errors: 0
     [exec] 
     [exec]

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
android
  • 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