This example describes the application unit test for Android learning notes. Share to everyone for your reference, specific as follows:
Step One: Add the following two paragraph code to the Androidmanifest.xml:
<manifest xmlns:android=" http://schemas.android.com/apk/res/android "package=" COM.PCCW "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion=" 8 "/> Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" android.i Ntent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter>
;
</activity> <!-Add code 1--> <uses-library android:name= "Android.test.runner"/> </application> <!-Add code 2--> <instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage = "COM.PCCW" android:label= "aaa"/> </manifest>
1. <uses-library android:name= "Android.test.runner"/> representative introduced some of the dependency libraries in the Unit test framework
2. <instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= "COM.PCCW" Android:label= "AAA"/> Represents the boot device of the configuration Unit test framework, which has several classes to choose from, and in general we use this above.
3. The targetpackage is the same as the package above, representing the unit test framework and the current application being in the same process
Step Two: Write the business logic, the module that needs to be tested
public class Personservice {public
void Save (string name) {
string sub = name.substring (6);
}
public int Add (int a, int b) {return
a+b;
}
}
Step Three: Write Unit test code
public class Personservicetest extends Androidtestcase {public
void Testsave () throws Exception {
Personservice Service = new Personservice ();
Service.save (null);
}
public void Testadd () throws Exception {
Personservice service = new Personservice ();
int result = Service.add (1, 2);
Assert.assertequals (3, result);
}
Step Fourth: Open the Outline window in Eclipse, which displays all the methods for the Unit test class
Then you want to test which method, the test method on which the right mouse, select Run as, and then choose the Android JUnit test, if there is an exception or error, the following situation occurs:
If it is normal, it will be as follows:
I hope this article will help you with the Android program.