不使用“reportOn”方法聚合多个gradle java项目测试结果

agxfikkp  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(105)

我们有一个多层次的Java项目。本项目在执行“gradlew testReport”命令时导出聚合测试报告。
我们使用“reportOn”方法,但该方法已被弃用,因此此警告日志将输出到控制台。
TestReport.reportOn(Object...)方法已被弃用。这将在Gradle 8.0中删除。请改用testResults方法。更多https://docs.gradle.org/7.5.1/dsl/org.gradle.api.tasks.testing.TestReport.html#org.gradle.api.tasks.testing.TestReport:testResults详细信息,请访问www.example.com。
我们已经重写了配置,引用了警告日志中的链接,但它不起作用。我们希望在不使用“reportOn”方法的情况下实现相同的行为。
我们的项目结构是这样的。

root
 |--- buildSrc
 |--- batch
 |--- common
 |--- webapp
 |--- webservice

每个build.gradle都是这样的。
root\buildSrc\src\main\groovy\java-common.gradle

plugins {
    id 'java'
    id 'eclipse'
    id 'project-report'
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
}

sourceCompatibility = 17
targetCompatibility = 17

[compileJava, compileTestJava]*.options*.encoding = "UTF-8"

group = 'com.example'

repositories {
    mavenCentral()
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    testCompileOnly {
        extendsFrom testAnnotationProcessor
    }
}

dependencies {
    // Spring
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    // Apache Commons Lang3
    implementation "org.apache.commons:commons-lang3:3.12.0"
    
    // lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

    // JUnit
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
    
    // Mockito
    testImplementation 'org.mockito:mockito-core:4.8.1'
    testImplementation 'org.mockito:mockito-inline:4.8.1'
}

tasks.named('test') {
    useJUnitPlatform()

    testLogging {
        showStandardStreams false
        events 'failed'
        exceptionFormat 'full'
    }
    systemProperty "file.encoding", "UTF-8"
    jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED']
}

test {
    // Test Result Reports are output from all projects at once.
    reports.html.required = false
}

// Create a Test Result Report
// gradlew testReport
task testReport(type: TestReport) {
    destinationDirectory = file("${rootDir}/build/reports/allTests")
    reportOn files("${rootDir}/batch/build/test-results/test/binary",
        "${rootDir}/common/build/test-results/test/binary",
        "${rootDir}/webapp/build/test-results/test/binary",
        "${rootDir}/webservice/build/test-results/test/binary")
}

root\batch\build.gradle

plugins {
    id 'java-common'
}

dependencies {
    implementation project(":common")
}

根\common\build.gradle

plugins {
    id 'java-common'
    id 'java-library'
}

dependencies {
    // Spring
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    api 'org.springframework.boot:spring-boot-starter-validation'
}

root\webapp\build.gradle

plugins {
    id 'java-common'
}

dependencies {
    implementation project(":common")
}

root\webservice\build.gradle

plugins {
    id 'java-common'
}

dependencies {
    implementation project(":common")
}

我们更改了测试报告输出的设置,如下所示,测试报告不再输出。

test {
    // Test Result Reports are output from all projects at once.
    reports.html.required = false

    // add this property
    tasks.test.binaryResultsDirectory = file("${rootDir}/build/test-results/test/binary")
}

// Create a Test Result Report
// gradlew testReport
task testReport(type: TestReport) {
    destinationDirectory = file("${rootDir}/build/reports/allTests")
    // reportOn files("${rootDir}/batch/build/test-results/test/binary",
    //     "${rootDir}/common/build/test-results/test/binary",
    //     "${rootDir}/webapp/build/test-results/test/binary",
    //     "${rootDir}/webservice/build/test-results/test/binary")
}

Gradle's TestReport class的实现中,我们还检查了testReport任务的“testResults”属性中包含的内容。
除此之外,我们还对上述配置进行了修改。下一步我们应该尝试什么配置修改?

iezvtpos

iezvtpos1#

我们使用The Test Report Aggregation Plugin解决了这个问题。
插件的设置在下面。
root\build.gradle

plugins {
    id 'java'
    id 'eclipse'
    id 'project-report'
    id 'test-report-aggregation'
}

dependencies {
    testReportAggregation project(':common')
    testReportAggregation project(':batch')
    testReportAggregation project(':webapp')
    testReportAggregation project(':webservice')
}

reporting {
    reports {
        testAggregateTestReport(AggregateTestReport) { 
            testType = TestSuiteType.UNIT_TEST
        }
    }
}

root\buildSrc\src\main\groovy\java-common.gradle

plugins {
    id 'java'
    id 'eclipse'
    id 'project-report'
    id 'org.springframework.boot'
    id 'io.spring.dependency-management'
}

sourceCompatibility = 17
targetCompatibility = 17

[compileJava, compileTestJava]*.options*.encoding = "UTF-8"

group = 'com.example'

repositories {
    mavenCentral()
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    testCompileOnly {
        extendsFrom testAnnotationProcessor
    }
}

dependencies {
    // Spring
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    // Apache Commons Lang3
    implementation "org.apache.commons:commons-lang3:3.12.0"
    
    // lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

    // JUnit
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
    
    // Mockito
    testImplementation 'org.mockito:mockito-core:4.8.1'
    testImplementation 'org.mockito:mockito-inline:4.8.1'
}

tasks.named('test') {
    useJUnitPlatform()

    testLogging {
        showStandardStreams false
        events 'failed'
        exceptionFormat 'full'
    }
    systemProperty "file.encoding", "UTF-8"
    jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED']
}

test {
    // Test Result Reports are output from all projects at once.
    reports.html.required = false
}

// Delete this task!!!
// Create a Test Result Report
// gradlew testReport
// task testReport(type: TestReport) {
//    destinationDirectory = file("${rootDir}/build/reports/allTests")
//    reportOn files("${rootDir}/batch/build/test-results/test/binary",
//        "${rootDir}/common/build/test-results/test/binary",
//        "${rootDir}/webapp/build/test-results/test/binary",
//        "${rootDir}/webservice/build/test-results/test/binary")
}

设置后,我们执行此命令。

gradlew testAggregateTestReport

相关问题