如何使用Gradle运行ScalaTest和JUnit测试?

Dav*_*les 15 junit gradle scalatest

我有一个混合的Java/Scala项目,包括JUnit和ScalaTest测试.使用scalatest插件,Gradle运行ScalaTest测试src/test/scala,但忽略了JUnit测试src/test/java.没有插件,Gradle运行JUnit测试但忽略Scala.我错过了什么伎俩?

我的build.gradle:

plugins {
  id 'java'
  id 'maven'
  id 'scala'
  id "com.github.maiflai.scalatest" version "0.6-5-g9065d91"
}

sourceCompatibility = 1.8

group = 'org.chrononaut'
version = '1.0-SNAPSHOT'

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

ext {
    scalaMajorVersion = '2.11'
    scalaVersion = "${scalaMajorVersion}.5"
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile "org.scala-lang:scala-library:${scalaVersion}"
    compile "org.scala-lang.modules:scala-xml_${scalaMajorVersion}:1.0.3"
    compile 'com.google.guava:guava:18.0'
    compile 'javax.xml.bind:jaxb-api:2.2.12'
    compile 'jaxen:jaxen:1.1.6'
    compile 'joda-time:joda-time:2.7'
    compile 'org.joda:joda-convert:1.7'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'org.jdom:jdom2:2.0.5'

    testCompile 'junit:junit:4.12'
    testCompile 'org.easytesting:fest-assert:1.4'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile "org.scalatest:scalatest_${scalaMajorVersion}:2.2.4"
    testRuntime 'org.pegdown:pegdown:1.1.0' // required by scalatest plugin
}

compileScala {
    scalaCompileOptions.additionalParameters = [
            "-feature",
            "-language:reflectiveCalls", // used for config structural typing
            "-language:postfixOps"
    ]
}
Run Code Online (Sandbox Code Playgroud)

ETA:我知道可以注释Scala测试以强制它们与JUnit测试运行器一起运行.我正在寻找一站式build.gradle解决方案,不需要编辑每个测试文件(或者一般情况下,搞乱测试以解决构建系统中的限制).

Tza*_*har 10

使用JUnit运行的另一种替代方法(以及在注释中建议创建Ant任务) - 正在创建一个直接运行ScalaTest的Runner的任务:

task scalaTest(dependsOn: ['testClasses'], type: JavaExec) {
  main = 'org.scalatest.tools.Runner'
  args = ['-R', 'build/classes/test', '-o']
  classpath = sourceSets.test.runtimeClasspath
}

test.dependsOn scalaTest // so that running "test" would run this first, then the JUnit tests
Run Code Online (Sandbox Code Playgroud)


Pio*_*hen 9

  1. 摆脱插件,因为它使测试任务只运行ScalaTest(因此JUnit被忽略).
  2. 注释您的ScalaTests,@RunWith(classOf[JUnitRunner])以便它们可以通过gradle作为JUnit测试运行.

  • 然后你应该扩展你的构建并添加一个新任务(例如`scalaTest.mustRunAfter test`)并在引擎盖下使用Ant任务.像https://issues.gradle.org/browse/GRADLE-2659这样的东西.这样`gradle check`将运行JUnit测试和ScalaTests. (2认同)