我们可以在一个项目中 运行 Spock Testcases 和 Junit 5 测试用例吗?

Can we run Spock Testcases and Junit 5 test cases together In one project?

我们无法 运行 将使用 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. 下面是构建路径。

您需要 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(即最新和最好的)。