activity_main.xml內(nèi)容為:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xuxu.unittest.MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button" />
</RelativeLayout>
界面效果為:
配置AndroidManifest.xml
添加instrumentation
標(biāo)簽:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.xuxu.unittest" >
</instrumentation>
在application標(biāo)簽里面添加uses-library
:
<uses-library android:name="android.test.runner" />
文件內(nèi)容截圖:
完成MainActivity,實(shí)現(xiàn)點(diǎn)擊Button
按鈕以后TextView
的內(nèi)容由 Hello world! 變成 Hello android!
實(shí)現(xiàn)代碼以下:
package com.xuxu.unittest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Hello android!");
}
});
}
}
對(duì)MainActivity進(jìn)行測(cè)試
com.xuxu.unittest.test
完成MainActivityTest
package com.xuxu.unittest.test;
import com.xuxu.unittest.MainActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.TextView;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
private TextView textView;
private Button button;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
textView = (TextView) mActivity.findViewById(com.xuxu.unittest.R.id.textView);
button = (Button) mActivity.findViewById(com.xuxu.unittest.R.id.button);
}
//測(cè)試初始化條件
public void testInit() {
assertEquals("Hello world!", textView.getText().toString());
}
//測(cè)試點(diǎn)擊Button以后TextView的值
public void testButton() throws Exception {
//在UI線程中操作
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
Thread.sleep(500); //加個(gè)延時(shí),否則TextView內(nèi)容還為更改,就已做斷言了
assertEquals("Hello android!", textView.getText().toString());
}
}
運(yùn)行測(cè)試。右鍵項(xiàng)目,選擇Run As Android JUnit