Android Junit单元测试4部曲
作者:网络转载 发布时间:[ 2013/7/19 13:47:54 ] 推荐标签:
第三步:新建一个Activity,用来启动单元测试,并显示测试结果。系统的AndroidTestRunner竟然什么连个UI界面也没有实现,这里只是简单的实现了一个
Java代码
public class TestActivity extends Activity {
private TextView resultView;
private TextView barView;
private TextView messageView;
private Thread testRunnerThread;
private static final int SHOW_RESULT = 0;
private static final int ERROR_FIND = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView = (TextView)findViewById(R.id.ResultView);
barView = (TextView)findViewById(R.id.BarView);
messageView = (TextView)findViewById(R.id.MessageView);
Button lunch = (Button)findViewById(R.id.LunchButton);
lunch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTest();
}
});
}
private void showMessage(String message) {
hander.sendMessage(hander.obtainMessage(ERROR_FIND, message));
}
private void showResult(String text) {
hander.sendMessage(hander.obtainMessage(SHOW_RESULT, text));
}
private synchronized void startTest() {
if (testRunnerThread != null
&& testRunnerThread.isAlive()) {
testRunnerThread = null;
}
if (testRunnerThread == null) {
testRunnerThread = new Thread(new TestRunner(this));
testRunnerThread.start();
} else {
Toast.makeText(this,
"Test is still running",
Toast.LENGTH_SHORT).show();
}
}
public Handler hander = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_RESULT:
resultView.setText(msg.obj.toString());
break;
case ERROR_FIND:
messageView.append(msg.obj.toString());
barView.setBackgroundColor(Color.RED);
break;
default:
break;
}
}
};
class TestRunner implements Runnable, TestListener {
private Activity parentActivity;
private int testCount;
private int errorCount;
private int failureCount;
public TestRunner(Activity parentActivity) {
this.parentActivity = parentActivity;
}
@Override
public void run() {
testCount = 0;
errorCount = 0;
failureCount = 0;
ExampleSuite suite = new ExampleSuite();
AndroidTestRunner testRunner = new AndroidTestRunner();
testRunner.setTest(suite);
testRunner.addTestListener(this);
testRunner.setContext(parentActivity);
testRunner.runTest();
}
@Override
public void addError(Test test, Throwable t) {
errorCount++;
showMessage(t.getMessage() + "
");
}
@Override
public void addFailure(Test test, AssertionFailedError t) {
failureCount++;
showMessage(t.getMessage() + "
");
}
@Override
public void endTest(Test test) {
showResult(getResult()); }
@Override
public void startTest(Test test) {
testCount++;
}
private String getResult() {
int successCount = testCount - failureCount - errorCount;
return "Test:" + testCount + " Success:" + successCount + " Failed:" + failureCount + " Error:" + errorCount;
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11