使用Instrumentation 进行自动化测试
明确问题:
假设需要测试的类是MainActivity,里面有一个EditText 和一个Button,通过用户输入的内容,点Button 执行不同的Toast提示,要怎么进行
单元测试来检查这个功能是否正常呢?
实现过程:
1.MainActivity的实现非常简单,核心代码就几行
mButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String msg = mEditText.getText().toString();
if (msg.equals("1")) {
Toast.makeText(getApplicationContext(), "hello_1", Toast.LENGTH_LONG).show();
} else if(msg.equals("2")){
Toast.makeText(getApplicationContext(), "hello_2", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "hello_nothing", Toast.LENGTH_SHORT).show();
}
}
});
2.编写自动测试的代码,右键工程MyAndroidUnitTestDemo ->new ->other->Android Test Project填写工程名然后select Test Target
(这里就选MyAndroidUnitTestDemo)。
3.然后项目列表中就生成了一个用于测试其他代码的工程,打开它的AndroidManiFest.xml,可以看到它自动加了
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.myandroidunittestdemo" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application> 这样的代码,android:targetPackage="com.example.myandroidunittestdemo"这个目标包名就是我们要进行测试的
MyAndroidUnitTestDemo的包名了,太吊了,是不是?
4.接下来就是编写测试代码了,写一个类继承自InstrumentationTestCase 自动向编辑框中输入1,2,3然后分别自动点击按钮 代码如下
public class TestUnitTestActivity extends InstrumentationTestCase{
final String TAG = "TestHelloAppTestHelloApp";
Button mHelloTestButton;
EditText mHelloEditText;
MainActivity mHelloTestActivity;
Instrumentation mInstrumentation;
public void testUnitActivity() {
Log.e(TAG, "call testHelloActivity()");
mHelloTestButton = (Button)mHelloTestActivity.findViewById(R.id.Button1);
mHelloEditText = (EditText)mHelloTestActivity.findViewById(R.id.EditText1);
for (int i = 0; i < 3; i++) {
//设置事件在主线程中执行
mInstrumentation.runOnMainSync(new Click(mHelloTestButton,mHelloEditText,Integer.toString(i)));
SystemClock.sleep(3000);
}
}
public void testUnitActivity2() {
}
private class Click implements Runnable{
Button button;
EditText editText;
String str;
Click(Button b,EditText e,String s){
button = b;
editText = e;
str = s;
}
@Override
public void run() {
editText.setText(str);
button.performClick();
}
}
//负责testcase开始前的初始化工作
@Override
protected void setUp() throws Exception {
super.setUp();
Log.e(TAG, "call setUp()");
mInstrumentation = getInstrumentation();
Intent intent = new Intent();
intent.setClassName("com.example.myandroidunittestdemo", "com.example.myandroidunittestdemo.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//通过intent触发activity
mHelloTestActivity = (MainActivity)mInstrumentation.startActivitySync(intent);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
Log.e(TAG, "tearDown()");
}
}
注意:1.测试的方法testUnitActivity 不能和类名一致(会与构造方法一样)
2.如果 mHelloTestButton = (Button)mHelloTestActivity.findViewById(R.id.Button1);
这行提示R文件找不到或者id找不到,就只能手动import MainActivity 的R文件啦。
3.setUp() 方法内的intent.setClassName();包名不能搞错。
5.右键测试工程,点击->run as->android JUnit test 等待安装运行后,eclipse Package Expoler 旁边会出现JUnit 一栏,里面可以看到
自动测试的结果。
使用Instrumentation 进行自动化测试差不多就是这样一个过程了,以后发现什么好的测试框架再补充。