原文网址 http://blog.sina.com.cn/s/blog_6091867a0100jsbu.html

Android下使用JUnit Andorid下使用Junit测试框架,是步入正规的Androdid开发的必经之路,在Junit中可以得到组件,可以模拟发送事件,检测程序处理的正确性,下面是一个简单的学习教程:转载来自:http://androidos.cc 工具: 1、Android1.5 SDK 2、ADT 0.9 3、Eclipse 需要的知识: 1、 Android开发的基础 2、Junit的基础 一、首先建立工程: 目录: 选中的test source folder是测试类的包,包名随便,但是在配置Manifest.xml要注意 二、配置/layout/main.xml文件,加入两个button组件,代码如下:

<!--l version="1.0" encoding="utf-8-->

三、被测试的Activity代码 package com.test.junit; import android.app.Activity; import android.os.Bundle; public class TestJ extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public int add(int a, int b) { return a + b; } } 四、测试TestJ代码,这里要继承ActivityInstrumentationTestCase2, ActivityInstrumentationTestCase2是TestCase的子类 package com.test.junit.test; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; import android.view.KeyEvent; import android.widget.Button; import com.test.junit.R; import com.test.junit.TestJ; public class TestJtestCase extends ActivityInstrumentationTestCase2 { int a; int b; int sum; int result; private Button button01; private Button button02; private TestJ testJ; public TestJtestCase() { super("com.test.junit", TestJ.class); } public TestJtestCase(String pkg, Class activityClass) { super(pkg, activityClass); // TODO Auto-generated constructor stub } @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); Log.v("SetUp", "init all var……"); a = 2; b = 22; sum = 24; testJ = getActivity(); button01 = (Button) testJ.findViewById(R.id.Button01); // button01.setEnabled(false); button02 = (Button) testJ.findViewById(R.id.Button02); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); } public void testButtonFocus(){ Log.v("Button01 Focus at first", "start the button focus......"); assertTrue("Button01 is fucused", button01.isFocusable()); Log.v("Button02 Focus at first", "send the down key event..."); this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); assertTrue("Button02 is fucused", button02.isFocusable()); } public void testAdd() { Log.v("testAdd","Test the add method in activity"); result = testJ.add(a, b); assertEquals(sum, result); } } 五、配置Manifest.xml文件 <!--l version="1.0" encoding="utf-8--><!--个很重要 如果没有配置 运行时报错:an instrumention test runner is not specified --><!-- Loading test library --><!--这个是关键,android:targetPackage="com.test.junit"要测试的包,android:name="android.test.InstrumentationTestRunner" 用于跑TestCase的类-->六、运行模拟器和项目 七、在TestCase类中点击右键: 八、运行结果 九、不使用Eclipse,控制台的命令: E:/android/android-sdk-windows-1.5_pre/tools>adb shell am instrument -w com.test.junit /android.test.InstrumentationTestRunner 如果你配置了Android环境变量,直接使用: adb shell am instrument -w com.test.junit/android.test.InstrumentationTestRunner 语法:adb shell am instrument -w <被测试的类的包名>/android.test.InstrumentationTestRunner

更多相关文章

  1. cocos2dx跨平台直播实例-ffmpeg-android篇
  2. Android(安卓)学习笔记--android――AsyncTask在Android4.X的机
  3. Android(安卓)Studio下Svn忽略文件配置的几种方法
  4. appium 调试问题--UiAutomator died while responding to comman
  5. Android4.0模拟器“谷歌输入法已停止运行”错误解决办法
  6. Unable to find method 'org.gradle.api.tasks.complie.ComplieO
  7. android 程序运行出现错误 Unable to execute dex: java.nio.Buf
  8. Android(安卓)自动化测试―robotium(四)CheckBox控件
  9. NFC之让Android自动运行程序

随机推荐

  1. jupyter和pycharm区别是什么?Python学习
  2. grid项目对齐示例并用grid模拟bootstrap/
  3. 0129-数组的排序, 数组的合并, 数组成员
  4. vue学习之路(路由)
  5. 0201-常用字符串函数
  6. Linux发行版的系统目录名称命名规则及用
  7. Centos7最小安装完成后,ifconfig命令用不
  8. 【最新】PMP、ACP换审续费流程
  9. 基于Docker部署Ldap环境
  10. Spinnaker持续交付实战