If you want to do unit testing on Android, there are two basic ways to do it.
The first is the Java programmer's most familiar and commonly used junit, but since the current Android SDK (version 1.1) only provides stubbed methods/classes, there is no specific implementation code, so if you use JUnit, We need to run the unit test with the JDK and use the Java command to start a runner of JUnit. If you are using Eclipse, you can create a new junit in Run configuration. But make sure to remember to change the Android library in bootstrap entries to JRE in the Classpath tab and add Junit.jar. The specific settings can refer to: Http://developer.android.com/guide/appendix/faq/troubleshooting.html#addjunit. And, more unfortunately, this method runs on top of the JDK, not Android, so you can only test things that are not related to Android, such as business logic, data encapsulation, numerical calculations, and so on. Does not test the Android API.
Second, the use of instrumentation. The main entrance to the Android unit test is Instrumentationtestrunner. It is the equivalent of Testrunner in JUnit. You can interpret instrumentation as a tool class that has no graphical interface and has the ability to start and monitor other classes (declared with target package). Any class that wants to be a instrumentation must inherit android.app.Instrumentation.
Here's an example to see how to do unit testing with instrumentation.
Step 1: First write the activity that needs to be tested.
package com.android.ut;
import android.app.Activity;
import android.os.Bundle;
public class AndroidUT extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public int add(int a, int b)
{
return a + b;
}
}
Step 2.
Next, write the test class, which is primarily to test the Add () method. We are in the current code directory, in a new folder, named Test, and in the new package com.android.ut.test. Then add a new class to the inside. as follows:
package com.android.ut.test;
import com.android.ut.AndroidUT;
import android.test.ActivityInstrumentationTestCase;
public class TestApp extends ActivityInstrumentationTestCase<AndroidUT> {
public TestApp()
{
super("com.android.ut", AndroidUT.class);
}
public void testSum()
{
assertEquals(5, getActivity().add(2, 3));
}
}
Step 3. The final move is to change the manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.ut"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidUT"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:targetPackage="com.android.ut" android:name="android.test.InstrumentationTestRunner" android:label="Test Unit Tests"></instrumentation>
</manifest>
It is to be noted that in this I have added:
<uses-library android:name= "Android.test.runner"/>
And:
<instrumentation android:targetpackage= "Com.android.ut" android:name= "Android.test.InstrumentationTestRunner" android:label= "Test Unit Tests" ></instrumentation>
Step 4. Run
First run the Androidut through the simulator,
Then run in the command line terminal
ADB shell am INSTRUMENT-E class Com.android.ut.test.testapp-wcom.android.ut/android.test.instrumentationtestrunner
So you can see the test results.