本文主要是介绍Android单元测试中的InstrumentationTestCase的用法。主要参考了这篇文章http://cjxixi.iteye.com/blog/1746495。
1.新建工程后,编写SampleActivity.java
public class SampleActivity extends Activity {
@SuppressWarnings("unused")
private String TAG="SampleActivity";
private TextView myText = null;
private Button button = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myText = (TextView) findViewById(R.id.mytext);
button = (Button) findViewById(R.id.testbtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
myText.setText("Hello Android");
}
});
}
public int add(int i, int j) {
return (i + j);
}
}
2.新建一个xxx.test包,编写测试代码SampleTest.java
public class SampleTest extends InstrumentationTestCase {
private SampleActivity sample = null;
private Button button = null;
private TextView text = null;
/*
* 初始设置
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() {
try {
super.setUp();
} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClassName("com.example.unittestinstrumentation", SampleActivity.class.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sample = (SampleActivity) getInstrumentation().startActivitySync(intent);
text = (TextView) sample.findViewById(R.id.mytext);
button = (Button) sample.findViewById(R.id.testbtn);
}
/*
* 垃圾清理与资源回收
* @see android.test.InstrumentationTestCase#tearDown()
*/
@Override
protected void tearDown() {
sample.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));
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 = sample.add(1, 1);
assertEquals(2, test);
}
}
3.布局文件activity_main.xml
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Instrumentation单元测试"
android:textSize="20dip"
android:textStyle="bold"
android:layout_marginTop="60dip"
android:layout_gravity="center"/>
<Button
android:id="@+id/testbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:layout_gravity="center"
android:text="测试"/>
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20dip"
android:layout_gravity="center"/>
</LinearLayout>
4.配置AndriodManifest.xml文件,将测试代码与被测试程序关联
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.unittestinstrumentation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
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=".SampleActivity"
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>
<!--表示被测试的目标包与instrumentation的名称。-->
<instrumentation android:targetPackage="com.example.unittestinstrumentation"
android:name="android.test.InstrumentationTestRunner" />
</manifest>
注意:
<uses-library android:name="android.test.runner" />放在application标签里边;
<instrumentation android:targetPackage="com.example.unittestinstrumentation"
android:name="android.test.InstrumentationTestRunner" />放在application标签外边。
5.运行测试程序,运行测试程序有2种方式
(1) 在andriodproject 上右击,run as Andriod JUnit Test,直接运行测试用例
(2)在模拟器上,运行测试用例。在模拟器中选择dev tools->“andriod.test.InstrumentationTestRunner”
来运行测试用例。这种很好,可以不需要pc运行测试用例。
注意:dev tools 真机上可能没有,需要安装