Android Learning Notes Application Unit Test example Analysis _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.