如何单元测试一个android library
android dev官方网站上, 对于测试一个android库, 有如下2个建议.
1. You can set up a test project that instruments an application project that depends on the library project. You can then add tests to the project for library-specific features.
(需要有一个app工程, 依赖了这个android 库工程, 那么你写一个android工程,测试这个app. 这个方法, 和传统的测试app的方法一致.)
2. You can set up a set up a standard application project that depends on the library and put the instrumentation in that project. This lets you create a self-contained project that contains both the tests/instrumentations and the code to test.
(这个是说, 你直接测试这个android 库工程. 你需要创建一个标准的android app工程依赖于这个android库, 然后进行instrumentation. 这相当于说, 你的这个测试工程, 是一个标准app工程+测试工程的集合体). 反过来说, 我们认为是一个测试工程, 里面加上了标准android app的定义. 那么就相当于, 自己测自己. 但实际因为依赖了三方的android库, 所以, 实际测的是android库代码.
有点拗口了. 我们来看个实际的例子吧.
1. 首先,我们创建一个android lib 工程. 这里我直接使用eclipse+adt进行的. 大家也可以直接用android 命令行的方式.
eclipse -> new project-> android libraryproject
2. 看一下这个android lib的manifest文件.
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android" package="com.whoistester.lib" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > </application> </manifest>
看看project.properties 文件,可以看出这个工程是一个android lib
# Project target.
target=android-17
android.library=true
3. 好了我们随便在这个库里, 写个class和2个方法吧.
package com.whoistester.lib;
public class Testlib { // 这是库的代码哦。 非测试代码。
public void test1 (boolean con)
{
if(con)
{
System.out.println(“test1 true”);
}
else{
System.out.println(“test1 false”);
}
}
public void test2 (boolean con)
{
if(con)
{
System.out.println(“test1 true”);
}
else{
System.out.println(“test1 false”);
}
}
}
4. 好了, 我们有了android lib。 我们要对他进行单元测试了。
接下来我们新建一个android test 工程。 并选择上,依赖上面的那个android 库。
new project -> android test project
5. 看下manifest文件,并修改。
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.whoistester.lib.test” // 我们这个测试apk的包名
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk android:minSdkVersion=”8″ />
<instrumentation
android:name=”android.test.InstrumentationTestRunner”
android:targetPackage=”com.whoistester.lib.test” /> // 因为我们没有被测apk, 所以我们的测试apk和被测apk是一个。 这是一个欺骗他的做法。
<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” >
<uses-library android:name=”android.test.runner” />
</application>
</manifest>
看下project.properties.
target=android-17
android.library.reference.1=../test-android-lib (这个是说明,我们的这个测试apk 要用到这个测试库代码, 这样我们才能测试这个库的嘛。) 文件夹的名字, 以你的实际路径为准,需要修改。
6 写case吧。 我们的case必须位于com.whoistester.lib.test 包下哦。因为我们在manifest里指定了我们这个测试apk的package 名字。
package com.whoistester.lib.test;
import android.test.AndroidTestCase;
import com.whoistester.lib.*; // import 近来android lib的代码
public class TestlibTest extends AndroidTestCase {
public void test1()
{
(new Testlib()).test1(true);
assertTrue(true);
}
public void test2()
{
(new Testlib()).test1(false);
}
}
7. 运行android junit test。 结果便有了。
此篇文章已被阅读3185 次