Android unit testing example
September 08, 2011 07:45:35 Last update: September 08, 2011 13:22:13
- Activity test:
-
AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.test" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <instrumentation android:targetPackage="com.example.android" android:name="android.test.InstrumentationTestRunner" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> </application> </manifest>
- Test code:
package com.example.android.test; import com.example.android.FirstScreen; import android.test.ActivityInstrumentationTestCase2; import android.widget.TextView; import android.util.Log; public class FirstAndroidTest extends ActivityInstrumentationTestCase2<FirstScreen> { private static final String TAG = "FirstAndroidTest"; private FirstScreen mActivity; private TextView mView; private String resourceString; public FirstAndroidTest() { super("com.example.android", FirstScreen.class); } @Override protected void setUp() throws Exception { Log.d(TAG, "setUp"); super.setUp(); mActivity = this.getActivity(); mView = (TextView) mActivity.findViewById(com.example.android.R.id.hello); resourceString = mActivity.getString(com.example.android.R.string.hello); } @Override protected void tearDown() throws Exception { Log.d(TAG, "tearDown"); super.tearDown(); } public void testA() { Log.d(TAG, "Running testA"); assertNull(resourceString); } public void testPreconditions() { Log.d(TAG, "Running testPreconditions"); assertNotNull(mView); } public void testText() { Log.d(TAG, "Running testText"); assertEquals(resourceString, (String)mView.getText()); } }
Notes: MethodssetUpandtearDownare called repetitively for each test method. If you override them, you must call the correspondingsupermethods for the tests to work.
-
- Self-instrumentation test (the target package is the test package itself):
-
AndroidManifest.xml:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test" android:versionCode="1" android:versionName="1.0"> <application> <uses-library android:name="android.test.runner"/> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.android.test"/> </manifest>
- Test code:
package com.android.test; import android.util.Log; import android.test.InstrumentationTestCase; public class AndroidTest extends InstrumentationTestCase { private static final String TAG = "AndroidTest"; @Override protected void setUp() throws Exception { Log.d(TAG, "setUp(): " + getName()); super.setUp(); } @Override protected void tearDown() throws Exception { Log.d(TAG, "tearDown(): " + getName()); super.tearDown(); } public void testCase1() { assertTrue(true); } public void testCase2() { assertEquals("12", 12); } public void testCase3() throws Exception { throw new Exception(); } public void thisIsNotATest() { Log.d(TAG, "This is not a test"); } }
-