Android JUnit Test

Source: Internet
Author: User

Android Unit Test steps

1. Modify the Androidmanifest.xml file.

Add the instrumentation node. Where name is a fixed value, Targetpackage is the package that contains the class that needs to be tested. such as:
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.ahai.sqlitedemo"

Add <uses-library android:name= "Android.test.runner"/> Under the application node

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.ahai.sqlitedemo"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Instrumentation Android:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.ahai.sqlitedemo" > </instrumentation> <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name= "Com.ahai.sqlitedemo.MainActivity"Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter&        Gt </activity> <uses-library android:name= "Android.test.runner"/> </application></manifest&gt ;

2. Write test class, inherit from Androidtestcase. This class can refer to methods such as GetContext, which is very convenient. Such as:

 Package Com.ahai.sqlitedemo; Import android.test.AndroidTestCase; Import Com.ahai.sqlitedemo.dao.PersonDao;  Public class extends androidtestcase {    publicvoid  Insert () {        new person ( "Xiaojiang");         New Persondao (GetContext ());        Persondao.insert (person);    }}

3. Test the code, such as testing the Insert method, in the Package Explorer, right-click-run as-android JUnit Test.

4. Common errors

(1) Test run failed:unable to find instrumentation target package:

This error occurs because of the package value specified in manifest, which is inconsistent with the android:targetpackage. You need to change two to be consistent and move the test class into it and then test it.

Android JUnit Test

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.