首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于Junit5的分级多项目集成测试

基于Junit5的分级多项目集成测试
EN

Stack Overflow用户
提问于 2019-03-28 16:00:55
回答 1查看 2.3K关注 0票数 4

如何在多项目构建文件中使用Gradle 5.2和JUnit 5.3创建集成测试?

这是我当前的构建文件:

代码语言:javascript
复制
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.10"
    classpath 'org.owasp:dependency-check-gradle:5.0.0-M2'
  }
}


allprojects {
  defaultTasks 'clean', 'build', 'publish', 'installDist'
  group = 'coyote'
  version = '0.1.0'
}

subprojects {

  apply plugin: 'java'
  apply plugin: 'jacoco'
  apply plugin: "com.github.spotbugs"
  apply plugin: 'org.owasp.dependencycheck'
  apply plugin: 'maven-publish'
  apply plugin: 'application'
  apply plugin: 'eclipse'
  apply plugin: 'idea'

  repositories {
    jcenter()
    mavenCentral()
  }

  dependencies {
    testImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
    testRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
    spotbugsPlugins ("com.h3xstream.findsecbugs:findsecbugs-plugin:1.8.0")
  }

  tasks.withType(Test) {
    useJUnitPlatform()
  }

  apply from: "$rootDir/integration-test.gradle"

  check.dependsOn jacocoTestCoverageVerification
  check.dependsOn dependencyCheckAnalyze

  spotbugs {
    effort = "max"
    reportLevel = "low"
    ignoreFailures = true
    showProgress = true
  }

  jacocoTestReport {
    reports {
      xml.enabled true
      html.enabled true
    }
  }

  check.dependsOn jacocoTestReport

  tasks.withType(com.github.spotbugs.SpotBugsTask) {
    reports {
      xml.enabled false
      html.enabled true
    }
  }

}

我在第46行应用的integration-test.gradle文件包含以下内容:

代码语言:javascript
复制
sourceSets {
    itest {
        compileClasspath += sourceSets.main.output + configurations.testCompile
        runtimeClasspath += output + compileClasspath + configurations.testRuntime
    }
}

idea {
    module {
        testSourceDirs += sourceSets.itest.java.srcDirs
        testResourceDirs += sourceSets.itest.resources.srcDirs
        scopes.TEST.plus += [configurations.itestCompile]
    }
}

task itest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.itest.output.classesDirs
    classpath = sourceSets.itest.runtimeClasspath
    outputs.upToDateWhen { false }
}

在Intellij中,一切看起来都很好,但是JUnit5并没有被添加到类路径中,所以itest目录中的任何测试都找不到JUnit库。

在没有找到gradlew itest类的情况下,运行JUnit失败的结果与此相似。

我尝试在useJUnitPlatform()任务中直接添加使用itest,但没有成功。我也尝试过将所有东西放在build.gradle文件中,但都没有成功。

最后,我希望使用相同的模式来定义加载和安全测试,将它们分别作为CI/CD管道的一部分运行,以便将所有内容整齐地放在各自项目下的目录中,而不是将所有内容混合到一个测试目录中并使用标记。

这也有助于其他团队建立CI/CD实践模型,因此使用Gradle 5和JUnit 5作为首选,而不是使用工作圈或黑客来使事情正常进行。可以接受的是,这些版本不一起工作,或者目前存在缺陷\问题阻碍了这种方法。希望这不是问题,我只是错过了一些简单的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 18:13:45

解决方案是我自己需要包含JUnit 5依赖项。以下是正确的integration-test.gradle文件:

代码语言:javascript
复制
sourceSets {
    itest {
        compileClasspath += sourceSets.main.output + configurations.testCompile
        runtimeClasspath += output + compileClasspath + configurations.testRuntime
    }
}

idea {
    module {
        testSourceDirs += sourceSets.itest.java.srcDirs
        testResourceDirs += sourceSets.itest.resources.srcDirs
        scopes.TEST.plus += [configurations.itestCompile]
    }
}

dependencies {
    itestImplementation ("org.junit.jupiter:junit-jupiter-api:5.3.2")
    itestRuntimeOnly ("org.junit.jupiter:junit-jupiter-engine:5.3.2")
}

task itest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.itest.output.classesDirs
    classpath = sourceSets.itest.runtimeClasspath
    outputs.upToDateWhen { false }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55402064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档