When I was learning Android, the most special thing was the test! The virtual machine is slow and uncomfortable, the real machine is also very slow, because to install the application frequently, so the use of Android unit testing is particularly important.
It is not difficult to implement simple unit testing, just a few steps to complete:
The first thing to do is to make the following declaration under the manifest file (androidmanifest.xml):
<!-- The instructions for the test environment are placed outside the application node - < android:name= "Android.test.InstrumentationTestRunner" android:targetpackage = "Com.tai.mylistview" />
<!-- instructions for the test environment
Placed inside the application node with activity peers- -<android:name= " Android.test.runner "/>
You can refer to the following statement:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.tai.mylistview"4 Android:versioncode= "1"5 Android:versionname= "1.0" >6 <USES-SDK7 android:minsdkversion= "8"8 android:targetsdkversion= "+" />9 <uses-permissionAndroid:name= "Android.permission.INTERNET"/>Ten <!--instructions for the test environment - One <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.tai.mylistview"/> A - <Application - Android:allowbackup= "true" the Android:icon= "@drawable/ic_launcher" - Android:label= "@string/app_name" - Android:theme= "@style/apptheme" > - <!--instructions for the test environment - + <uses-libraryAndroid:name= "Android.test.runner"/> - <Activity + Android:name= "Com.tai.mylistview.MainActivity" A Android:label= "@string/app_name" > at <Intent-filter> - <ActionAndroid:name= "Android.intent.action.MAIN" /> - - <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> - </Intent-filter> - </Activity> in <ActivityAndroid:name= "Com.tai.mylistview.SimpleAdapter_Act"></Activity> - <ActivityAndroid:name= "Com.tai.mylistview.CheckImage"></Activity> to <ActivityAndroid:name= "Com.tai.mylistview.DataBase"></Activity> + - the <provider * Android:name= "Com.tai.provider.PersonDBProvider" $ android:authorities= "Com.tai.mylistview.personProvider"/>Panax Notoginseng </Application> - the </Manifest>
View Code
And then declare a class, this class to inherit from the Androidtestcase, the rest of the drink junit is the same.
Here's how to test the database operations in the previous article:
1 Packagecom.tai.test;2 3 ImportCom.person.dao.PersonDao;4 ImportCom.person.dao.PersonDao2;5 ImportCom.tai.db.PersonDBOpenHelper;6 7 Importandroid.test.AndroidTestCase;8 9 Public classTestextendsAndroidtestcase {Ten One Public voidTestcreatedb ()throwsException A { - //GetContext () is an API provided by the Android test class androidtestcase for easy access to the test context -Persondbopenhelper helper =NewPersondbopenhelper (GetContext ()); the helper.getreadabledatabase (); - } - - PrivatePersonDao2 DAO; + //when the test framework is ready, it performs the normal sit-down initialization operation. - @Override + protected voidSetUp ()throwsException { A Super. SetUp (); atDAO =NewPersonDao2 (GetContext ()); - } - //when the test method executes after the execution of the method is generally done to wipe the butt of the Operation - @Override - protected voidTearDown ()throwsException { - Super. TearDown (); in } - to Public voidTestadd ()throwsException + { - Booleanresult = Dao.add ("Zhang San", "999"); theAssertequals (true, result); * } $ Panax Notoginseng Public voidTestfind ()throwsException - { the Booleanresult = Dao.find ("Zhang San"); +Assertequals (true, result); A } the + Public voidTestupdate ()throwsException - { $ Booleanresult = Dao.update ("110", "Zhang San"); $Assertequals (true, result); - } - Public voidTestdelete ()throwsException the { - Booleanresult = Dao.delete ("Zhang San");WuyiAssertequals (true, result); the } -}
Test
The following two methods are useful.
When the test framework is ready, it performs the normal sit-down initialization operation.
@Override
protected void SetUp () throws Exception {
Super.setup ();
DAO = new PersonDao2 (GetContext ());
}
When the test method executes after the execution of the method is generally done to wipe the butt of the operation
@Override
protected void TearDown () throws Exception {
Super.teardown ();
}
Remember to write the test method when you want the unit frame throws an exception!!
OK simple test just so it can be used.
And one of the most lazy ways to build a test project directly!
After that, you can write test methods directly!