博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Instrumentation自动化测试框架
阅读量:7039 次
发布时间:2019-06-28

本文共 10557 字,大约阅读时间需要 35 分钟。

hot3.png

Instrumentation 是google开发的Android测试框架()

主要分为下列项目:

AndroidTestCase 主要来测试相关非交互性API,比如数据库,内容提供者等,其优点是可以通过getContext获取上下文

public class TestAudio extends AndroidTestCase {      private AudioManager mAudioManager;      private boolean mUseFixedVolume;      private final static long TIME_TO_PLAY = 2000;      private final static int MP3_TO_PLAY = R.raw.testmp3;          private Context mContext;          @Override      protected void setUp() throws Exception {          // TODO Auto-generated method stub          super.setUp();                  mContext = getContext();              }            public void testmp3(){          MediaPlayer mp = MediaPlayer.create(mContext, MP3_TO_PLAY);          mp.setAudioStreamType(STREAM_MUSIC);          mp.setLooping(true);          mp.start();          try {              Thread.sleep(20*1000);          } catch (InterruptedException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }      }  }

ActivityInstrumentationTestCase2,,主要来测试Activity相关API,方便之处是可以直接获取Activity

public class AdminMainTest extends ActivityInstrumentationTestCase2
 {         private MainActivity mActivity;    private Instrumentation mInstrumentation;    private Button login;    private EditText account;    private EditText password;    private RadioGroup radioGroup;    //private RadioButton button;    private RadioButton button1;    private RadioButton button2;    private RadioButton button3;    private RadioButton button4;         private Context mContext;    private View buttonView;         public AdminMainTest(){        super(MainActivity.class); //目标Activity    }    @Before    protected void setUp() throws Exception {        super.setUp();        setActivityInitialTouchMode(false);        mInstrumentation=getInstrumentation();        mContext=mInstrumentation.getContext();        mActivity=getActivity();        login=(Button) mActivity.findViewById(com.example.example.R.id.landed);        account=(EditText) mActivity.findViewById(com.example.example.R.id.landed_account);        password=(EditText) mActivity.findViewById(com.example.example.R.id.landed_password);        radioGroup = (RadioGroup) mActivity.findViewById(R.id.landed_user_type);                button1=(RadioButton) mActivity.findViewById(R.id.landed_user_type_admin);        button2=(RadioButton) mActivity.findViewById(R.id.landed_user_type_publisher);        button3=(RadioButton)mActivity.findViewById(R.id.landed_user_type_common);        button4=(RadioButton)mActivity.findViewById(R.id.landed_user_type_visitor);    }     @After    protected void tearDown() throws Exception {        mActivity.finish();        super.tearDown();    }         public void testPreConditions(){        assertNotNull(mActivity);        assertNotNull(login);        assertNotNull(account);        assertNotNull(password);        assertNotNull(radioGroup);        assertNotNull(button1);        assertNotNull(button2);        assertNotNull(button3);        assertNotNull(button4);    }     public void input() {                  mActivity.runOnUiThread(new Runnable(){             @Override            public void run() {                // TODO Auto-generated method stub                SystemClock.sleep(1500);                account.requestFocus();                SystemClock.sleep(1500);                account.performClick();                //SystemClock.sleep(3000);            }        });        mInstrumentation.waitForIdleSync();                 sendKeys(KeyEvent.KEYCODE_S,KeyEvent.KEYCODE_O,                KeyEvent.KEYCODE_N,KeyEvent.KEYCODE_G);                 mActivity.runOnUiThread(new Runnable(){             @Override            public void run() {                // TODO Auto-generated method stub                SystemClock.sleep(1500);                password.requestFocus();                SystemClock.sleep(1500);                password.performClick();            }        });        mInstrumentation.waitForIdleSync();                 sendKeys(KeyEvent.KEYCODE_S,KeyEvent.KEYCODE_O,                KeyEvent.KEYCODE_N,KeyEvent.KEYCODE_G);                 mInstrumentation.waitForIdleSync();    }          public void testFirstRadioButton(){        assertTrue("The Admin button is checked",button1.isChecked());        //assertEquals("商品发布者",button.getText().toString());        assertEquals(R.id.landed_user_type_admin,radioGroup.getCheckedRadioButtonId());    }         public void testSecondRadioButton(){        //assertTrue("The Publisher button is checked",button2.isChecked());        //assertEquals(R.id.landed_user_type_publisher,radioGroup.getCheckedRadioButtonId());        assertEquals("商品发布者",button2.getText().toString());             }     public void testThirdRadioButton()    {        //assertTrue("The common user is checked",button3.isChecked());        //assertEquals(R.id.landed_user_type_common,radioGroup.getCheckedRadioButtonId());        assertEquals("普通用户",button3.getText().toString());    }            public void testFourthRadioButton()    {        //assertTrue("The guest is checked",button4.isChecked());        //assertEquals(R.id.landed_user_type_visitor,radioGroup.getCheckedRadioButtonId());        assertEquals("访客",button4.getText().toString());    }         public void testButton2Selection(){        testFirstRadioButton();        TouchUtils.clickView(this, button2);    //  assertFalse("The admin radio button should not be checked",button1.isChecked());        assertTrue("The publisher radio button should be checked",button2.isChecked());        assertEquals("The publisher button should be checked",R.id.landed_user_type_publisher,                radioGroup.getCheckedRadioButtonId());    }         public void testButton3Selection(){        testFirstRadioButton();        TouchUtils.clickView(this, button3);        assertTrue("The common user is checked",button3.isChecked());        assertEquals(R.id.landed_user_type_common,radioGroup.getCheckedRadioButtonId());    }         public void testButton4Selection(){        testFirstRadioButton();        TouchUtils.clickView(this, button4);        assertTrue("The guest is checked",button4.isChecked());        assertEquals(R.id.landed_user_type_visitor,radioGroup.getCheckedRadioButtonId());             }     /**     public void testRadioButtonChange(){        testFirstRadioButton();        TouchUtils.clickView(this, button);        assertFalse("The admin radio button should not be checked",button1.isChecked());        assertTrue("The publisher button should be checked",button.isChecked());        assertEquals("The publisher button should be checked",R.id.landed_user_type_publisher,                radioGroup.getCheckedRadioButtonId());    }**/    public void testInput(){        input();        assertEquals("song",account.getText().toString());        assertEquals("song",password.getText().toString());    }     @Test    public void testLogin(){        input();        //testRadioButtonChange();        mInstrumentation.runOnMainSync(new Runnable(){             @Override            public void run() {                // TODO Auto-generated method stub                SystemClock.sleep(1500);                login.requestFocus();                SystemClock.sleep(1500);                login.performClick();            }                     });    }      }

ServiceTestCase专门用来测试Service服务

public class MyServiceTest extends ServiceTestCase
 {    private String TAG="myservicetest";    private Context mContext;    /**     * 构造方法     */    public MyServiceTest() {        super(MyService.class);    }    /**     * 重写setUp方法,第一句调用super.setUp     */    protected void setUp() throws Exception {        super.setUp();        mContext = getContext();    }  // public void testAndroidTestCaseSetupProperly() {  // super.testAndroidTestCaseSetupProperly(); // }    protected void tearDown() throws Exception {        mContext = null;        super.tearDown();    }    /**     * 测试Service正确地启动     */    public void testStart() {        Log.i(TAG, "start testStart");            Intent intent = new Intent();            startService(intent);            MyService Serv=getService();            assertNotNull(Serv);        Log.i(TAG, "end testStart");        }    }    /**     * 测试Service正确的终止     */    public void teststop() {        Log.i(TAG, "start teststopService");            Intent intent = new Intent();            startService(intent);            MyService service = getService();            service.stopService(intent);         }}

