Andriod 自动化测试—InstrumentationTestCase

本文详细介绍了一种使用InstrumentationTestCase进行Andriod自动化测试的方法。通过实例演示了如何创建测试项目、编写测试代码以及配置AndriodManifest.xml文件来关联测试程序与被测试应用。此外,还提供了两种运行测试程序的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转:http://cjxixi.iteye.com/blog/1746495
 

Andriod 自动化测试—InstrumentationTestCase:

 

1,新建一个andriod project:

 

2,编写MainActivity代码,如:

 

Java代码  收藏代码
  1. package cj.andriodtest.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. import android.view.View;  
  7.   
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class App extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState)  
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         final TextView myText = (TextView) findViewById(R.id.text1);  
  19.   
  20.         Button button = (Button) findViewById(R.id.button1);  
  21.         button.setOnClickListener(new Button.OnClickListener() {  
  22.         @Override  
  23.         public void onClick(View arg0) {  
  24.                myText.setText("Hello Android");  
  25.            }});  
  26.       }             
  27.      public int add(int i, int j)  
  28.      {  
  29.         return (i + j);  
  30.       }  
  31.           
  32. }  
 

3,编写测试代码,如:

 

Java代码  收藏代码
  1. package cj.andriodtest.test;  
  2.   
  3. import cj.andriodtest.com.App;  
  4. import android.content.Intent;  
  5. import android.os.SystemClock;  
  6. import android.test.InstrumentationTestCase;  
  7. import android.util.Log;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class SampleTest extends InstrumentationTestCase {  
  12.       
  13.       
  14.     private App app = null;  
  15.     private Button button = null;  
  16.     private TextView text = null;  
  17.   
  18.     /* 
  19.      * 初始设置,该方法第一个被调用,完成初始化工作 
  20.      */  
  21.     @Override  
  22.     protected void setUp() {  
  23.         try {  
  24.               
  25.             super.setUp();  
  26.           
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.         Intent intent = new Intent();  
  31.         //System.out.println("---------------------------------------------------");  
  32.         intent.setClassName("cj.andriodtest.com", App.class.getName());  
  33.         System.out.println("99999999999999999");  
  34.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  35.         //获取被测对象context  
  36.         app = (App) getInstrumentation().startActivitySync(intent);  
  37.         //System.out.println("Apppppppppp:  "+app);  
  38.         //获取被测试的程序的资源要加包名,不然无法获取  
  39.         text = (TextView) app.findViewById(cj.andriodtest.com.R.id.text1);  
  40.         //System.out.println("textttttttt:  "+text);  
  41.         //获取被测试的程序的资源要加包名,不然无法获取  
  42.         button = (Button)app.findViewById(cj.andriodtest.com.R.id.button1);;  
  43.           
  44.           
  45.     }  
  46.   
  47.     /* 
  48.      * 垃圾清理与资源回收 ,测试用例完成时调用 
  49.      *  
  50.      */  
  51.     @Override  
  52.     protected void tearDown() {  
  53.         app.finish();  
  54.         try {  
  55.             super.tearDown();  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.   
  61.     /* 
  62.      * 活动功能测试 
  63.      */  
  64.     public void testActivity() throws Exception {  
  65.         Log.v("testActivity""test the Activity");  
  66.         SystemClock.sleep(1500);  
  67.         //getInstrumentation().runOnMainSync(new PerformClick(button));       
  68.         getInstrumentation().runOnMainSync(new PerformClick(button));  
  69.         SystemClock.sleep(3000);  
  70.         assertEquals("Hello Android", text.getText().toString());  
  71.     }  
  72.   
  73.     /* 
  74.      *模拟按钮点击的接口 
  75.      */  
  76.     private class PerformClick implements Runnable {  
  77.         Button btn;  
  78.   
  79.         public PerformClick(Button button) {  
  80.             btn = button;  
  81.         }  
  82.         public void run() {  
  83.               
  84.             btn.performClick();  
  85.         }  
  86.     }  
  87.   
  88.     /* 
  89.      * 测试类中的方法  
  90.      */  
  91.     public void testAdd() throws Exception {  
  92.         String tag = "testAdd";  
  93.         Log.v(tag, "test the method");  
  94.         int test = app.add(11);  
  95.         assertEquals(2, test);  
  96.     }  
  97. }  

 

4,配置AndriodManifest.xml文件,将测试代码与被测试程序关联:

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cj.andriodtest.com"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.     <!--需要加载测试扩展包-->  
  8.         <uses-library android:name="android.test.runner" />  
  9.         <activity android:name=".App"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18.     <uses-sdk android:minSdkVersion="8" />  
  19.         <!--加载被测主程序包名-->  
  20.     <instrumentation android:targetPackage="cj.andriodtest.com" android:name="android.test.InstrumentationTestRunner" />  
  21. </manifest>   

 5,运行测试程序,运行测试程序有2种方式:

 

1> 在andriodproject 上右击,run as Andriod JUnit Test,直接运行测试用例

 

 

2>在模拟器上,运行测试用例。在模拟器中选择dev tools->“andriod.test.InstrumentationTestRunner”

来运行测试用例。这种很好,可以不需要pc运行测试用例。

注意:dev tools 真机上可以没有,需要安装


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值