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

How do I run both ScalaTest and JUnit tests with Gradle?

我有一个包含JUnit和ScalaTest测试的Java / Scala混合项目。 使用scalatest插件,Gradle在src/test/scala中运行ScalaTest测试,但忽略src/test/java中的JUnit测试。 如果没有插件,Gradle将运行JUnit测试,但会忽略Scala。 我想念什么技巧?

我的build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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"
    ]
}

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


与JUnit运行(以及按照注释中的建议创建Ant任务)的另一种选择是-创建一个直接运行ScalaTest的Runner的任务:

1
2
3
4
5
6
7
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

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

  • 使用最新版本的scala-test插件,您可以选择是否用scala-tests-task替换现有的(junit)-test任务。在您的情况下,您可以在

    gradle.properties:

    1
    com.github.maiflai.gradle-scalatest.mode = append

    现在您可以执行两个测试:

    • junit:gradle测试
    • scala-test:gradle scalatest

    或将它们组合成一项任务

    1
    2
    3
    4
    task allTests {
        dependsOn test
        dependsOn scalatest
    }

    那对我有用。


    如果您不想使用@RunWith(classOf[JUnitPlatformRunner])注释所有ScalaTest测试,但想要使用JUnit 5,则可以在Gradle中使用scalatest-junit-runner。
    由于它已集成到JUnitPlatform系统中,因此您只需使用test任务,ScalaTest将与其他测试引擎执行的其他测试一起运行:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    plugins {
        id 'scala'
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation"org.scala-lang:scala-library:$scala_version"

        testImplementation"org.scalatest:scalatest_$scala_lib_version:3.2.0-M3"
        testRuntime"org.junit.platform:junit-platform-engine:$junit_platform_version"
        testRuntime"org.junit.platform:junit-platform-launcher:$junit_platform_version"
        testRuntime"co.helmethair:scalatest-junit-runner:0.1.2"
    }

    test{
        useJUnitPlatform {
            includeEngines 'scalatest'      
        }
    }