- 浏览: 335754 次
- 性别:
- 来自: 安徽
文章分类
最新评论
-
secally:
楼主可以上传个例子吗,谢谢,我咋跑不起来,,eclipse不是 ...
Android自动打包、签名、优化、上传ANT脚本 -
secally:
请问下执行命令是啥,ant release 还是?麻烦指教谢谢 ...
Android自动打包、签名、优化、上传ANT脚本 -
trp1022:
好牛啊
程序员不能不通文史 -
sunny78117:
谢谢,写得好
Android bitmap优化 -
futurebp:
这是什么类型的脚本啊?
Android自动打包、签名、优化、上传ANT脚本
本文版权所有,欢迎转载,转载请注明来源: http://mdev.cc/dev
Andorid下使用Junit测试框架,是步入正规的Androdid开发的必经之路,在Junit中可以得到组件,可以模拟发送事件,检测程序处理的正确性,下面就开始我们的教程:
工具: 1、Android1.5 SDK 2、ADT 0.9 3、Eclipse
需要的知识: 1、 Android开发的基础 2、Junit的基础
一、
首先建立工程:
目录:
选中的test
source folder是测试类的包,包名随便,但是在配置
Manifest.xml要注意
二、配置\layout\main.xml文件,加入两个button组件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>
三、被测试的Activity代码
package cc.androidos.activity; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class NewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public int add(int a , int b){ return a+b; } }
四、测试NewActivity代码,这里要继承ActivityInstrumentationTestCase2,ActivityInstrumentationTestCase2是TestCase的子类
package cc.andoridos.activity.test; import cc.androidos.activity.NewActivity; import cc.androidos.activity.R; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; import android.view.KeyEvent; import android.widget.Button; public class TestNewActivity extends ActivityInstrumentationTestCase2<NewActivity> { private Button button1 = null; private Button button2 = null; private NewActivity newActivity = null; public TestNewActivity() { super("cc.androidos.activity", NewActivity.class); //This first parameter should the Activity package //if other , the junit give us the exception: unable resolve the activity } @Override protected void setUp() throws Exception { String tag = "setUp"; Log.e(tag, "init all var...."); newActivity = getActivity(); button1 = (Button) newActivity.findViewById(R.id.Button01); button2 = (Button) newActivity.findViewById(R.id.Button02); } /** * Testing the button is focused or not */ public void testButtonFocus() { String tag = "testButtonFocus"; Log.e(tag, "start the button focus..."); assertTrue("Button1 is focused", button1.isFocused()); Log.e(tag, "send the down key event..."); sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); assertTrue("Button2 is focused", button2.isFocused()); } /** * Testing the add method in actvity */ public void testAdd(){ String tag ="testAdd"; Log.e(tag, "Test the add method in NewActivity..."); int i = newActivity.add(2, 5); assertEquals(7, i); } }
五、配置Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cc.androidos.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".NewActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NewActivity2" android:label="@string/app_name"> </activity> <uses-library android:name="android.test.runner" /> <!-- Loading test library --> </application> <uses-sdk android:minSdkVersion="3" /> <!--这个是关键,android:targetPackage="cc.androidos.activity"要测试的包,android:name="android.test.InstrumentationTestRunner" 用于跑TestCase的类--> <instrumentation android:targetPackage="cc.androidos.activity" android:label="Test New Activty" android:name="android.test.InstrumentationTestRunner"></instrumentation> </manifest>
六、运行模拟器和项目
七、在TestCase类中点击右键:
八、运行结果
九、不使用Eclipse,控制台的命令:
E:\android\android-sdk-windows-1.5_pre\tools>adb shell am instrument -w cc.andro
idos.activity/android.test.InstrumentationTestRunner
idos.activity/android.test.InstrumentationTestRunner
如果你配置了Android环境变量,直接使用:
adb shell am instrument -w cc.androidos.activity/android.test.InstrumentationTestRunner
语法:adb shell am instrument -w <被测试的类的包名>/android.test.InstrumentationTestRunner
参数参考帮助文档
adb shell am instrument -w cc.androidos.activity/android.test.InstrumentationTestRunner
语法:adb shell am instrument -w <被测试的类的包名>/android.test.InstrumentationTestRunner
参数参考帮助文档
评论
2 楼
andyzhchy
2010-09-28
其实之前看到过CTS,Google的CTS就是一些单元测试,(*^__^*) 嘻嘻……
1 楼
andyzhchy
2010-09-28
学习啦,谢谢lz,之前还没研究到这一块,(*^__^*) 嘻嘻……
发表评论
-
抓取Android WebView截屏
2011-08-19 21:33 7604一般使用setDrawingCacheEnabled(true ... -
Android bitmap优化
2011-08-11 17:02 5899直接使用ImageView显示 ... -
Android模拟器更改分辨率后 无法取得分辨率问题
2011-05-11 21:06 2811在PC上更改模拟器分辨率为480*800 (如果你的PC界面 ... -
获取屏幕硬件分辨率方法 收藏
2011-05-11 16:28 1886TextView tv=(TextVie ... -
Bytecode for the Dalvik VM
2011-05-10 22:35 1323http://www.netmite.com/android/ ... -
Android DEX反编译后部分代码解析
2011-05-10 22:07 3877一个private 方法 private Stri ... -
反编译并且修改Android APK包
2011-05-10 16:57 3070為了某個實驗的動機,我們評估反編譯 Android ... -
Android Ant编译时候进行混淆
2010-10-18 13:49 3809版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
自定义Ant Task
2010-09-13 17:58 2968版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自动打包、签名、优化、上传ANT脚本
2010-07-26 16:51 13940版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
Android自定义含有CheckBox的Adapter遇到的问题
2010-06-23 13:48 4092版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
QR code类库
2010-06-03 23:11 2294Google code发现的好东西。 QR code 类库 ... -
Android QQ,一个WEBOS APP诞生!!(两个QQ对比!)
2010-04-10 12:05 1850请看这个大哥的言论 http://linuxtoy.or ... -
Android 、BlackBerry 文本对齐方式对比
2010-02-22 13:11 1781版权所有,欢迎转载,转载请注明 : SinFrancis h ... -
教你如何开关Android的APN网络
2010-01-08 11:36 5974版权所有,转载请注明来自Mobile Developer (h ... -
史上最强NDK入门项目实战
2009-10-10 13:29 2667以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
使用TimerTask控制ProgressBar
2009-10-10 13:21 1051以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
自定义List布局
2009-10-10 13:19 1861以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
重画Progressbar的进度
2009-10-10 13:15 1915以下内容Sinfrancis版权所有,专注请注明来自 htt ... -
谈谈Android中文短信的实现
2009-10-10 13:13 2147似 乎Google在推出G1时 ...
相关推荐
Android下使用JUnitTest用例,可以参见博客:http://www.cnblogs.com/plokmju/p/Android_JUnit.html
一个Gradle插件,允许在使用AndroidGradlePlugin3.2.0或更高版本的Android环境中执行JUnit5测试。
一个Gradle插件,允许使用Android Gradle Plugin 3.5.0或更高版本在Android环境中执行测试。 如何? 该插件为项目的每个构建变体配置单元测试任务,以在JUnit Platform上运行。 此外,它附加到android.testOptions...
Android Studio JUnit单元测试
android-junit-report jar包及源码
android studio junit测试 activity 对应文章地址 http://blog.csdn.net/qq_28478281/article/details/52891347
根据别人的Junit做的一个工程,免费提供,可以运行。 ...运行时,1....-- Android JUnit配置 --> <uses-library android:name="android.test.runner" /> 2. targetPackage与上面mainfest的package相同即可
自动化单元测试可以做许多的事,并帮你节省时间。它也可以被用作快速检验新建工程或进行冒烟测试。始终,单元测试是作为一种有效的、系统的检验应用 程序各功能...本教程假设你已熟悉Android和JUnit在Eclipse的使用。
我自己粗略的写了一下JUnit和android里面对JUnit的应用,因为没有太多时间整理,所以很粗略,等有时间再好好整理一下,见笑了。
自己进行简单的程序调试
android-junit android敏捷开发TDD的基础知识。
我们曾经和大家探讨过全面剖析JavaME单元测试理念,其实在Android上实现JUnit单元测试也不是很困难,主要是在配置文件和测试环境上将花费很长时间,下面从四步简单讲一下在Android上实 我们曾经和大家探讨过全面...
在本文中,你将会学习到如何在Eclipse中创建Android JUnit的单元测试工程以及在不同的条件下创建及运行自动测试用例
官方版本,亲测可用
android junit 例子代码,自学小例子。如果不能下来可以直接联系我
官方版本,亲测可用
android-junit-report-1.5.8.jar
Android JUnit单元测试基本实例
android-junit-report-1.5.8.jar — Version 1.5 (build 8). 8KB · Uploaded on 17 Oct 2012