首页 > 代码库 > 02_android下单元测试

02_android下单元测试

Java的单元测试JUnit。

技术分享

Java程序入口是main方法。一般不在安卓程序入口

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

做测试。

技术分享


package com.itheima.test;

import android.test.AndroidTestCase;

public class MyMathTest extends AndroidTestCase {
      public void testAdd(){
          MyMath math = new MyMath();
          int result = math.add(3, 4);
          assertEquals(7, result);//断言,前面是期望值,后面是实际值
      }
}

 

这是一个安卓的应用,最终想测试这个方法,必须得把代码跑到设备上才行。代码得跑在ARM/Dalvik虚拟机才行。所以首先要把代码部署到设备上。

技术分享

[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 the 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单元测试] ------------------------------
[2017-06-13 07:19:33 - Day03_01_android单元测试] Android Launch!
[2017-06-13 07:19:33 - Day03_01_android单元测试] adb is running normally.
[2017-06-13 07:19:33 - Day03_01_android单元测试] Day03_01_android单元测试 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml

必须在清单文件里面指定一个仪器设备instrumentation a android.test.InstrumentationTestRunner

技术分享

 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>

指定了测试要用到的仪器<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>,还有测试所要依赖的库<uses-library android:name="android.test.runner"/>。


这个代码一定要在安卓的虚拟机下执行。PC机上是没有安卓设备的。所以一定要把它部署到一个安卓设备上,才能够测试安卓项目里面的代码。

技术分享


技术分享


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.test"></instrumentation>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>
        <activity
            android:name="com.itheima.test.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>

</manifest>
package com.itheima.test;

public class MyMath {
       public int add(int i,int j){
           return i+j;
       }
}
package com.itheima.test;

import android.test.AndroidTestCase;

public class MyMathTest extends AndroidTestCase {
      public void testAdd(){
          MyMath math = new MyMath();
          int result = math.add(3, 4);
          assertEquals(7, result);//断言,前面是期望值,后面是实际值
      }
}

 

02_android下单元测试