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

Leave a Reply

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