Unit test JUnit for Java.
The Java program entry is the Main method. Generally not in the Android app portal
@Override protectedvoid onCreate (Bundle savedinstancestate) { Super . OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); }
Do the testing.
Package com.itheima.test; Import android.test.AndroidTestCase; Public class extends androidtestcase { publicvoid Testadd () { new MyMath (); int result = Math.add (3, 4); Assertequals (7, result); // assertion, preceded by expectations, followed by actual values }}
This is an Android app that ultimately wants to test this method and has to run the code on the device. The code has to run on the Arm/dalvik virtual machine. So the first thing to do is to deploy the code to the device.
[2017-06-13 06:38:14-SDK Manager] Created AVD ' Android95device ' based on Android 4.2, Intel Atom (x86) processor,[2017-06-13 06:38:14-sdk Manager] with th E following hardware config:[2017-06-13 06:38:14-SDK Manager] hw.sdcard=yes[2017-06-13 06:38:14-SDK Manager] Hw.device . manufacturer=generic[2017-06-13 06:38:14-SDK Manager] hw.mainkeys=yes[2017-06-13 06:38:14-SDK Manager] HW.LCD.DENSITY=160[2017-06-13 06:38:14-SDK Manager] hw.accelerometer=yes[2017-06-13 06:38:14-SDK Manager] Hw.dPad=no [2017-06-13 06:38:14-sdk Manager] hw.device.hash=-1587417588[2017-06-13 06:38:14-SDK Manager] hw.trackball=yes[ 2017-06-13 06:38:14-SDK Manager] hw.device.name=3.2in QVGA (ADP2) [2017-06-13 06:38:14-sdk Manager] Hw.camera.back=none [2017-06-13 06:38:14-sdk Manager] hw.sensors.proximity=yes[2017-06-13 06:38:14-SDK Manager] hw.battery=yes[ 2017-06-13 06:38:14-SDK Manager] disk.datapartition.size=200m[2017-06-13 06:38:14-SDK Manager] hw.audioinput=yes[ 2017-06-13 06:38:14-SDK MAnager] hw.sensors.orientation=yes[2017-06-13 06:38:14-sdk Manager] hw.gps=yes[2017-06-13 06:38:14-SDK manager] skin. DYNAMIC=YES[2017-06-13 06:38:14-SDK Manager] hw.keyboard=yes[2017-06-13 06:38:14-SDK Manager] Vm.heapsize=16[2017-06 -13 06:38:14-sdk Manager] hw.ramsize=512[2017-06-13 07:19:33-day03_01_android Unit Test]------------------------------[ 2017-06-13 07:19:33-day03_01_android Unit Test] Android launch! [2017-06-13 07:19:33-day03_01_android Unit Test] adb is running normally. [2017-06-13 07:19:33-day03_01_android Unit Test] Day03_01_android Unit test does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-l Ibrary Android.test.runner in its androidmanifest.xml
You must specify an instrument device in the manifest file instrumentation a Android.test.InstrumentationTestRunner
<android:name= "Android.test.InstrumentationTestRunner" android: Targetpackage= "Com.itheima.test"></instrumentation>
<USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.itheima.test"></Instrumentation> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <uses-libraryAndroid:name= "Android.test.runner"/>
Specifies the instrument to be used for the test <instrumentation android:name= "Android.test.InstrumentationTestRunner" android:targetpackage= " Com.itheima.test "></instrumentation>, and also the library to be relied on for testing <uses-library android:name=" Android.test.runner "/ >.
This code must be executed under Android's virtual machine. There is no Android device on the PC. So be sure to deploy it on an Android device to test the code inside the Android project.
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.itheima.test"Android:versioncode= "1"Android:versionname= "1.0" > <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" /> <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.itheima.test"></Instrumentation> <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <uses-libraryAndroid:name= "Android.test.runner"/> <ActivityAndroid:name= "Com.itheima.test.MainActivity"Android:label= "@string/app_name" > <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity> </Application></Manifest>
Package com.itheima.test; Public class MyMath { publicint Add (int i,int j) { Return i+j; }}
Package com.itheima.test; Import android.test.AndroidTestCase; Public class extends androidtestcase { publicvoid Testadd () { new MyMath (); int result = Math.add (3, 4); Assertequals (7, result); // assertion, preceded by expectations, followed by actual values }}
02_android under Unit test