Unit Testing for Android applications

Source: Internet
Author: User
Step 1: In androidmanifest. add the following two pieces of code to XML: [Java] view plaincopy <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> <Ction Android: Name = "android. intent. 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 "/> indicates introducing some dependent libraries in the unit test framework. 2. <instrumentation Android: Name = "android. test. instrumentationtestrunner "Android: targetpackage =" com. PCCW "Android: Label =" AAA "/> indicates the Startup Device for configuring the unit test framework. There are several types of start devices. You can choose from them. Generally, we use the above. 3. the targetpackage is the same as the preceding package, indicating that the unit test framework and the current application are in the same process. Step 2: Compile the business logic, that is, the module to be tested [Java] view plaincopypublic class personservice {public void save (string name) {string sub = Name. substring (6) ;}public int add (int A, int B) {return a + B ;}} Step 3: write the unit test code [Java] view plaincopypublic 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 4: Open the outline window in eclipse, where all methods of the unit test class are displayed and the method to be tested, right-click the test method, select Run as, and then select Android JUnit test. If an exception or error occurs, the following situation occurs, it will be as follows:
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.