`
cynhafa
  • 浏览: 154773 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android-JUnit

 
阅读更多

原文网址 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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics