新建测试项目
- Name: "HelloAndroidTest". 测试类名
- Superclass: "
android.test.ActivityInstrumentationTestCase2<HelloAndroid>
".//HelloAndorid为我们要测试的类名
package com.example.helloandroid.test;
import com.example.helloandroid.HelloAndroid;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;
public class HelloAndroidTest extends ActivityInstrumentationTestCase2<HelloAndroid> {
private HelloAndroid mActivity; // the activity under test
private TextView mView; // the activity's TextView (the only view)
private String resourceString;
//引入所要测试的类
public HelloAndroidTest() {
super("com.example.helloandroid", HelloAndroid.class);
}
//初始化
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
mView = (TextView) mActivity.findViewById(com.example.helloandroid.R.id.textview);
resourceString = mActivity.getString(com.example.helloandroid.R.string.hello);
}
//测试元素是否定义
public void testPreconditions() {
assertNotNull(mView);
}
//测试test值是否相符
public void testText() {
assertEquals(resourceString,(String)mView.getText());
}
}
一个简单的测试示例,供参考!