android基础知识12:android自动化测试06—Instrumentation 01 例子

发布时间:2020-07-22 16:06:15 作者:zhukev
来源:网络 阅读:276

转载处:http://blog.csdn.net/xianming01/article/details/7893391

下面通过一个简单的例子来讲解Instrumentation的基本测试方法。在这个例子中我们会建立一个简单的android应用,同时在其上添加Instrumentation测试程序。

    1.首先建立一个android  project,其文件结构最终如下:

android基础知识12:android自动化测试06—Instrumentation 01 例子

2、布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.hustophone.sample" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <!--用于引入测试库-->  
  7.         <uses-library android:name="android.test.runner" />  
  8.         <activity android:name=".Sample" android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.         </activity>  
  14.     </application>  
  15.     <uses-sdk android:minSdkVersion="3" />  
  16.     <!--表示被测试的目标包与instrumentation的名称。-->  
  17.     <instrumentation android:targetPackage="com.hustophone.sample"  
  18.         android:name="android.test.InstrumentationTestRunner" />  
  19. </manifest>  
3、被测程序Sample类

[java] view plaincopy
  1. package com.hustophone.sample;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;   
  8.   
  9. public class Sample extends Activity {  
  10.   
  11.     private TextView myText = null;  
  12.     private Button button = null;   
  13.   
  14.     /** Called when the activity is first created. */  
  15.   
  16.     @Override  
  17.   
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.   
  20.         super.onCreate(savedInstanceState);  
  21.   
  22.         setContentView(R.layout.main);  
  23.   
  24.         myText = (TextView) findViewById(R.id.text1);  
  25.   
  26.         button = (Button) findViewById(R.id.button1);  
  27.   
  28.         button.setOnClickListener(new OnClickListener() {  
  29.    
  30.   
  31.             @Override  
  32.   
  33.             public void onClick(View arg0) {  
  34.   
  35.                 // TODO Auto-generated method stub  
  36.   
  37.                 myText.setText("Hello Android");  
  38.   
  39.             }  
  40.   
  41.         });  
  42.   
  43.     }   
  44.   
  45.     public int add(int i, int j) {  
  46.   
  47.         // TODO Auto-generated method stub  
  48.   
  49.         return (i + j);  
  50.   
  51.     }  
  52.   
  53. }  
        这个程序的功能比较简单,点击按钮之后,TextView的内容由Hello变为Hello Android.同时,在这个类中,我还写了一个简单的方法,没有被调用,仅供测试而已。
4、测试类SampleTest
      通常可以将测试程序作为另一个android应用程序。但是这里我们为了操作方便,写在了一个应用里面了。
[java] view plaincopy
  1. package com.hustophone.sample.test;  
  2. import com.hustophone.sample.R;  
  3. import com.hustophone.sample.Sample;  
  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.     private Sample sample = null;  
  14.     private Button button = null;  
  15.     private TextView text = null;  
  16.   
  17.     /* 
  18.  
  19.      * 初始设置 
  20.      * 
  21.      * @see junit.framework.TestCase#setUp() 
  22.      */  
  23.   
  24.     @Override  
  25.   
  26.     protected void setUp()  {  
  27.   
  28.         try {  
  29.             super.setUp();  
  30.         } catch (Exception e) {  
  31.   
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         }  
  35.   
  36.         Intent intent = new Intent();  
  37.         intent.setClassName("com.hustophone.sample", Sample.class.getName());  
  38.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  39.         sample = (Sample) getInstrumentation().startActivitySync(intent);  
  40.         text = (TextView) sample.findViewById(R.id.text1);  
  41.         button = (Button) sample.findViewById(R.id.button1);  
  42.   
  43.     }  
  44.   
  45.     /* 
  46.  
  47.      * 垃圾清理与资源回收 
  48.  
  49.      * 
  50.      * @see android.test.InstrumentationTestCase#tearDown() 
  51.  
  52.      */  
  53.   
  54.     @Override  
  55.   
  56.     protected void tearDown()  {  
  57.   
  58.         sample.finish();  
  59.   
  60.         try {  
  61.   
  62.             super.tearDown();  
  63.   
  64.         } catch (Exception e) {  
  65.   
  66.             // TODO Auto-generated catch block  
  67.   
  68.             e.printStackTrace();  
  69.   
  70.         }  
  71.   
  72.     }  
  73.   
  74.    
  75.   
  76.     /* 
  77.  
  78.      * 活动功能测试 
  79.  
  80.      */  
  81.   
  82. public void testActivity() throws Exception {  
  83.   
  84. Log.v("testActivity""test the Activity");  
  85.   
  86.         SystemClock.sleep(1500);  
  87.   
  88.         getInstrumentation().runOnMainSync(new PerformClick(button));  
  89.   
  90.         SystemClock.sleep(3000);  
  91.   
  92.         assertEquals("Hello Android", text.getText().toString());  
  93.   
  94.     }   
  95.   
  96.     /* 
  97.  
  98.      * 模拟按钮点击的接口 
  99.  
  100.      */  
  101.   
  102.     private class PerformClick implements Runnable {  
  103.   
  104.         Button btn;   
  105.   
  106.         public PerformClick(Button button) {  
  107.   
  108.             btn = button;  
  109.   
  110.         }   
  111.   
  112.         public void run() {  
  113.   
  114.             btn.performClick();  
  115.   
  116.         }  
  117.   
  118.     }   
  119.   
  120.     /* 
  121.  
  122.      * 测试类中的方法 
  123.  
  124.      */  
  125.   
  126.     public void testAdd() throws Exception{  
  127.   
  128.         String tag = "testAdd";  
  129.   
  130.         Log.v(tag, "test the method");  
  131.   
  132.         int test = sample.add(11);  
  133.   
  134.         assertEquals(2, test);  
  135.   
  136.     }   
  137.   
  138. }  
