黑马程序员------------------------Android 单元测试

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

我在学习Android的时候最头特的就是 测试! 虚拟机慢有不舒服,真机也是觉得很慢,因为要频繁的安装应用,所以Android 单元测试的使用就显得尤为重要了。

想实现简单的单元测试不是很难,只要几步就可以完成了:

 

首先要在清单文件(AndroidManifest.xml)下进行如下声明:

  <!-- 测试环境的指令 放在application节点的外面-->
     <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>
<!-- 测试环境的指令
放在application节点的里面与activity同级--><uses-library android:name="android.test.runner"/>
可参照如下声明:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tai.mylistview"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>
       <!-- 测试环境的指令 -->
    <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <!-- 测试环境的指令 -->
    <uses-library android:name="android.test.runner"/>
        <activity
            android:name="com.tai.mylistview.MainActivity"
            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="com.tai.mylistview.SimpleAdapter_Act"></activity>
        <activity android:name="com.tai.mylistview.CheckImage"></activity>
        <activity android:name="com.tai.mylistview.DataBase"></activity>
        
        
     <provider 
         android:name="com.tai.provider.PersonDBProvider"
         android:authorities="com.tai.mylistview.personProvider"/>
    </application>

</manifest>

再就是声明一个类,这个类要继承自AndroidTestCase,剩下的就喝Junit是一样的。

下面这就是对上篇中的数据库操作的测试方法:

package com.tai.test;

import com.person.dao.PersonDao;
import com.person.dao.PersonDao2;
import com.tai.db.PersonDBOpenHelper;

import android.test.AndroidTestCase;

public class Test extends AndroidTestCase {

    public void testCreateDb() throws Exception
    {
        //getContext() 是android 测试类 androidTestCase 提供的方便得到测试 上下文的API
        PersonDBOpenHelper helper = new PersonDBOpenHelper(getContext());
        helper.getReadableDatabase();
    }
    
    private PersonDao2 dao;
    //当测试框架准备好的时候才会执行  一般都坐初始化操作
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        dao = new PersonDao2(getContext());
    }
    //当测试方法执行完之后执行的方法  一般都做些擦屁股的操作
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }
    
    public void testAdd() throws Exception
    {
        boolean result = dao.add("张三","999");
        assertEquals(true, result);
    }
    
    public void testFind() throws Exception
    {
        boolean result = dao.find("张三");
        assertEquals(true, result);
    }
    
    public void testUpdate() throws Exception
    {
        boolean result = dao.update("110","张三");
        assertEquals(true, result);
    }
    public void testDelete() throws Exception
    {
        boolean result = dao.delete("张三");
        assertEquals(true, result);
    }
}

test

下面这两个方法很实用。

//当测试框架准备好的时候才会执行 一般都坐初始化操作
@Override
protected void setUp() throws Exception {
super.setUp();
dao = new PersonDao2(getContext());
}
//当测试方法执行完之后执行的方法 一般都做些擦屁股的操作
@Override
protected void tearDown() throws Exception {
super.tearDown();
}

 

记得再写测试方法的时候想单元框架抛出异常!!

OK简单的测试就这样可以用了。

 

还有最懒的一个方法  直接生成一个测试工程!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值