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>