How to add a unit test for Android

Source: Internet
Author: User

1. modify the configuration file androidmanifest. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "cn. ycmoon. Test. Activity" Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK Android: minsdkversion = "8"/>

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Uses-library Android: Name = "android. Test. Runner"/>
<Activity Android: Name = ". mainactivity" Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>
<Instrumentation Android: Name = "android. Test. instrumentationtestrunner"
Android: targetpackage = "cn. ycmoon. Test. Activity" Android: Label = "testing"/>
</Manifest>

Note: 1. Add it before the activity in the application Node<Uses-library Android: Name = "android. Test. Runner"/>

2. Add an application Node<Instrumentation Android: Name = "android. Test. instrumentationtestrunner" Android: targetpackage = "cn. ycmoon. Test. Activity" Android: Label = "testing"/>

2. Compile the method in the corresponding package,

 

 

3. Add a test folder [customizable] To the src directory at the same level, and create the same package name as the project: CN. ycmoon. Test. activity.

4. Right-click the unit test method and choose new> JUnit test case. The following page is displayed:

Select superclass as follows:

 

Click Next and select the method to be tested:

 

Click Finish.

Add the new unit test class to the test/project package name.

 

Add an implementation in the Unit Test method:

Expand the unit test class under test/CN. ycmoon. Test. Activity, right-click the method to be tested, and choose run as> Android JUnit test. The unit test result page is displayed.

If there is no error in this method, the result will display a green status bar:

 

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.