Android Study Notes: unit testing for Android apps

Source: Internet
Author: User

 

 

Step 1: Add the following two pieces of code to 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. intent. action. main "/> <cat Egory 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: Write the business logic, that is, the module 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 3: 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 4: Open the outline window in eclipse. All methods of the unit test class are displayed.

 

Right-click the method to be tested, select Run as, and then choose Android JUnit test. If an exception or error occurs, the following occurs:

 

If it is normal, 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.