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> ;
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