在前面两篇文章分别介绍了《java单元测试》和《android单元测试》,本文是在结合这两篇文章的基础上,通过开发一个在手机上可以直接查看这些单元测试的结果的UI界面,和以及获取相关的测试信息。
界面的效果图如下:
废话少说,上代码:
1.布局文件main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
style="@android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn" />
</LinearLayout>
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/testCounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FF7F00" />
<TextView
android:id="@+id/failureCounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#33b5e5" />
<TextView
android:id="@+id/errorCounter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ff0000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:transcriptMode="normal" >
</ListView>
</LinearLayout>
</LinearLayout>
2.主界面代码:
package com.phicomme.hu;
import java.util.HashSet;
import java.util.Set;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import android.app.Activity;
import android.os.Bundle;
import android.test.AndroidTestRunner;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
* @Description 写这个类的好处是可以在手机界面上查看测试信息,
* 而且同时兼容查看android Unit Test和Junit Test的测试,
* 但是要通过套件类TestSuite将相关的测试类(包括AndroidUnit和Junit测试类)
* 添加进来,然后在AndroidTestRunner中测试TestSuite。
*
* @Creator duyong.hu
* @CreatTime 2012-12-12 下午03:48:04
* @Modifier duyong.hu
* @ModifyTime 2012-12-12 下午03:48:04
* @Remark
*
*/
public class FxUnitTestUI extends Activity
{
private static final String TAG = "Fxjunit";
Thread testRunnerThread = null;
private ArrayAdapter<String> mMsgAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button launchTest = (Button)findViewById(R.id.btn);
mMsgAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
launchTest.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.i(TAG, "setOnClickListener");
startTest();
}
});
}
//为该方法添加线程锁
private synchronized void startTest()
{
if((testRunnerThread != null) &&
!testRunnerThread.isAlive())
{
testRunnerThread = null;
Log.i(TAG, "testRunnerThread is null");
}
else if(testRunnerThread == null)
{
//添加线程驱动的任务,即TestRunner类run方法中的任务
testRunnerThread = new Thread( new TestRunner(this));
//启动线程
testRunnerThread.start();
Log.i(TAG, "testRunnerThread is not null");
}
else
{
Toast.makeText(this, "Test is still running", Toast.LENGTH_SHORT).show();
}
}
private class TestRunner implements Runnable,TestListener
{
private int testCounter;
private int failureCounter;
private int errorCounter;
private AndroidTestRunner testRunner;
private Set<String> className;
private TextView statusText;
private TextView testText;
private TextView failureText;
private TextView errorText;
ListView listView;
private Activity parentActivity;
public TestRunner(Activity activity)
{
this.parentActivity = activity;
}
@Override
public void run()
{
// TODO Auto-generated method stub
//初始化各计数
testCounter = 0;
failureCounter = 0;
errorCounter = 0;
//获取view组件
statusText = (TextView)parentActivity.findViewById(R.id.status);
testText = (TextView)parentActivity.findViewById(R.id.testCounter);
failureText = (TextView)parentActivity.findViewById(R.id.failureCounter);
errorText = (TextView)parentActivity.findViewById(R.id.errorCounter);
listView = (ListView)findViewById(R.id.list);
className = new HashSet<String>();
Log.i(TAG, "Test started");
//创建AndroidTestRunner类
testRunner = new AndroidTestRunner();
try
{
/**设置在testRunner上运行测试类套件中的多个测试类,
* 此处也可以单独运行一个测试类,如testRunner.setTest(new MathTest());
*/
testRunner.setTest(AllTest.suite());
//为testRunner添加监听
testRunner.addTestListener(this);
testRunner.setContext(parentActivity);
//运行测试
testRunner.runTest();
}
catch (RuntimeException e)
{
// TODO: handle exception
}
Log.i(TAG, "Test ended");
}
//监听测试的错误状态,进行处理
@Override
public void addError(Test test, final Throwable t)
{
// TODO Auto-generated method stub
class ErrorRunnable implements Runnable
{
@Override
public void run()
{
// TODO Auto-generated method stub
//在界面的listView中打印出测试错误的相关信息
addMsg("errorMsg:" + t + "\n" + "detail:" + testRunner.getFilteredTrace(t));
listView.setAdapter(mMsgAdapter);
}
}
//计数自增
++ errorCounter;
//创建TestDisplay对象
TestDisplay td = new TestDisplay(className,
testCounter, failureCounter, errorCounter,
statusText, testText, failureText, errorText);
/**实现线程驱动TestDisplay对象run方法中的任务,
* 此处意为将该任务放在Activity的UI线程中启动,
* 因为该对象run方法中涉及到View组件(UI组件)的操作。
*/
parentActivity.runOnUiThread(td);
//原理同上
parentActivity.runOnUiThread(new ErrorRunnable());
}
//监听失败测试状态,进行处理
@Override
public void addFailure(Test test, final AssertionFailedError t)
{
// TODO Auto-generated method stub
class FailureRunnable implements Runnable
{
@Override
public void run()
{
// TODO Auto-generated method stub
//在界面的listView中打印出测试失败的相关信息
addMsg("failureMsg:" + t + "\n" + "detail:" + testRunner.getFilteredTrace(t));
listView.setAdapter(mMsgAdapter);
}
}
Log.i(TAG, "addFailure:" + test.getClass().getName());
//Log.i(TAG, t.getMessage(), t);
Log.i(TAG, "Msg:" + t.getMessage());
//计数自增
++ failureCounter;
TestDisplay td = new TestDisplay(className,
testCounter, failureCounter, errorCounter,
statusText, testText, failureText, errorText);
parentActivity.runOnUiThread(td);
parentActivity.runOnUiThread(new FailureRunnable());
}
//监听结束测试
@Override
public void endTest(Test test)
{
// TODO Auto-generated method stub
Log.i(TAG, "endTest:" + test.getClass().getName());
className.add(test.getClass().getSimpleName());
TestDisplay td = new TestDisplay(className,
testCounter, failureCounter, errorCounter,
statusText, testText, failureText, errorText);
parentActivity.runOnUiThread(td);
}
//监听开始测试
@Override
public void startTest(Test test)
{
// TODO Auto-generated method stub
Log.i(TAG, "startTest: " + test.getClass().getSimpleName());
++ testCounter;
TestDisplay td = new TestDisplay(className,
testCounter, failureCounter, errorCounter,
statusText, testText, failureText, errorText);
parentActivity.runOnUiThread(td);
}
}
private static class TestDisplay implements Runnable
{
private Set<String> className;
private int testCounter;
private int failureCounter;
private int errorCounter;
private TextView statusText;
private TextView testText;
private TextView failureText;
private TextView errorText;
public TestDisplay(Set<String> className,
int testCounter, int failureCounter, int errorCounter,
TextView statusText, TextView testText, TextView failureText,
TextView errorText)
{
this.className = className;
this.testCounter = testCounter;
this.failureCounter = failureCounter;
this.errorCounter = errorCounter;
this.statusText = statusText;
this.testText = testText;
this.failureText = failureText;
this.errorText = errorText;
}
@Override
public void run()
{
// TODO Auto-generated method stub
//获取测试类
statusText.setText("TestClass:" + className);
Log.i(TAG, "list:" + className);
testText.setText("runTest:" + testCounter);
failureText.setText("Failure:" + failureCounter);
errorText.setText("Error:" + errorCounter);
}
}
private void addMsg(String msg)
{
mMsgAdapter.add(msg);
Log.d(TAG, "msg");
}
}
上面的代码主要是实现将测试类通过AndroidTestRunner运行测试,绑定TestListener监听,将测试相关的监听结果通过Activity展现在UI界面上。具体详细的讲述代码中都有相关的注释,这里就不再累赘。工程中的其他测试类代码这里也不在列出,相关工程的其测试的代码有兴趣的读者,可以点击以下链接进行下载:
http://download.csdn.net/detail/stevenhu_223/4891366
最后,别忘了在AndroidManifest.xml文件中进行如下配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phicomme.hu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".FxUnitTestUI"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityTest"></activity>
<uses-library android:name="android.test.runner"/>
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.phicomme.hu" />
</manifest>