Instrumentation 是google開發的Android測試框架(http://developer.android.com/reference/android/test/InstrumentationTestRunner.html)java
主要分爲下列項目:android
ProviderTestCase
this
ServiceTestCase
google
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,SingleLaunchActivityTestCase,ActivityUnitTestCase主要來測試Activity相關API,方便之處是能夠直接獲取Activity
public class AdminMainTest extends ActivityInstrumentationTestCase2<MainActivity> { 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<MyService> { 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()"); } }
參考