最後我設法它得到它的工作,我想與大家分享的解決方案:
使儀器爲您buildType並配置相應SonarQube例如
...
apply plugin: 'jacoco'
...
android {
...
productFlavors {
acceptance {
applicationId packageName + ".acceptance"
buildTypes {
debug {
testCoverageEnabled true
}
}
}
}
}
sonarRunner {
sonarProperties {
property "sonar.host.url", "..."
property "sonar.jdbc.url", sonarDatabaseUrl
property "sonar.jdbc.driverClassName", sonarDatabaseDriverClassName
property "sonar.jdbc.username", sonarDatabaseUsername
property "sonar.jdbc.password", sonarDatabasePassword
property "sonar.sourceEncoding", "UTF-8"
property "sonar.sources", "src/main"
property "sonar.tests", "src/test"
property "sonar.inclusions", "**/*.java,**/*.xml"
property "sonar.import_unknown_files", "true"
property "sonar.java.binaries", "build/intermediates/classes/acceptance/debug"
property "sonar.junit.reportsPath", "build/test-results/acceptanceDebug"
property "sonar.android.lint.report", "build/outputs/lint-results.xml"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPath", "build/jacoco/testAcceptanceDebugUnitTest.exec"
// see steps below on how to get that file:
property "sonar.jacoco.itReportPath", "build/jacoco/jacoco-it.exec"
property "sonar.projectKey", projectKey
property "sonar.projectName", projectName
property "sonar.projectVersion", appVersionName
}
}
以下內容添加到您的AndroidManifest.xml
<receiver
android:name=".util.CoverageDataDumper"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="org.example.DUMP_COVERAGE_DATA"/>
</intent-filter>
</receiver>
CoverageDataDumper應該看起來像:
public class CoverageDataDumper extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(CoverageDataDumper.class);
@Override
public void onReceive(Context context, Intent intent) {
try {
Class
.forName("com.vladium.emma.rt.RT")
.getMethod("dumpCoverageData", File.class, boolean.class, boolean.class)
.invoke(null,
new File(App.getContext().getExternalFilesDir(null) + "/coverage.ec"),
true, // merge
false // stopDataCollection
);
}
catch (Exception e) {
LOG.error("Error when writing coverage data", e);
}
}
}
然後運行Appium測試用例驗收味道應用(與儀表類)。致電前「重置應用」或「關閉應用程序」請確保調用以下方法(只是一個草案,但我認爲你的想法):
// intent is "org.example.DUMP_COVERAGE_DATA"
public void endTestCoverage(String intent) {
if (driver instanceof AndroidDriver) {
((AndroidDriver) driver).endTestCoverage(intent, "");
}
}
public void pullCoverageData(String outputPath) {
String coverageFilePath = (String) appiumDriver.getCapabilities().getCapability("coverageFilePath");
if (coverageFilePath != null) {
byte[] log = appiumDriver.pullFile(coverageFilePath);
MobileAppLog.writeLog(new File(outputPath), log);
}
else {
throw new AppiumLibraryNonFatalException(
"Tried to pull the coverage data, but the coverageFilePath wasn't specified.");
}
}
outputPath可能是例如:/ SD卡/安卓/data/org.example.acceptance/files/coverage.ec
現在Jacoco數據被寫入智能手機。接下來我們需要下載該文件。您可以使用
appiumDriver.pullFile(logFilePath);
現在你需要複製該文件「jacoco-it.exec」(這應該永遠是附加當你拉文件)到編譯/ jacoco/jacoco-it.exec的gradle見.build以上並運行
gradlew sonarRunner
在SonarQube添加集成測試覆蓋率的Widget,你現在一些值應該看到...
不幸的是,如果你使用的是retrolambda代碼覆蓋率將無法正常工作(因爲我們做)。Retrolambda將生成不屬於源文件的匿名類 - 所以SonarQube無法正確匹配它們,並且顯示的代碼覆蓋率比實際低得多。如果有人找到了解決方案,我會很高興:-)
我缺少的東西,或者是你同時使用Emma和jacoco這裏?我有類似的問題,所以這個答案非常有趣 - 但我無法理解。 – Vish