Android自学笔记-7-Android中的junit
来源:互联网 发布:淘宝开放api 编辑:程序博客网 时间:2024/06/01 09:19
在android的开发的过程中,经常需要对业务方面的代码进行测试,熟悉java的人都知道junit,在android中google提供了基于junit为android进行了优化得自动化框架junit。在java中使用junit编写测试用例,我们需要继承TestCase,在android中需要继承AndroidTestCase。下面使用例子说明:
我们有如下的业务类:
package com.mxy.service;public class CalcService {public int add(int a,int b) {System.out.println("执行了加方法");return a + b;}}
下面我们就可以编写一个测试类,如下:
package com.mxy.test;import com.mxy.service.CalcService;import android.test.AndroidTestCase;public class CalcServiceTest extends AndroidTestCase {public void testAdd() {CalcService cal = new CalcService();int result = cal.add(5,3);System.out.println(result);assertEquals(8, result);}}这时候我们可以在项目上点击Run As---->Android Junit Test,这时候会将我们开发的app,安装到手机或者模拟器上,然后运行我们的测试用例。很不幸这时候我们会出现如下的错误:
01_junit does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml这是因为我们还有写地方没有配置,这时候我们打开我们的AndroidMainfest.xml,添加如下红色(由于红色无法正常显示,这里表示<span style="color:#ff0000;">包围的代码)代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mxy" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.mxy"android:label="your tests label" /> <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.mxy.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>这时候我们再重新运行一下,会出现跟使用junit类似的界面。
可能大家感觉这样写起来太费劲,一个是需要自己添加AndroidMainfest.xml里面的代码,另外一个等到程序发布的时候还得删除测试代码(当然如果不删除也没问题,只是apk的包会大一点),这时候我们就可以直接建立一个Android Test Project。我们可以点击新建---->Android Test Project,然后根据向导选择已经存在的工程。建立好工程后,我们就可以直接新建测试用例了。
例子工程下载地址:http://download.csdn.net/detail/mengxiangyue/6997545
转载请注明出处:http://blog.csdn.net/mengxiangyue
3 0
- Android自学笔记-7-Android中的junit
- android 自学中的散乱笔记
- 自学android过程中的一点笔记 -- 1
- Android中的Junit单元测试
- Android中的junit单元测试
- Android中的Junit单元测试
- Android中的Junit测试
- Android中的junit测试
- android junit 学习笔记
- 【Android自学笔记】Android中的Log小技巧
- Android OpenGL自学笔记
- Android JNI 自学笔记
- 【Android自学笔记】对应资源文件夹中的图标尺寸
- junit在android中的使用
- Android学习笔记-junit测试
- 小白自学Android路之 Junit测试
- android自学笔记(一)
- Android自学笔记之SharedPreference
- 数据库sharding(scale up to scale out)
- 一次面试的经过
- 工具集合pop
- 如何将安装好的英文版的Ubuntu系统改为中文
- HDU 1428 漫步校园(SPFA+记忆化搜索)
- Android自学笔记-7-Android中的junit
- windows下python的tar.gz文件安装
- UML用例图的作用、功能模块图作用与数据库设计三者关系
- 数据连接池——JNDI
- 封闭化:雅虎停用FB谷歌ID登陆
- 如何在mac上安装git
- 自学完成的第一个Java版猜拳游戏
- 1.文件操作之,文件流读取文本
- 让AtomicLong再飞一会儿