背景
前段时间在研究手工测试覆盖率问题,尝试将结果记录下来。有什么问题欢迎同学指正. : )
- 由于现在单元测试在我们这小公司无法推行,且为了解决新功能测试以及回归测试在手工测试的情况下,即便用例再为详尽,也会存在遗漏的用例。通过统计手工测试覆盖率的数据,可以及时的完善用例。 经过了解准备使用Jacoco完成这个需求.Jacoco是Java Code Coverage的缩写,在统计完成Android代码覆盖率的时候使用的是Jacoco的离线插桩方式,在测试前先对文件进行插桩,在手工测试过程中会生成动态覆盖信息,最后统一对覆盖率进行处理,并生成报告;通过了解现在实现Android覆盖率的方法主要有两种方式,一是通过activity退出的时候添加覆盖率的统计,但是这种情况会修改app的源代码。另外一种是使用的是Android测试框架Instrumentation。这次需求的实现使用的是Instrumentation.。
实现
1. 将3个类文件放入项目test文件夹;
- 具体各个类的代码如下:
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 android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <activity android:label="InstrumentationActivity" android:name="包名.test.InstrumentedActivity" /> <instrumentation android:handleProfiling="true" android:label="CoverageInstrumentation" android:name="包名.test.JacocoInstrumentation" android:targetPackage="包名"/>
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报告
- appbuildreportsjacocojacocoTestReporthtml目录下看到html报告
- 打开index.html,就可以看到具体的覆盖率数据了