package aexp.junit;
import android.app.Activity;
import android.os.Bundle;
import android.test.AndroidTestCase;
import android.test.AndroidTestRunner;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.util.Log;
import junit.framework.TestListener;
import junit.framework.Test;
import junit.framework.AssertionFailedError;
public class JUnit 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 addFailure(Test test, AssertionFailedError t)
{
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 );
}
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 );
}
}
AndroidJUnit是Android平台上进行单元测试的关键工具,它基于Java的JUnit框架,允许开发者对应用程序的代码进行自动化测试。在Android开发中,单元测试是确保代码质量、可维护性和避免引入bug的重要环节。本资料详细讲解了如何在Android环境中有效地利用JUnit进行测试。 一、JUnit简介 JUnit是一个开源的、用于Java编程语言的单元测试框架。它提供了注解(Annotation)、断言(Assertion)以及测试套件(Test Suite)等机制,使得编写和运行测试变得简单。在Android中,JUnit被用于测试单个的代码单元,如方法或类。 二、AndroidJUnit扩展 AndroidJUnit是JUnit的扩展,专门针对Android环境进行了优化。它添加了一些特性,如ActivityScenario、Espresso等,可以更方便地测试Android组件,如Activity、Service、BroadcastReceiver等。此外,AndroidJUnit还支持 Espresso UI 测试,可以进行UI层面的交互测试。 三、测试用例(Test Cases) 在JUnit中,测试用例通常是一个公共的、无参数的方法,由@Test注解标识。这个方法应该包含一系列断言,来验证被测试代码的行为。例如,你可以创建一个测试方法来检查某个函数是否返回预期的结果。 四、测试运行器(Test Runners) AndroidJUnit4是常用的测试运行器,它继承自JUnit4并添加了对Android特性的支持。使用AndroidJUnit4,可以运行在模拟器或真实设备上,并能与Android的Instrumentation框架协同工作。 五、测试套件(Test Suites) 通过@TestSuite注解,可以组合多个测试类或测试用例成为一个测试套件,一次运行多个测试。这对于组织和管理大量的测试用例非常有用。 六、测试规则(Test Rules) 测试规则是JUnit的一种机制,可以在每个测试用例执行前后执行一些操作,如清理工作、资源管理等。AndroidJUnit中的ActivityTestRule可以帮助你在测试中启动和控制Activity的生命周期。 七、Mock对象和依赖注入 在AndroidJUnit中,可以使用Mockito等库创建mock对象来隔离被测试代码和其依赖。这使得我们可以专注于测试单一功能,而不受其他组件的影响。 八、 Espresso UI 测试 Espresso是一个强大的Android UI 测试框架,可以录制并回放用户在UI上的操作,从而测试应用的交互行为。它与JUnit结合使用,可以创建强大的UI测试用例。 九、测试覆盖率 为了评估测试的质量,我们需要知道测试覆盖了多少代码。JaCoCo是一个Java代码覆盖率工具,可以与AndroidJUnit配合,提供详细的代码覆盖率报告。 十、持续集成与测试自动化 将AndroidJUnit集成到持续集成(CI)工具,如Jenkins、Travis CI或CircleCI,可以自动运行测试并在代码提交时提供反馈,确保每次更改都不会破坏现有功能。 通过学习和熟练掌握AndroidJUnit,开发者可以提高代码质量,减少bug,确保软件的稳定性和可靠性。这个资料包将帮助你深入理解Android平台上的单元测试,从基础到高级技巧,全面掌握AndroidJUnit的使用。




































































- 1

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整

- 粉丝: 77
- 资源: 34
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 计算机计算机二级C语言.pdf
- 计算机计算机二级C语言程序设计 第12讲.pdf
- 计算机计算机二级C语言程序设计第 6讲数组.pdf
- 计算机计算机二级C语言程序设计第 9讲-1.pdf
- 计算机计算机基础知识.pdf
- 计算机计算机技巧.pdf
- 计算机计算机二级C语言辅导 第二章.pdf
- 计算机计算机配件选择与组装 清单.pdf
- 计算机计算机数学-7关系的闭包运算.pdf
- 计算机计算机四级网络工程 师第3章.pdf
- 计算机计算机外文翻译1.pdf
- 计算机计算机外围设备原理.pdf
- 计算机计算机网络基础.pdf
- 计算机计算机网络试卷.pdf
- 计算机计算机性能分析与评价.pdf
- 计算机计算机系统结构基本习题和答案.pdf


