Android测试,测试activity,使用InstrumentationTestCase类
测试后台逻辑,使用 AndroidTestCase 类
Android单元测试,主要目的是为了保证复杂逻辑是否正常正确,以及依据TDD的原则,测试先于编码,理清代码编写的思路。
主要以AndroidTestCase类测试复杂逻辑来讲解:
以android sqlite数据库逻辑代码的测试为例。
数据库逻辑代码:
import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Random;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.DatabaseUtils;import android.database.sqlite.SQLiteDatabase;import com.android.hanhan.R;public class DatabaseService { private DatabaseHelper dbOpenHelper; protected static final String TBL_NAME = "article"; protected static final String FIELD_ID = "id"; protected static final String FIELD_TITLE = "title"; protected static final String FIELD_CONTENT = "content"; protected static final String FIELD_DELETE = "deleted"; protected static final String FIELD_PUBLISHDATE = "publishdate"; protected static final String FIELD_FAVORITE = "favorite"; protected static final String FIELD_CLICKCOUNT = "clickcount"; protected static final String FIELD_FAVORITEDATE = "favoritedate";//构造器,初始数据库服务 public DatabaseService(Context context) { dbOpenHelper = new DatabaseHelper(context); }// 删除表 public void dropTable(String taleName) { dbOpenHelper.getWritableDatabase().execSQL( "DROP TABLE IF EXISTS " + taleName); }// 关闭数据库 public void closeDB() { dbOpenHelper.getWritableDatabase().close(); }// 取得数据库TBL_NAME 表的所有数据 public List<Map<String, Object>> fetchALLArticle() { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Cursor cur = db.query(TBL_NAME, new String[] {FIELD_ID, FIELD_TITLE, FIELD_CONTENT, FIELD_PUBLISHDATE,FIELD_FAVORITE ,FIELD_DELETE }, null, null, null, null, null); list = getListFromDb(list, cur); return list; }// 取得数据库TBL_NAME表的数据个数 public long getPageCount(){ SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); return DatabaseUtils.queryNumEntries(db, TBL_NAME); }// 根据分页尺寸和页数,取得数据列表 public List<Map<String, Object>> fetchCustomArticle(long pageSize, long pageID) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); String sql = "select * from " + TBL_NAME + " Limit "+String.valueOf((pageID-1)*pageSize)+" ,"+String.valueOf((pageID)*pageSize); Cursor cur = db.rawQuery(sql, null); list = getListFromDb(list, cur); return list; }//取得数据列表方法 private List<Map<String, Object>> getListFromDb(List<Map<String, Object>> list, Cursor cur) { if(cur.moveToFirst()){ do{ Map<String, Object> map = new HashMap<String, Object>(); map = addValueToMap(cur); list.add(map); }while(cur.moveToNext()); } return list; }// 将数据映射到map private Map<String, Object> addValueToMap(Cursor cur) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", cur.getString(cur.getColumnIndex(FIELD_ID))); map.put("title", cur.getString(cur.getColumnIndex(FIELD_TITLE))); map.put("info", cur.getString(cur.getColumnIndex(FIELD_PUBLISHDATE))); map.put("content", cur.getString(cur.getColumnIndex(FIELD_CONTENT))); map.put("publishdate", cur.getString(cur.getColumnIndex(FIELD_PUBLISHDATE))); map.put("favorite", cur.getString(cur.getColumnIndex(FIELD_FAVORITE))); map.put("delete", cur.getString(cur.getColumnIndex(FIELD_DELETE))); map.put("img", R.drawable.stop); return map; } }
数据库单元测试代码,测试数据取得所有数据方法和 getPageCount()方法
import android.test.AndroidTestCase;import com.android.hanhan.util.DatabaseService;public class DatabaseServiceTest extends AndroidTestCase{ private DatabaseService dbs; @Override protected void setUp() throws Exception { dbs = new DatabaseService(getContext()); } @Override protected void tearDown() throws Exception { dbs.closeDB(); } public void testPageCount() throws Exception{ assertEquals(12, dbs.getPageCount()); } public void testFetchALLArticle() throws Exception{ assertEquals(12, dbs.fetchALLArticle().size()); } }
如果为绿色,则通过;红色,表示不通过
如出错,android junit test会有详细的错误信息,方便更正,更能确保程序的健壮
当然需要配置AndroidManifest.xml 文件
<!-- android 单元测试需要添加的内容 --><uses-library android:name="android.test.runner" /><instrumentation android:targetPackage="com.android.abc" android:label="Test Sqlite" android:name="android.test.InstrumentationTestRunner"></instrumentation>