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

转载处: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.   
  2.     package="com.hustophone.sample" android:versionCode="1"  
  3.     android:versionName="1.0">  
  4.       
  5.           
  6.           
  7.           
  8.               
  9.                   
  10.                   
  11.               
  12.           
  13.       
  14.       
  15.       
  16.     
  17.         android:name="android.test.InstrumentationTestRunner" />  
  18.   
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(1, 1);  
  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线程。
用于引入测试库
       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的测试内容还有很多,也有比较复杂的,我会在以后的学习过程中继续分享我的体会。好了,今天就到这里吧!



当前文章:android基础知识12:android自动化测试06—Instrumentation01例子
链接地址:http://bzwzjz.com/article/pihjsp.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都响应式网站建设 网站制作 成都网站设计 品牌网站建设 网站设计制作报价 网站建设开发 古蔺网站建设 成都网站建设 移动手机网站制作 成都网站制作 梓潼网站设计 成都网站建设 成都网站制作 成都品牌网站建设 成都企业网站建设公司 企业网站制作 营销型网站建设 四川成都网站设计 成都网站设计 成都网站建设 成都网站制作 重庆网站建设