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

Leave a Reply

Your email address will not be published. Required fields are marked *