1.背景介紹
在產(chǎn)品安卓端的測試過程中,新功能測試以及回歸測試在手工測試的情況下,即便測試用例再詳盡,因為沒有覆蓋率等客觀數(shù)據(jù)的支持,難免可能會有疏漏之處。如果可以統(tǒng)計出手工代碼覆蓋率的情況,可以及時地補充測試用例。統(tǒng)計代碼覆蓋率的工具主要有Emma和Jacoco。
jacoco是Java Code Coverage的縮寫,顧名思義,是Java代碼覆蓋率統(tǒng)計的主流工具之一。關(guān)于jacoco的原理可以移步jacoco原理介紹。在安卓項目的代碼覆蓋率統(tǒng)計使用了jacoco的離線插樁方式,在測試前先對文件進行插樁,然后生成插過樁的class或jar包,測試(單元測試、UI測試或者手工測試等)插過樁的class和jar包后,會生成動態(tài)覆蓋信息到文件,最后統(tǒng)一對覆蓋信息進行處理,并生成報告。
作為QA,統(tǒng)計代碼覆蓋率的首要問題就是,是否需要修改開發(fā)的核心源代碼。通過調(diào)研,現(xiàn)在實現(xiàn)安卓手工代碼覆蓋率的方法主要有兩大類,一類是在activity退出時增加覆蓋率的統(tǒng)計(要修改核心源代碼),一類是通過instrumentation調(diào)起被測APP,在instrumentation activity退出時增加覆蓋率的統(tǒng)計(不修改核心源代碼)。當然還有其他諸如通過Broadcast Receiver或者Service等方法。本文采用第二類instrumentation方法實現(xiàn),構(gòu)建方式是gradle。
2.生成手工代碼覆蓋率文件
1)首先要有一個安卓工程
這里我們可以自己寫一個簡單的工程,MainActivity中直接寫個Hello World文本就好。
2)在代碼主路徑下新建test文件夾,新建3個類文件
我們在java/<packageName>下新建test文件夾,然后在test路徑下新建三個類文件,分別是抽象類FinishListener,Instrumentation的Activity和instrumentation類。
FinishListener:
package coverage.netease.com.test;
public interface FinishListener {
void onActivityFinished();
void dumpIntermediateCoverage(String filePath);
}
InstrumentedActivity:
package coverage.netease.com.test;
import android.util.Log;
import coverage.netease.com.androidcoverage.MainActivity;
import coverage.netease.com.test.FinishListener;
import coverage.netease.com.test.FinishListener;
public class InstrumentedActivity extends MainActivity {
public static String TAG = "IntrumentedActivity";
private coverage.netease.com.test.FinishListener mListener;
public void setFinishListener(FinishListener listener) {
mListener = listener;
}
@Override
public void onDestroy() {
Log.d(TAG + ".InstrumentedActivity", "onDestroy()");
super.finish();
if (mListener != null) {
mListener.onActivityFinished();
}
}
}
JacocoInstrumentation:
public class JacocoInstrumentation extends Instrumentation implements FinishListener {
public static String TAG = "JacocoInstrumentation:";
private static String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/coverage.ec";
private final Bundle mResults = new Bundle();
private Intent mIntent;
private static final boolean LOGD = true;
private boolean mCoverage = true;
private String mCoverageFilePath;
public JacocoInstrumentation() {
}
@Override
public void onCreate(Bundle arguments) {
Log.d(TAG, "onCreate(" + arguments + ")");
super.onCreate(arguments);
DEFAULT_COVERAGE_FILE_PATH = getContext().getFilesDir().getPath().toString() + "/coverage.ec";
File file = new File(DEFAULT_COVERAGE_FILE_PATH);
if(!file.exists()){
try{
file.createNewFile();
}catch (IOException e){
Log.d(TAG,"新建文件異常:"+e);
e.printStackTrace();}
}
if(arguments != null) {
mCoverageFilePath = arguments.getString("coverageFile");
}
mIntent = new Intent(getTargetContext(), InstrumentedActivity.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
start();
}
@Override
public void onStart() {
super.onStart();
Looper.prepare();
InstrumentedActivity activity = (InstrumentedActivity) startActivitySync(mIntent);
activity.setFinishListener(this);
}
private String getCoverageFilePath() {
if (mCoverageFilePath == null) {
return DEFAULT_COVERAGE_FILE_PATH;
} else {
return mCoverageFilePath;
}
}
private void generateCoverageReport() {
Log.d(TAG, "generateCoverageReport():" + getCoverageFilePath());
OutputStream out = null;
try {
out = new FileOutputStream(getCoverageFilePath(), false);
Object agent = Class.forName("org.jacoco.agent.rt.RT")
.getMethod("getAgent")
.invoke(null);
out.write((byte[]) agent.getClass().getMethod("getExecutionData", boolean.class)
.invoke(agent, false));
} catch (Exception e) {
Log.d(TAG, e.toString(), e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onActivityFinished() {
if (LOGD) Log.d(TAG, "onActivityFinished()");
if (mCoverage) {
generateCoverageReport();
}
finish(Activity.RESULT_OK, mResults);
}
}
3)修改Build.gradle文件
增加jacoco插件和打開覆蓋率統(tǒng)計開關(guān)
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "coverage.netease.com.androidcoverage"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
/**打開覆蓋率統(tǒng)計開關(guān)/
testCoverageEnabled = true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
4)在manifest.xml增加聲明
在<application>中聲明InstrumentedActivity:
<activity android:label="InstrumentationActivity" android:name="coverage.netease.com.test.InstrumentedActivity" />
在manifest中聲明使用SD卡權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
最后,在manifest中單獨聲明JacocoInstrumentation:
<instrumentation
android:handleProfiling="true"
android:label="CoverageInstrumentation"
/*這里android:name寫明此instrumentation的全稱*/
android:name="coverage.netease.com.test.JacocoInstrumentation"
/*這里android:targetPackage寫明被測應用的包名*/
android:targetPackage="coverage.netease.com.androidcoverage"/>
5)在真機或者模擬器上運行一下app,并點擊返回退出。
6)在命令行下通過adb shell am instrument命令調(diào)起app,具體命令是:
adb shell am instrument coverage.netease.com.androidcoverage/coverage.netease.com.test.JacocoInstrumentation
也就是adb shell am instrument <yourPackageName>/<InstrumentationName>
7)調(diào)起app后我們就可以進行手工測試了,測試完成后點擊返回鍵退出app。
此時,jacoco便將覆蓋率統(tǒng)計信息寫入/data/data/<yourPackageName>/files/coverage.ec文件。
接下來我們需要新增gradle task,分析覆蓋率文件生成覆蓋率html報告。
3.生成覆蓋率html報告
1)首先將coverage.ec文件拉到本地,置于指定目錄下。
我們在FileExplorer中找到coverage.ec文件。
打開DDMS工具:
找到data/data/<yourPackageName>/files/coverage.ec文件,點擊左上角的Pull File From Device按鈕,將該文件拖至入app根目錄/build/outputs/code-coverage/connected下(文件夾沒有的話可新建)。
2)新增gradle task,修改build.gradle文件為:
apply plugin: 'com.android.application'
apply plugin: 'jacoco'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "coverage.netease.com.androidcoverage"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled = true
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
def coverageSourceDirs = [
'../app/src/main/java'
]
task jacocoTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R*.class',
'**/*$InjectAdapter.class',
'**/*$ModuleAdapter.class',
'**/*$ViewInjector*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files("$buildDir/outputs/code-coverage/connected/coverage.ec")
doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}
}
}
3)在命令行執(zhí)行g(shù)radle jacocoTestReport
4)查看覆蓋率html報告
執(zhí)行完gradle jacocoTestReport后,我們可以在app\build\reports\jacoco\jacocoTestReport\html目錄下看到html報告。
將index.html拖到瀏覽器中,可以看到具體的覆蓋率數(shù)據(jù)啦。
點擊進去還可以看到源代碼覆蓋的情況哦。