使用gradle+jacoco收集android手工代碼覆蓋率

1.背景介紹

在產(chǎn)品安卓端的測(cè)試過程中,新功能測(cè)試以及回歸測(cè)試在手工測(cè)試的情況下,即便測(cè)試用例再詳盡,因?yàn)闆]有覆蓋率等客觀數(shù)據(jù)的支持,難免可能會(huì)有疏漏之處。如果可以統(tǒng)計(jì)出手工代碼覆蓋率的情況,可以及時(shí)地補(bǔ)充測(cè)試用例。統(tǒng)計(jì)代碼覆蓋率的工具主要有Emma和Jacoco。
jacoco是Java Code Coverage的縮寫,顧名思義,是Java代碼覆蓋率統(tǒng)計(jì)的主流工具之一。關(guān)于jacoco的原理可以移步jacoco原理介紹。在安卓項(xiàng)目的代碼覆蓋率統(tǒng)計(jì)使用了jacoco的離線插樁方式,在測(cè)試前先對(duì)文件進(jìn)行插樁,然后生成插過樁的class或jar包,測(cè)試(單元測(cè)試、UI測(cè)試或者手工測(cè)試等)插過樁的class和jar包后,會(huì)生成動(dòng)態(tài)覆蓋信息到文件,最后統(tǒng)一對(duì)覆蓋信息進(jìn)行處理,并生成報(bào)告。
作為QA,統(tǒng)計(jì)代碼覆蓋率的首要問題就是,是否需要修改開發(fā)的核心源代碼。通過調(diào)研,現(xiàn)在實(shí)現(xiàn)安卓手工代碼覆蓋率的方法主要有兩大類,一類是在activity退出時(shí)增加覆蓋率的統(tǒng)計(jì)(要修改核心源代碼),一類是通過instrumentation調(diào)起被測(cè)APP,在instrumentation activity退出時(shí)增加覆蓋率的統(tǒng)計(jì)(不修改核心源代碼)。當(dāng)然還有其他諸如通過Broadcast Receiver或者Service等方法。本文采用第二類instrumentation方法實(shí)現(xiàn),構(gòu)建方式是gradle。

2.生成手工代碼覆蓋率文件

1)首先要有一個(gè)安卓工程
這里我們可以自己寫一個(gè)簡(jiǎn)單的工程,MainActivity中直接寫個(gè)Hello World文本就好。
2)在代碼主路徑下新建test文件夾,新建3個(gè)類文件



我們?cè)趈ava/<packageName>下新建test文件夾,然后在test路徑下新建三個(gè)類文件,分別是抽象類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)計(jì)開關(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)計(jì)開關(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中單獨(dú)聲明JacocoInstrumentation:

<instrumentation    
android:handleProfiling="true"    
android:label="CoverageInstrumentation"
/*這里android:name寫明此instrumentation的全稱*/
android:name="coverage.netease.com.test.JacocoInstrumentation"
/*這里android:targetPackage寫明被測(cè)應(yīng)用的包名*/
android:targetPackage="coverage.netease.com.androidcoverage"/>

5)在真機(jī)或者模擬器上運(yùn)行一下app,并點(diǎn)擊返回退出。
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后我們就可以進(jìn)行手工測(cè)試了,測(cè)試完成后點(diǎn)擊返回鍵退出app。
此時(shí),jacoco便將覆蓋率統(tǒng)計(jì)信息寫入/data/data/<yourPackageName>/files/coverage.ec文件。
接下來我們需要新增gradle task,分析覆蓋率文件生成覆蓋率html報(bào)告。

3.生成覆蓋率html報(bào)告

1)首先將coverage.ec文件拉到本地,置于指定目錄下。
我們?cè)贔ileExplorer中找到coverage.ec文件。
打開DDMS工具:


找到data/data/<yourPackageName>/files/coverage.ec文件,點(diǎn)擊左上角的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報(bào)告
執(zhí)行完gradle jacocoTestReport后,我們可以在app\build\reports\jacoco\jacocoTestReport\html目錄下看到html報(bào)告。

將index.html拖到瀏覽器中,可以看到具體的覆蓋率數(shù)據(jù)啦。


點(diǎn)擊進(jìn)去還可以看到源代碼覆蓋的情況哦。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容