使用 Jacoco 實現 Android 端手工測試覆蓋率統計

背景

前段時間在研究手工測試覆蓋率問題,嘗試將結果記錄下來。有什麼問題歡迎同學指正. : )

  • 由於現在單元測試在我們這小公司無法推行,且為了解決新功能測試以及迴歸測試在手工測試的情況下,即便用例再為詳盡,也會存在遺漏的用例。通過統計手工測試覆蓋率的數據,可以及時的完善用例。 經過了解準備使用Jacoco完成這個需求.Jacoco是Java Code Coverage的縮寫,在統計完成Android代碼覆蓋率的時候使用的是Jacoco的離線插樁方式,在測試前先對文件進行插樁,在手工測試過程中會生成動態覆蓋信息,最後統一對覆蓋率進行處理,並生成報告;通過了解現在實現Android覆蓋率的方法主要有兩種方式,一是通過activity退出的時候添加覆蓋率的統計,但是這種情況會修改app的源代碼。另外一種是使用的是Android測試框架Instrumentation。這次需求的實現使用的是Instrumentation.。

實現

1. 將3個類文件放入項目test文件夾;

使用 Jacoco 實現 Android 端手工測試覆蓋率統計


  • 具體各個類的代碼如下:

FinishListener:

package 你的包名;
public interface FinishListener {
void onActivityFinished();
void dumpIntermediateCoverage(String filePath);
}

InstrumentedActivity:

package你的包名;
import 你的啟動的activity;
import android.util.Log;

public class InstrumentedActivity extends MainActivity {
public static String TAG = "InstrumentedActivity";

private你的包名.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:

package 包名.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;

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;


/**
* Constructor
*/
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() {
if (LOGD)
Log.d(TAG, "onStart()");
super.onStart();

Looper.prepare();
InstrumentedActivity activity = (InstrumentedActivity) startActivitySync(mIntent);
activity.setFinishListener(this);
}

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();
}
}
}
}

private String getCoverageFilePath() {

if (mCoverageFilePath == null) {
return DEFAULT_COVERAGE_FILE_PATH;
} else {
return mCoverageFilePath;
}
}

private boolean setCoverageFilePath(String filePath){
if(filePath != null && filePath.length() > 0) {
mCoverageFilePath = filePath;
return true;
}
return false;
}


@Override
public void onActivityFinished() {
if (LOGD)
Log.d(TAG, "onActivityFinished()");
if (mCoverage) {
generateCoverageReport();
}
finish(Activity.RESULT_OK, mResults);
}

@Override
public void dumpIntermediateCoverage(String filePath){
// TODO Auto-generated method stub
if(LOGD){
Log.d(TAG,"Intermidate Dump Called with file name :"+ filePath);
}
if(mCoverage){
if(!setCoverageFilePath(filePath)){
if(LOGD){
Log.d(TAG,"Unable to set the given file path:"+filePath+" as dump target.");
}
}
generateCoverageReport();
setCoverageFilePath(DEFAULT_COVERAGE_FILE_PATH);
}
}

}

2. 修改build.gradle文件

  • 增加Jacoco插件,打開覆蓋率統計開關,生成日誌報告.

添加的代碼內容:

apply plugin: 'jacoco'

jacoco {
toolVersion = "0.7.9"
}
android {
buildTypes {
debug { testCoverageEnabled = true
/**打開覆蓋率統計開關/
}
}

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/flavors/coverage.ec")

doFirst {
new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
if (file.name.contains('$$')) {
file.renameTo(file.path.replace('$$', '$'))
}
}

}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

3. 修改AndroidManifest.xml文件
添加以及修改部分:

<uses-permission>
<activity>
<instrumentation> android:handleProfiling="true"
android:label="CoverageInstrumentation"
android:name="包名.test.JacocoInstrumentation"
android:targetPackage="包名"/>
/<instrumentation>

4. 我們需要通過adb shell am instrument 包名/包名.test.JacocoInstrumentation 啟動app;

5. 進行app手工測試,測試完成後退出App,覆蓋率文件會保存在手機/data/data/yourPackageName/files/coverage.ec目錄

6. 導出coverage.ec使用gradle jacocoTestReport分析覆蓋率文件並生成html報告

7. 查看覆蓋率html報告

  • app\\build\\reports\\jacoco\\jacocoTestReport\\html目錄下看到html報告


使用 Jacoco 實現 Android 端手工測試覆蓋率統計


  • 打開index.html,就可以看到具體的覆蓋率數據了


使用 Jacoco 實現 Android 端手工測試覆蓋率統計



分享到:


相關文章: