3.
定义test
runner,以及构建UI来处理测试流程,查看测试结果等。<!--[endif]-->
MyJUnitExample.java
package com.ut.prac;
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.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyJUnitExample extends Activity {
static final String LOG_TAG = "junit";
Thread testRunnerThread = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button launcherButton = (Button)findViewById( R.id.launch_button );
launcherButton.setOnClickListener( new View.OnClickListener() {
public void onClick( View view ) {
startTest();
}
} );
}
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();
}
}
class TestDisplay implements Runnable {
public enum displayEvent {
START_TEST,
END_TEST,
ERROR,
FAILURE,
}
displayEvent ev;
String testName;
int testCounter;
int errorCounter;
int failureCounter;
TextView statusText;
TextView testCounterText;
TextView errorCounterText;
TextView failureCounterText;
public TestDisplay( displayEvent ev,
String testName,
int testCounter,
int errorCounter,
int failureCounter,
TextView statusText,
TextView testCounterText,
TextView errorCounterText,
TextView failureCounterText ) {
this.ev = ev;
this.testName = testName;
this.testCounter = testCounter;
this.errorCounter = errorCounter;
this.failureCounter = failureCounter;
this.statusText = statusText;
this.testCounterText = testCounterText;
this.errorCounterText = errorCounterText;
this.failureCounterText = failureCounterText;
}
public void run() {
StringBuffer status = new StringBuffer();
switch( ev ) {
case START_TEST:
status.append( "Starting" );
break;
case END_TEST:
status.append( "Ending" );
break;
case ERROR:
status.append( "Error: " );
break;
case FAILURE:
status.append( "Failure: " );
break;
}
status.append( ": " );
status.append( testName );
statusText.setText( new String( status ) );
testCounterText.setText( "Tests: "+testCounter );
errorCounterText.setText( "Errors: "+errorCounter );
failureCounterText.setText( "Failure: "+failureCounter );
}
}
class TestRunner implements Runnable,TestListener {
static final String LOG_TAG = "TestRunner";
int testCounter;
int errorCounter;
int failureCounter;
TextView statusText;
TextView testCounterText;
TextView errorCounterText;
TextView failureCounterText;
Activity parentActivity;
public TestRunner( Activity parentActivity ) {
this.parentActivity = parentActivity;
}
public void run() {
testCounter = 0;
errorCounter = 0;
failureCounter = 0;
statusText = (TextView)parentActivity.
findViewById( R.id.status );
testCounterText = (TextView)parentActivity.
findViewById( R.id.testCounter );
errorCounterText = (TextView)parentActivity.
findViewById( R.id.errorCounter );
failureCounterText = (TextView)parentActivity.
findViewById( R.id.failureCounter );
Log.d( LOG_TAG, "Test started" );
AndroidTestRunner testRunner = new AndroidTestRunner();
testRunner.setTest( new ExampleSuite() );
testRunner.addTestListener( this );
testRunner.setContext( parentActivity );
testRunner.runTest();
Log.d( LOG_TAG, "Test ended" );
}
// TestListener
public void addError(Test test, Throwable t) {
Log.d( LOG_TAG, "addError: "+test.getClass().getName() );
Log.d( LOG_TAG, t.getMessage(), t );
++errorCounter;
TestDisplay td = new TestDisplay(
TestDisplay.displayEvent.ERROR,
test.getClass().getName(),
testCounter,
errorCounter,
failureCounter,
statusText,
testCounterText,
errorCounterText,
failureCounterText );
parentActivity.runOnUiThread( td );
}
public void endTest(Test test) {
Log.d( LOG_TAG, "endTest: "+test.getClass().getName() );
TestDisplay td = new TestDisplay(
TestDisplay.displayEvent.END_TEST,
test.getClass().getName(),
testCounter,
errorCounter,
failureCounter,
statusText,
testCounterText,
errorCounterText,
failureCounterText );
parentActivity.runOnUiThread( td );
}
public void startTest(Test test) {
Log.d( LOG_TAG, "startTest: "+test.getClass().getName() );
++testCounter;
TestDisplay td = new TestDisplay(
TestDisplay.displayEvent.START_TEST,
test.getClass().getName(),
testCounter,
errorCounter,
failureCounter,
statusText,
testCounterText,
errorCounterText,
failureCounterText );
parentActivity.runOnUiThread( td );
}
@Override
public void addFailure(Test test, junit.framework.AssertionFailedError t) {
// TODO Auto-generated method stub
Log.d( LOG_TAG, "addFailure: "+test.getClass().getName() );
Log.d( LOG_TAG, t.getMessage(), t );
++failureCounter;
TestDisplay td = new TestDisplay(
TestDisplay.displayEvent.FAILURE,
test.getClass().getName(),
testCounter,
errorCounter,
failureCounter,
statusText,
testCounterText,
errorCounterText,
failureCounterText );
parentActivity.runOnUiThread( td );
}
}
4.
最后是xml文件<!--[endif]-->
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ut.prac"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<activity android:name=".MyJUnitExample"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Mail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/launch_button" android:text="@string/launch_test" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
<TextView android:id="@+id/testCounter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
<TextView android:id="@+id/errorCounter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
<TextView android:id="@+id/failureCounter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
最后编译,执行。。。
分享到:
相关推荐
Android[中级教程]第四章_单元测试AndroidTestCase.doc
Android review AndroidTestCase
我们曾经和大家探讨过全面剖析JavaME单元测试理念,其实在Android上... 第一步:新建一个TestCase,记得要继承androidTestCase,才能有getContext()来获取当前的上下文变量,这在Android测试中很重要的,因为很多的
主要介绍了Android中使用AndroidTestCase的方法实例,本文直接给出实现代码,需要的朋友可以参考下
androidtestcase的小例子程序,希望对需要的人有所帮助。
android之Junit 深入研究的工程代码,为我们更好的理解如何来使用androidTestCase进行单元测试开发提供了方便。
一个库jar,提供用于测试为Google Android平台编写的应用程序的API。 com.google.android/android-test/2.3.1/android-test-2.3.1.jar
Android_天气 Udacity提供的“个人Android应用程序项目”。...使用AndroidTestCase进行数据库测试 内容提供商 游标加载器 更新的用户界面 在更大的屏幕上支持多窗格 通知支持+开/关首选项 屏幕截图:
需要5个类: 1.实体类:Person.java ...5.测试类:Test.java(继承AndroidTestCase) 1.Person.java 代码如下:package com.mrzhu.sqltite; public class Person { private int _id; private String name; public
使用不同的模块,我们可以使用不同的测试套件(用于纯Java代码的JUnit,用于Android代码的AndroidTestCase) 模块API src |- main |- |- test: contains all unit tests for this module |- java: code for Unit...