1.在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧 是正规的Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程 序处理的正确性。
第一步:首先在AndroidManifest.xml中加入下面代码:(注意替换成测试应用的包名)
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.action“ android:versionCode="1“
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
**<uses-library android:name="android.test.runner" />**
....
</application>
<uses-sdk android:minSdkVersion="6" />
**<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.itcast.action" android:label="Tests for My App" />**
</manifest>
|
2.第二步:编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android Junit 。编写一个类继承AndroidTestCase, 在这个类中编写测试方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class PersonServiceTest extends AndroidTestCase
{
public void testSave() throws Exception{//这个方法出错了
//会抛出一个异常给了测试框架
PersonServices services=new PersonServices();
services.save(null);
}
public void testAdd()throws Exception{
PersonServices services=new PersonServices();
int result=services.add(1, 2);
Assert.assertEquals(3, result);
}
}
|
选择要测试的方法,右键点击“Run As”--“Android Junit Test”
2.测试也可以单独的建一个项目以后通用
新建-->other--> Android Test Project-->
创建一个测试项目