`
CJxixi
  • 浏览: 104493 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Andriod 自动化测试—InstrumentationTestCase

 
阅读更多

Andriod 自动化测试—InstrumentationTestCase:

 

1,新建一个andriod project:

 

2,编写MainActivity代码,如:

 

package cj.andriodtest.com;

import android.app.Activity;
import android.os.Bundle;

import android.view.View;

import android.widget.Button;
import android.widget.TextView;

public class App extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final TextView myText = (TextView) findViewById(R.id.text1);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        	   myText.setText("Hello Android");
       	   }});
      }        	  
     public int add(int i, int j)
     {
        return (i + j);
      }
        
}
 

3,编写测试代码,如:

 

package cj.andriodtest.test;

import cj.andriodtest.com.App;
import android.content.Intent;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

public class SampleTest extends InstrumentationTestCase {
	
	
	private App app = null;
	private Button button = null;
	private TextView text = null;

	/*
	 * 初始设置,该方法第一个被调用,完成初始化工作
	 */
	@Override
	protected void setUp() {
		try {
			
			super.setUp();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		Intent intent = new Intent();
		//System.out.println("---------------------------------------------------");
		intent.setClassName("cj.andriodtest.com", App.class.getName());
		System.out.println("99999999999999999");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//获取被测对象context
		app = (App) getInstrumentation().startActivitySync(intent);
		//System.out.println("Apppppppppp:  "+app);
		//获取被测试的程序的资源要加包名,不然无法获取
		text = (TextView) app.findViewById(cj.andriodtest.com.R.id.text1);
		//System.out.println("textttttttt:  "+text);
		//获取被测试的程序的资源要加包名,不然无法获取
		button = (Button)app.findViewById(cj.andriodtest.com.R.id.button1);;
		
		
	}

	/*
	 * 垃圾清理与资源回收 ,测试用例完成时调用
	 * 
	 */
	@Override
	protected void tearDown() {
		app.finish();
		try {
			super.tearDown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * 活动功能测试
	 */
	public void testActivity() throws Exception {
		Log.v("testActivity", "test the Activity");
		SystemClock.sleep(1500);
		//getInstrumentation().runOnMainSync(new PerformClick(button));		
		getInstrumentation().runOnMainSync(new PerformClick(button));
		SystemClock.sleep(3000);
		assertEquals("Hello Android", text.getText().toString());
	}

	/*
	 *模拟按钮点击的接口
	 */
	private class PerformClick implements Runnable {
		Button btn;

		public PerformClick(Button button) {
			btn = button;
		}
		public void run() {
			
			btn.performClick();
		}
	}

	/*
	 * 测试类中的方法 
	 */
	public void testAdd() throws Exception {
		String tag = "testAdd";
		Log.v(tag, "test the method");
		int test = app.add(1, 1);
		assertEquals(2, test);
	}
}

 

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

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cj.andriodtest.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <!--需要加载测试扩展包-->
    	<uses-library android:name="android.test.runner" />
        <activity android:name=".App"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />
        <!--加载被测主程序包名-->
    <instrumentation android:targetPackage="cj.andriodtest.com" android:name="android.test.InstrumentationTestRunner" />
</manifest> 

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

 

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

 

 

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

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

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

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics