Android下使用JUnit

本文版权所有,欢迎转载,转载请注明来源: http://mdev.cc/dev

 

Andorid下使用Junit测试框架,是步入正规的Androdid开发的必经之路,在Junit中可以得到组件,可以模拟发送事件,检测程序处理的正确性,下面就开始我们的教程:

 

Java代码
  1. 工具:   
  2. 1、Android1.5 SDK   
  3. 2、ADT 0.9  
  4. 3、Eclipse   
工具:
1、Android1.5 SDK
2、ADT 0.9
3、Eclipse 

 

Java代码
  1. 需要的知识:   
  2. 1、 Android开发的基础   
  3. 2Junit的基础  
需要的知识:
1、 Android开发的基础
2、Junit的基础

 

 
一、
首先建立工程:
目录:
 
选中的test source folder是测试类的包,包名随便,但是在配置 Manifest.xml要注意


 

二、配置/layout/main.xml文件,加入两个button组件,代码如下:
Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  android:orientation="vertical" android:layout_width="fill_parent"  
  4.  android:layout_height="fill_parent">  
  5.  <Button android:text="Button01" android:id="@+id/Button01"  
  6.   android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  7.  <Button android:text="Button02" android:id="@+id/Button02"  
  8.   android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>  
  9. </LinearLayout>  
 三、被测试的Activity代码
Java代码
  1. package cc.androidos.activity;   
  2. import android.app.Activity;   
  3. import android.os.Bundle;   
  4. import android.widget.Button;   
  5. public class NewActivity extends Activity {   
  6.     /** Called when the activity is first created. */    
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {   
  9.         super.onCreate(savedInstanceState);   
  10.         setContentView(R.layout.main);   
  11.     }   
  12.     public int add(int a , int b){   
  13.      return a+b;   
  14.     }   
  15. }  
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的子类

Java代码
  1. package cc.andoridos.activity.test;   
  2. import cc.androidos.activity.NewActivity;   
  3. import cc.androidos.activity.R;   
  4. import android.test.ActivityInstrumentationTestCase2;   
  5. import android.util.Log;   
  6. import android.view.KeyEvent;   
  7. import android.widget.Button;   
  8. public class TestNewActivity extends  
  9.   ActivityInstrumentationTestCase2<NewActivity> {   
  10.  private Button button1 = null;   
  11.  private Button button2 = null;   
  12.  private NewActivity newActivity = null;   
  13.  public TestNewActivity() {   
  14.   super("cc.androidos.activity", NewActivity.class);   
  15.   //This first parameter should the Activity package   
  16.   //if other , the junit give us the exception: unable resolve the activity   
  17.  }   
  18.     
  19.  @Override  
  20.  protected void setUp() throws Exception {   
  21.   String tag = "setUp";   
  22.   Log.e(tag, "init all var....");   
  23.   newActivity = getActivity();   
  24.   button1 = (Button) newActivity.findViewById(R.id.Button01);   
  25.   button2 = (Button) newActivity.findViewById(R.id.Button02);   
  26.  }   
  27.  /**  
  28.   * Testing the button is focused or not  
  29.   */  
  30.  public  void testButtonFocus() {   
  31.   String tag = "testButtonFocus";   
  32.   Log.e(tag, "start the button focus...");   
  33.   assertTrue("Button1 is focused", button1.isFocused());   
  34.   Log.e(tag, "send the down key event...");   
  35.   sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);   
  36.   assertTrue("Button2 is focused", button2.isFocused());   
  37.  }   
  38.     
  39.  /**  
  40.   * Testing the add method in actvity  
  41.   */  
  42.  public void testAdd(){   
  43.   String tag ="testAdd";   
  44.   Log.e(tag, "Test the add method in NewActivity...");   
  45.   int i  = newActivity.add(25);   
  46.   assertEquals(7, i);   
  47.  }   
  48.   
  49.     
  50. }  
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代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.  package="cc.androidos.activity" android:versionCode="1"  
  4.  android:versionName="1.0">  
  5.  <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.   <activity android:name=".NewActivity" android:label="@string/app_name">  
  7.    <intent-filter>  
  8.     <action android:name="android.intent.action.MAIN" />  
  9.     <category android:name="android.intent.category.LAUNCHER" />  
  10.    </intent-filter>  
  11.   </activity>  
  12.   <activity android:name=".NewActivity2" android:label="@string/app_name">  
  13.   </activity>  
  14.   <uses-library android:name="android.test.runner" />  
  15.   <!-- Loading test library -->  
  16.  </application>  
  17.  <uses-sdk android:minSdkVersion="3" />  
  18.     
  19. <!--这个是关键,android:targetPackage="cc.androidos.activity"要测试的包,android:name="android.test.InstrumentationTestRunner" 用于跑TestCase的类-->  
  20.  <instrumentation android:targetPackage="cc.androidos.activity"  
  21.   android:label="Test New Activty" android:name="android.test.InstrumentationTestRunner"></instrumentation>  
  22. </manifest>   
 
 
六、运行模拟器和项目
 
 七、在TestCase类中点击右键:


 

八、运行结果




九、不使用Eclipse,控制台的命令:
E:/ android/ android-sdk-windows-1.5_pre/tools>adb shell am instrument -w cc.andro
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
参数参考帮助文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值