 InstrumentationTestCase 相对于Activity,Service等测试,相对而言,比较灵活,其他测试很多都是继承自这个

public class TestHelloActiviry extends InstrumentationTestCase {            final String TAG = "TestHelloAppTestHelloApp";             Button mHelloTestButton;      EditText mHelloEditText;      HelloActivity mHelloTestActivity;      Instrumentation mInstrumentation;            public void testHelloActivity() {          Log.i(TAG, "call testHelloActivity()");          mHelloTestButton = (Button)mHelloTestActivity.findViewById(R.id.Button1);          mHelloEditText = (EditText)mHelloTestActivity.findViewById(R.id.EditText1);          for (int i = 0; i < 3; i++) {              //设置事件在主线程中执行              mInstrumentation.runOnMainSync(new Click(mHelloTestButton,mHelloEditText,Integer.toString(i)));              SystemClock.sleep(3000);          }                }            public void testHelloActivity2() {                }            private class Click implements Runnable{          Button button;          EditText editText;          String str;          Click(Button b,EditText e,String s){              button = b;              editText = e;              str = s;          }          @Override          public void run() {              editText.setText(str);                button.performClick();                        }      }            //负责testcase开始前的初始化工作      @Override      protected void setUp() throws Exception {          super.setUp();          Log.i(TAG, "call setUp()");          mInstrumentation = getInstrumentation();          Intent intent = new Intent();          intent.setClassName("com.example.hello", "com.example.hello.HelloActivity");          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);          //通过intent触发activity          mHelloTestActivity = (HelloActivity)mInstrumentation.startActivitySync(intent);      }          @Override      protected void tearDown() throws Exception {          super.tearDown();                    Log.i(TAG, "tearDown()");      }              }

参考

转载于:https://my.oschina.net/ososchina/blog/621740

你可能感兴趣的文章
第一讲 scala开发环境搭建
查看>>
MySQL簇的安装及配置
查看>>
screen命令的用法
查看>>
看程序员是如何把自动化做到极致的!
查看>>
MySQL性能优化的21条最佳经验
查看>>
Lua 学习记录
查看>>
关于linux批量改文件名问题
查看>>
SOLR组合条件查询
查看>>
CentOS6.x升级到7
查看>>
使用 Buildot 实现持续集成(转载)
查看>>
IE6兼容性大全
查看>>
freemaker模板学习笔记
查看>>
Redis配置文件解析+单机多实例(主从)配置
查看>>
Android消息推送机制
查看>>
在ECS上搭建阿里云数据库RDS的只读实例
查看>>
Go语言开发(二十一)、GoMock测试框架
查看>>
Java基础学习总结(2)——接口
查看>>
MyBatis学习总结(三)——优化MyBatis配置文件中的配置
查看>>
Linux实用工具
查看>>
北大高材生被华为辞退:用不用你,与能力无关!
查看>>