首页 技术 正文
技术 2022年11月22日
0 收藏 547 点赞 4,116 浏览 3959 个字

Java的单元测试JUnit。

02_android下单元测试

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

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

做测试。

02_android下单元测试


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虚拟机才行。所以首先要把代码部署到设备上。

02_android下单元测试

[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

02_android下单元测试

 <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机上是没有安卓设备的。所以一定要把它部署到一个安卓设备上,才能够测试安卓项目里面的代码。

02_android下单元测试


02_android下单元测试


<?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);//断言,前面是期望值,后面是实际值
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842