activity_main.xml内容为:
<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
标签:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.xuxu.unittest" >
</instrumentation>
在application标签里面添加uses-library
:
<uses-library android:name="android.test.runner" />
文件内容截图:
完成MainActivity,实现点击Button
按钮以后TextView
的内容由 Hello world! 变成 Hello android!
实现代码以下:
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!");
}
});
}
}
对MainActivity进行测试
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);
}
//测试初始化条件
public void testInit() {
assertEquals("Hello world!", textView.getText().toString());
}
//测试点击Button以后TextView的值
public void testButton() throws Exception {
//在UI线程中操作
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
Thread.sleep(500); //加个延时,否则TextView内容还为更改,就已做断言了
assertEquals("Hello android!", textView.getText().toString());
}
}
运行测试。右键项目,选择Run As Android JUnit