[android]android自動化測試十一之程式碼覆蓋率報告EMMA
以下程式碼時監聽測試程式結束後將Emma測試報告寫入到虛擬機器上,然後可以使用adb pull命令傳送至硬碟上,這裡需要測試程式整合Emma jar包,並需要修改ant build檔案。
定義finish介面
修改ant build.xml檔案
https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project
http://dtmilano.blogspot.com/search/label/android
package com.example.instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.example.i2at.tc.TemperatureConverterActivity;
//import com.vladium.emma.rt.RT;
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 EmmaInstrumentation extends Instrumentation implements FinishListener {
private static final String TAG = "EmmaInstrumentation";
private static final boolean LOGD = true;
private static final String DEFAULT_COVERAGE_FILE_PATH = "/mnt/sdcard/coverage.ec";
private final Bundle mResults = new Bundle();
private Intent mIntent;
private boolean mCoverage = true;
private String mCoverageFilePath;
/**
* Extends the AUT to provide the necessary behavior to invoke the
* {@link FinishListener} that may have been provided using
* {@link #setFinishListener(FinishListener)}.
*
* It's important to note that the original Activity has not been modified.
* Also, the Activity must be declared in the
* <code>AndroidManifest.xml</code> because it is started by an intent in
* {@link EmmaInstrumentation#onStart()}. This turns more difficult to use
* other methods like using template classes. This latter method could be
* viable, but all Activity methods should be re-written to invoke the
* template parameter class corresponding methods.
*
* @author diego
*
*/
public static class InstrumentedActivity extends
TemperatureConverterActivity {
private FinishListener mListener;
public void setFinishListener(FinishListener listener) {
mListener = listener;
}
@Override
public void finish() {
if (LOGD)
Log.d(TAG + ".InstrumentedActivity", "finish()");
super.finish();
if (mListener != null) {
mListener.onActivityFinished();
}
}
}
/**
* Constructor
*/
public EmmaInstrumentation() {
}
@Override
public void onCreate(Bundle arguments) {
if (LOGD)
Log.d(TAG, "onCreate(" + arguments + ")");
super.onCreate(arguments);
if (arguments != null) {
mCoverage = getBooleanArgument(arguments, "coverage");
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 boolean getBooleanArgument(Bundle arguments, String tag) {
String tagString = arguments.getString(tag);
return tagString != null && Boolean.parseBoolean(tagString);
}
private void generateCoverageReport() {
if (LOGD)
Log.d(TAG, "generateCoverageReport()");
java.io.File coverageFile = new java.io.File(getCoverageFilePath());
// We may use this if we want to avoid refecltion and we include
// emma.jar
// RT.dumpCoverageData(coverageFile, false, false);
// Use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod(
"dumpCoverageData", coverageFile.getClass(), boolean.class,
boolean.class);
dumpCoverageMethod.invoke(null, coverageFile, false, false);
} catch (ClassNotFoundException e) {
reportEmmaError("Is emma jar on classpath?", e);
} catch (SecurityException e) {
reportEmmaError(e);
} catch (NoSuchMethodException e) {
reportEmmaError(e);
} catch (IllegalArgumentException e) {
reportEmmaError(e);
} catch (IllegalAccessException e) {
reportEmmaError(e);
} catch (InvocationTargetException e) {
reportEmmaError(e);
}
}
private String getCoverageFilePath() {
if (mCoverageFilePath == null) {
return DEFAULT_COVERAGE_FILE_PATH;
} else {
return mCoverageFilePath;
}
}
private void reportEmmaError(Exception e) {
reportEmmaError("", e);
}
private void reportEmmaError(String hint, Exception e) {
String msg = "Failed to generate emma coverage. " + hint;
Log.e(TAG, msg, e);
mResults.putString(Instrumentation.REPORT_KEY_STREAMRESULT, "\nError: "
+ msg);
}
/* (non-Javadoc)
* @see com.example.instrumentation.FinishListener#onActivityFinished()
*/
@Override
public void onActivityFinished() {
if (LOGD)
Log.d(TAG, "onActivityFinished()");
if (mCoverage) {
generateCoverageReport();
}
finish(Activity.RESULT_OK, mResults);
}
}
定義finish介面
package com.example.instrumentation;
/**
* Listen for an Activity to finish and invokes {@link #onActivityFinished()} when this happens.
*
* @author diego
*
*/
public interface FinishListener {
/**
* Invoked when the Activity finishes.
*/
void onActivityFinished();
}
修改ant build.xml檔案
<!-- It only instruments class files, not any external libs -->
<emma enabled="true">
<instr verbosity="${verbosity}"
mode="overwrite"
instrpath="${out.absolute.dir}/classes"
outdir="${out.absolute.dir}/classes">
<!-- DTM: 2011-12-23: added filter for R -->
<filter excludes="*.R" />
<filter excludes="*.R$*" />
</instr>
<!-- TODO: exclusion filters on R*.class and allowing custom exclusion from
user defined file -->
</emma>
https://wiki.jenkins-ci.org/display/JENKINS/Building+an+Android+app+and+test+project
http://dtmilano.blogspot.com/search/label/android
相關文章
- 使用EMMA獲取Android測試覆蓋率Android
- EMMA 覆蓋率工具
- 程式碼覆蓋率與測試覆蓋率比較
- Jenkins實現iOS自動化測試及覆蓋率報告輸出JenkinsiOS
- 如何制定介面自動化測試的覆蓋率?
- go 程式碼覆蓋率測試Go
- 程式碼測試覆蓋率分析
- 自動化單元工具EvoSuie的程式碼覆蓋報告UI
- Java 程式碼覆蓋率調研報告Java
- 使用Rational PureCoverage測試程式碼覆蓋率
- [android]android自動化測試十二之程式碼控制截圖Android
- iOS 覆蓋率檢測原理與增量程式碼測試覆蓋率工具實現iOS
- 多程式下的測試覆蓋率
- [android]android自動化測試十三之sciroccoAndroid
- [android]android自動化測試五之RobolectricAndroid
- [android]android自動化測試十三之monkeyRunner自動化框架Android框架
- [android]android自動化測試十四之dumpsys效能測試Android
- [android]android自動化測試Android
- 基於JaCoCo的Android測試覆蓋率統計(二)Android
- Android 自動化測試之 MonkeyAndroid
- [android]android自動化測試十五之junitRepoterAndroid
- [android]android自動化測試九之monkeyRecordAndroid
- 程式碼覆蓋率測試:從誤傳到現實
- [android]android自動化測試十之單元測試例項Android
- Junit測試Android自動化測試Android
- [Android]android自動化測試十六之calabash-androidAndroid
- 安卓app功能或自動化測試覆蓋率統計(不用instrumentation啟動app)安卓APP
- pHp程式碼覆蓋率PHP
- 程式碼覆蓋率分析
- [android]android自動化測試二之命令列建立AVDAndroid命令列
- James Shore:不要使用單元測試的程式碼覆蓋率
- C++語言的單元測試與程式碼覆蓋率C++
- ant中使用cobertura分析測試用例的程式碼覆蓋率
- 你真正需要的程式碼測試覆蓋率是多少?
- Jacoco--測試覆蓋率工具
- Mockito提升單元測試覆蓋率Mockito
- [android]android自動化測試六之命令列編譯APKAndroid命令列編譯APK
- [android]android自動化測試十三之JavaMonkey跨APP操作AndroidJavaAPP