【问题标题】:Can we run Spock Testcases and Junit 5 test cases together In one project?我们可以在一个项目中同时运行 Spock 测试用例和 Junit 5 测试用例吗?
【发布时间】:2019-12-26 08:43:20
【问题描述】:

我们不能在一个 gardle 项目 中同时运行使用 Junit 5Spock 框架 编写的测试用例吗?

我们尝试将https://www.baeldung.com/junit-5-gradle 中给出的依赖项添加到我们的 gradle 文件中。 Gradle 版本是 4.10.3 和 Junit 5。下面是我的 build.gradle 文件

apply plugin: 'groovy'
apply plugin: 'java'

repositories {
  mavenCentral()
  maven {
    url "http://repo.fusesource.com/nexus/content/groups/public/"
  }
  maven {
    url "https://repository.jboss.org/nexus/content/groups/public"
  }
  jcenter()
}

dependencies {

  compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
  compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'

  testCompile(
    'org.codehaus.groovy:groovy-all:2.4.8',
    'org.spockframework:spock-core:1.0-groovy-2.4',
    'org.jmockit:jmockit:1.8',
    'junit:junit:4.12'
  )
  testRuntime(
    'cglib:cglib:2.2.2',
    'com.athaydes:spock-reports:1.2.7'
  )

  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'

  testCompileOnly 'junit:junit:4.12'

}

test {
  useJUnitPlatform()
  testLogging { showStandardStreams = true }
}

我创建了两个测试用例,一个使用 spock 框架,另一个使用 junit 5。但是当我执行 gradlew -test 时,它只运行用 Junit 5 编写的测试用例。下面是构建路径。

【问题讨论】:

    标签: unit-testing gradle build.gradle spock junit5


    【解决方案1】:

    您需要 Vintage 测试引擎来执行 Spock 测试,因为它们基于 JUnit 4,并且您需要 Jupiter 测试引擎来执行 JUnit Jupiter 测试。

    所以你需要依赖两个引擎。

    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    

    我还建议您升级到 JUnit 5.5.1(即最新最好的)。

    【讨论】:

    • 山姆,感谢您的回复。我尝试使用老式测试引擎,但现在 gradle 只执行 spock 测试用例,而 junit 测试用例没有被执行。当我运行“gradlew clean test”命令时,我需要同时运行它们。
    • 您还需要junit-jupiter-engine。如果我的措辞不清楚,我很抱歉。我现在会改进它。
    • 成功了...非常感谢...我会尝试运行更多场景...
    猜你喜欢
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多