如何在安卓开发中单元测试
在JavaEE中,有一个Junit测试包
而在开发安卓中,我们要使用谷歌公司开发好的一些类
代码如下:
这里要测试一个计算器类:
package org.dreamtech.helloworld;
public class Calc {
// 计算器类
public int add(int x, int y) {
return x + y;
}
}在配置文件下添加这一行:
<application
android:allowBackup="true"
android:enabled="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="org.dreamtech.helloworld.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>在外部加上这几行:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="org.dreamtech.helloworld" />写一个测试类:
package org.dreamtech.helloworld;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase {
public void testAdd() {
Calc calc = new Calc();
int result = calc.add(3, 5);
// 断言:第一个参数是期望值
assertEquals(8, result);
}
}测试成功!
相关推荐
蛰脚踝的天蝎 2020-11-10
Cocolada 2020-11-12
TuxedoLinux 2020-09-11
snowphy 2020-08-19
83540690 2020-08-16
lustdevil 2020-08-03
83417807 2020-07-19
张文倩数据库学生 2020-07-19
bobljm 2020-07-07
83417807 2020-06-28
86427019 2020-06-28
86427019 2020-06-25
zhengzf0 2020-06-21
tobecrazy 2020-06-16
宿命java 2020-06-15
83417807 2020-06-15
84284855 2020-06-11