FAILURE:构建完成,有 2 次失败。
1:任务因异常而失败。
其中:构建文件
'/private/var/folders/56/_q9rs4153zq4v4y_d12ch77h0000gr/T/junit17412812315691736065/build.gradle' line: 17
出了什么问题:评估根项目“QualityToolsTestProject”时出现问题。
未能应用插件“com.android.library”。> 无法添加任务“unitTestCoverage”,因为具有该名称的任务已存在。
2:任务因异常而失败。
com.android.builder.errors.EvalIssueException: compileSdkVersion is not specified. Please add it to build.gradle
CodeCoveragePluginForAndroidConnectedTest 代码如下
package
com.jitendra.sdk.android.gradle.quality
import com.jitendra.sdk.android.gradle.quality.test.*
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import java.io.File
class CodeCoveragePluginForAndroidConnectedTest : BaseTest() {
private lateinit var buildFile: File
private lateinit var sourceMainDir: File
private lateinit var sourceAndroidTestDir: File
@Before
fun setup() {
testProjectDir.apply {
createGradleProperties()
createSettingsFile(projectName = TEST_PROJECT_NAME)
buildFile = createProjectFile("build.gradle") {
"""
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
}
}
plugins {
id 'code-coverage'
id 'java'
}
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
repositories {
google()
mavenCentral()
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
targetSdkVersion 30
minSdkVersion 21
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
testCoverageEnabled = true
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
androidTestImplementation "androidx.test.ext:junit:1.1.1"
androidTestImplementation "androidx.test:runner:1.2.0"
testImplementation "junit:junit:4.13"
}
"""
}
createManifestFile()
sourceMainDir = createSourceMainDirectories()
sourceAndroidTestDir = createSourceAndroidTestDirectories()
}
}
@Test
fun `when applying code coverage for android, code coverage tasks should be added`() {
runBuild("tasks") {
// checkOutputContains("coverage - Generate Jacoco coverage reports on test builds.")
// checkOutputContains("androidTestCoverageDebug - Generate Jacoco coverage reports on the Debug build.")
// checkOutputContains("coveragePrintDebug - Print the code coverage of the Debug build using the coverageDebug report.")
// checkOutputContains("coveragePrintRelease - Print the code coverage of the Release build using the coverageRelease report.")
// checkOutputContains("coverageVerificationDebug - Run Jacoco coverage verification on the Debug build.")
// checkOutputContains("coverageVerificationRelease - Run Jacoco coverage verification on the Release build.")
}
}
@Test
fun `when coverage below default minimum, verify task outcome should be FAILED`() {
// Setup with 40% coverage, default is 50%
buildFile {
"""
codeCoverage {
failOnBelowMinimumCoverage true
}
"""
}
"One".also { className ->
sourceMainDir.createSourceKotlinFile(className = className)
sourceAndroidTestDir.createAndroidTestKotlinFile(testFor = className)
}
"Two".also { className ->
sourceMainDir.createSourceJavaFile(className = className)
sourceAndroidTestDir.createAndroidTestKotlinFile(testFor = className)
}
sourceMainDir.createSourceJavaFile(className = "Three")
sourceMainDir.createSourceKotlinFile(className = "Four")
sourceMainDir.createSourceKotlinFile(className = "Five")
runBuildAndFail("coverageVerificationDebug") {
assertEquals(TaskOutcome.FAILED, task(":coverageVerificationDebug")?.outcome)
// checkOutputContains("Code coverage for bundle $TEST_PROJECT_NAME: 40.00%")
// checkOutputContains("Rule violated for bundle $TEST_PROJECT_NAME: instructions covered ratio is 0.4, but expected minimum is 0.5")
}
}
}