android单元测试

在实际开发中,开发android应用程序的过程中是需要不断的进行单元测试的,使用JUnit测试框架,是正规android开发比用技术,良好的测试习惯,一是能减少后期维护 和增强软件的健壮性。在JUnit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。
   其实android中也是扩展了JUnit,派生出好几个类倾向于不同情况下的测试。这一点与Spring中初始化容器相似,你可以通过BeanFactory 也可以通过 ApplicationContext 来完成。只不过他们的倾向点不同。在android中你可以使用这些类来完成单元测试:
Test—TestCase—AndroidTestCase :多用于对业务逻辑的单元测试
Test—TestCase—InstrumentationTestCase :用于测试与组件交互的功能
Test—TestSuite—InstrumentationTestSuite :一组测试用例
TestListener——BaseTestRunner—AndroidTestRunner
Instrumentation—InstrumentationTestRunner
我们常用到的一般是前前两个,你会发现他们的基类都是Test。只不过各自的应用场景不同。
第一步:在AndroidManifest.xml中加入下面蓝色代码:
<!--EndFragment-->
Xml代码  收藏代码
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
        <!-- 使用的类库 --> 
        <span style="color: #0000ff;"><uses-library android:name="android.test.runner"/></span> 
        <activity android:name="com.iteye.androidtoast.JUnitActivity" 
                  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> 
    <!-- 这里的 targetPackage的内容与上面package内容需相同。表示该测试运行在此包下,说白了就是在同一个进程 --> 
    <span style="color: #0000ff;"><instrumentation android:name="android.test.InstrumentationTestRunner" 
     android:targetPackage="com.iteye.androidtoast" android:label="Tests for My App" /></span> 
第二步,看代码
  首先是AndroidTestCase的简单应用
 
Java代码  收藏代码
package com.iteye.service.tests; 
 
import junit.framework.Assert; 
import android.test.AndroidTestCase; 
import android.util.Log; 
 
import com.iteye.service.SomeService; 
/**
* 需要测试类要继承AndroidTestCase
* AndroidTestCase 多用于对系统中业务逻辑的测试
* 需要与界面交互的测试一般采用InstrumentationTestCase
* @author androidtoast
*
*/ 
public class SomeServiceTest extends AndroidTestCase { 
     
    private static final String TAG="SomeServiceTest"; 
    SomeService some; 
    protected int a; 
    protected int b; 
     
    //初始化测试环境 在实例化当前类的时候自动调用此方法 
    @Override 
    protected void setUp() throws Exception { 
            super.setUp(); 
            some=new SomeService(); 
            a=3; 
            b=8; 
    } 
 
    //测试结束后调用此方法,用于清理测试环境中得变量 
    @Override 
    protected void tearDown() throws Exception { 
        super.tearDown(); 
        Log.i(TAG, "Test Over!"); 
    } 
 
    //测试getAdd方法 
    public void testAdd()throws Exception{ 
        Log.d(TAG, "testAdd"); 
        int result=some.getAdd(a, b); 
        Assert.assertEquals(11, result); 
    } 


  InstrumentationTestCase应用代码:
   
Java代码  收藏代码
package com.iteye.androidtoast.tests; 
 
import com.iteye.androidtoast.JUnitActivity; 
import com.iteye.androidtoast.R; 
 
import android.content.Intent; 
import android.os.SystemClock; 
import android.test.InstrumentationTestCase; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView;  
/**
* InstrumentationTestCase多用于测试与组件相关的操作
* @author androidtoast
*
*/ 
public class JUnitActivityTest extends InstrumentationTestCase { 
 
    JUnitActivity mActivityTested; 
     
    public JUnitActivityTest() { 
    } 
 
    /**
     * 初始化测试环境
     */ 
    @Override 
    protected void setUp() throws Exception { 
        super.setUp(); 
        //意图用于激活Activity 
        Intent intent = new Intent(); 
        //设置用于激活哪个Activity 
        intent.setClassName("com.iteye.androidtoast", JUnitActivity.class.getName()); 
        //启动一个新的任务 并在后台运行 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        //获得Instrumentation 启动一个活动 
        mActivityTested = (JUnitActivity) getInstrumentation().startActivitySync(intent); 
    } 
 
    //清理资源  
    @Override 
    protected void tearDown() throws Exception { 
        mActivityTested.finish();//测试完成后关闭Activity 
        super.tearDown(); 
    } 
    //测试方法 (其实就是一个点击按钮 然后隐藏自身显示文本这么一简单功能) 
    public void testClickButtonToShowText() throws Exception { 
        TextView tv = (TextView) mActivityTested.findViewById(R.id.text); 
        SystemClock.sleep(2000);//等待两秒 
        //如果当前的TextView的状态是隐藏的则正确通过 
        assertEquals("TextView should be Gone before Button Clicking", 
                View.GONE, tv.getVisibility()); 
         
        Button btn = (Button) mActivityTested.findViewById(R.id.button); 
        //在主线程里执行点击按钮这一动作 
        getInstrumentation().runOnMainSync(new PerformClick(btn)); 
        SystemClock.sleep(2000); 
        assertEquals("TextView should be Visible after Button Clicking", 
                View.VISIBLE, tv.getVisibility());       
    } 
     
    private class PerformClick implements Runnable { 
        Button mBtnClicked; 
         
        public PerformClick(Button button) { 
            mBtnClicked = button; 
        } 
         
        public void run() { 
            mBtnClicked.performClick(); 
        } 
    } 
 
}

猜你喜欢

转载自xiaofei2157094.iteye.com/blog/1990015