云测服务 Android 云测试创建 APP 初始数据

GUO · 2013年11月11日 · 最后由 GUO 回复于 2013年12月10日 · 1604 次阅读
本帖已被设为精华帖!

很多应用测试的时候需要有点初始化数据,我看现在 MTC 或者 Testin 上面都没法让你上传自定义的文件,或者需要登录,不好弄登录的脚本之类的问题

我的解决思路是把本地应用的 sqllite 数据库和 SDCard 里面的 cache 文件打个 zip,一起打包到 apk 安装文件里。再从程序运行的时候写到云端测试机上,目前来看可以满足部分测试需求。

SqlLite 数据位置在/data/data/packet name/database 下面,用 root explorer 可以 copy 出来,然后放到 Android 项目 assets 下面,这样打 apk 包的时候这些文件不会被压缩。

下面是用到的部分代码:

还原数据库
数据库部分代码的来源 http://gundumw100.iteye.com/blog/1671352


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;


/**
 * copy数据库到apk包
 * 
 * @author NGJ
 * 
 */
public class DataBaseUtil {

    private Context context;
    public static String dbName ="sql.db";// 数据库的名字
    private static String DATABASE_PATH;// 数据库在手机里的路径

    public DataBaseUtil(Context context) {
        this.context = context;
        String packageName = context.getPackageName();
        DATABASE_PATH="/data/data/"+packageName+"/databases/";
    }

    /**
     * 判断数据库是否存在
     * 
     * @return false or true
     */
    public boolean checkDataBase() {
        SQLiteDatabase db = null;
        try {
            String databaseFilename = DATABASE_PATH + dbName;
            db = SQLiteDatabase.openDatabase(databaseFilename, null,SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {

        }
        if (db != null) {
            db.close();
        }
        return db != null ? true : false;
    }

    /**
     * 复制数据库到手机指定文件夹下
     * 
     * @throws IOException
     */
    public void copyDataBase() throws IOException {
        String databaseFilenames = DATABASE_PATH + dbName;
        File dir = new File(DATABASE_PATH);
        if (!dir.exists())// 判断文件夹是否存在,不存在就新建一个
            dir.mkdir();
        FileOutputStream os = new FileOutputStream(databaseFilenames);// 得到数据库文件的写入流
//      InputStream is = context.getResources().openRawResource(R.raw.kao);// 得到数据库文件的数据流
        AssetManager assetManager = App.getInstance().getContext().getAssets();

        InputStream is = assetManager.open(dbName);
        byte[] buffer = new byte[8192];
        int count = 0;
        while ((count = is.read(buffer)) > 0) {
            os.write(buffer, 0, count);
            os.flush();
        }
        is.close();
        os.close();
    }
}

解压 zip 包

private static void unzip() {
        try {

            AssetManager assetManager = App.getInstance().getContext()
                    .getAssets();
            InputStream is = assetManager.open("zip文件名");
            CheckedInputStream csumi = new CheckedInputStream(is, new CRC32());
            ZipInputStream in2 = new ZipInputStream(csumi);
            BufferedInputStream bi = new BufferedInputStream(in2);

            java.util.zip.ZipEntry ze;
            while ((ze = in2.getNextEntry()) != null) {
                String entryName = ze.getName();
                String decompressDir = SdcardUtils
                        .getSDCardPathWithFileSeparators() ;//+ "lvtu";

                if (ze.isDirectory()) {
                    // System.out.println("正在创建解压目录 - " + entryName);
                    File decompressDirFile = new File(decompressDir + "/"
                            + entryName);
                    if (!decompressDirFile.exists()) {
                        decompressDirFile.mkdirs();
                    }
                } else {
                    // System.out.println("正在创建解压文件 - " + entryName);
                    String decompressFile = decompressDir + "/" + entryName;
                    {
                        int idxName = decompressFile.lastIndexOf("/");
                        String dir = decompressFile.substring(0, idxName);
                        File decompDir = new File(dir);
                        if (!decompDir.exists())
                            decompDir.mkdirs();
                    }
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(decompressFile));
                    byte[] buffer = new byte[1024];
                    int readCount = bi.read(buffer);
                    while (readCount != -1) {
                        bos.write(buffer, 0, readCount);
                        readCount = bi.read(buffer);
                    }
                    bos.close();
                }
            }

            bi.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
共收到 5 条回复 时间 点赞

这个还原数据库在这里是什么意思,使用的时机是什么时候?

GUO #2 · 2013年11月11日 Author

#1 楼 @lihuazhang 云测试平台只能上传 APK 的安装包,如果需要一些初始数据,比如音乐类的需要缓存一些歌曲啥的,可以这样搞上去,相当于安装了一个程序的快照.

能把使用场景再介绍的详细点吗?据我所知,云测试都是些安装/卸载,稳定性(monkey),兼容性测试,怎么走到你这个场景上呢?

GUO #5 · 2013年12月10日 Author

#3 楼 @whuiscool 哦 其实这个也就是在 monkey 或者遍历测试的时候有用,好像 MTC 就有遍历测试.这个主要是在安装的时候,创建点 APP 的初始数据,避免在点击打开一个列表界面的时候里面都是空的.

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册