下面来简单讲解一下代码:
  setUp()和tearDown()都是受保护的方法,通过继承可以覆写这些方法。
  在android Developer中有如下的解释
[java] view plaincopy
  1. protected void setUp ()  
  2.   
  3. Since: API Level 3  
  4.   
  5. Sets up the fixture, for example, open a network connection. This method is called before a test is executed.  
  6.   
  7. protected void tearDown ()  
  8.   
  9. Since: API Level 3  
  10.   
  11. Make sure all resources are cleaned up and garbage collected before moving on to the next test. Subclasses that override this method should make sure they call super.tearDown() at the end of the overriding method.   
 setUp ()用来初始设置,如启动一个Activity,初始化资源等。
    tearDown ()则用来垃圾清理与资源回收。
 在testActivity()这个测试方法中,我模拟了一个按钮点击事件,然后来判断程序是否按照预期的执行。在这里PerformClick这个方法继承了Runnable接口,通过新的线程来执行模拟事件,之所以这么做,是因为如果直接在UI线程中运行可能会阻滞UI线程。
<uses-library android:name="android.test.runner" />用于引入测试库
<instrumentation android:targetPackage="com.hustophone.sample"
       android:name="android.test.InstrumentationTestRunner" />
   表示被测试的目标包与instrumentation的名称。 
   经过以上步骤,下面可以开始测试了。测试方法也有以下几种,下面介绍两个常用的方法: 
 (1) 用Eclipse集成的JUnit工具
     在Eclipse中选择工程Sample,单击右键,在Run as子菜单选项中选择Android JUnit Test
android基础知识12:android自动化测试06—Instrumentation 01 例子
同时可以通过LogCat工具查看信息
android基础知识12:android自动化测试06—Instrumentation 01 例子
(2) 通过模拟器运行单元测试
     点击模拟器界面的Dev Tools菜单
android基础知识12:android自动化测试06—Instrumentation 01 例子
再点击Instrumentation选项,进入Instrumentation菜单
android基础知识12:android自动化测试06—Instrumentation 01 例子
android基础知识12:android自动化测试06—Instrumentation 01 例子
这里有一个InstrumentationTestRunner,它是测试的入口,点击这个选项,就可以自动运行我们的测试代码。以下为运行结果:
按钮点击前
android基础知识12:android自动化测试06—Instrumentation 01 例子
按钮点击后
android基础知识12:android自动化测试06—Instrumentation 01 例子

         至此,一个简单的测试过程结束了。当然,android的测试内容还有很多,也有比较复杂的,我会在以后的学习过程中继续分享我的体会。好了,今天就到这里吧!


推荐阅读:
  1. python基础知识 01 python模块
  2. Shell脚本基础知识

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

自动化 andriod instrumentation

上一篇:Android 常见对话框

下一篇:Python如何引用参数传递对象

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》