码迷,mamicode.com
首页 > 移动开发 > 详细

02_android下单元测试

时间:2017-06-13 10:04:44      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:http   虚拟机   eric   oid   boa   density   rac   man   class   

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下单元测试

标签:http   虚拟机   eric   oid   boa   density   rac   man   class   

原文地址:http://www.cnblogs.com/ZHONGZHENHUA/p/6999104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!