instrumentation单元测试

1、写一个继承Activity的类SampleActivity,供测试用

public class SampleActivity extends Activity {
	private static final String TAG = SampleActivity.class.getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sample);
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.btn:
			TextView tv = (TextView) findViewById(R.id.txt);
			tv.setText("Hello Android");
			break;
		default:
			break;
		}
	}
	/**测试用*/
	public int and(int i,int j){
		return i+j;
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView 
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClick"/>
</LinearLayout>

2、在test包下再写个继承InstrumentationTestCase的类,用来测试SampleActivity

public class SampleTest extends InstrumentationTestCase {
	private static final String TAG = SampleTest.class.getSimpleName();
	private SampleActivity sample;
	private Button button;
	private TextView textView;
	@Override
	protected void setUp() {
		Log.v(TAG, "setUp");
		try {
			super.setUp();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Intent intent = new Intent();
		intent.setClassName("com.ivt.expandablelistviewdemo", SampleActivity.class.getName());
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		sample = (SampleActivity) getInstrumentation().startActivitySync(intent);
		button = (Button) sample.findViewById(R.id.btn);
		textView = (TextView) sample.findViewById(R.id.txt);
	}
	@Override
	protected void tearDown() {
		Log.v(TAG, "tearDown");
		sample.finish();
		try {
			super.tearDown();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void testActivity(){
		Log.v(TAG, "testActivity");
		SystemClock.sleep(1500);
		getInstrumentation().runOnMainSync(new PerformClick(button));
		SystemClock.sleep(3000);
		assertEquals("Hello Android", textView.getText());
	}
	private class PerformClick implements Runnable{
		Button btn;
		public PerformClick(Button button){
			btn = button;
		}
		@Override
		public void run() {
			btn.performClick();
		}
	}
	public void testAdd(){
		Log.v(TAG, "testAdd");
		int test = sample.and(1, 1);
		assertEquals(2, test);
	}
}

3、在AndroidManifest.xml进行配置,在manifest下插入子标签instrumentation,在application下插入子标签uses-library

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ivt.expandablelistviewdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <instrumentation android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.ivt.expandablelistviewdemo"></instrumentation>
    
    <application
        android:name="com.ivt.expandablelistviewdemo.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <uses-library android:name="android.test.runner"/>
        <activity android:name="com.ivt.expandablelistviewdemo.SampleActivity">
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值