28
task scalaTest(dependsOn: testClasses) << {
    description = 'Runs Scalatest suite'
    ant.taskdef(name: 'scalatest',
            classname: 'org.scalatest.tools.ScalaTestAntTask',
            classpath: sourceSets.test.runtimeClasspath.asPath
    )
    ant.scalatest(runpath: sourceSets.test.output.classesDir,
            haltonfailure: 'true', fork: 'false') {
        reporter(type: 'stdout')
    }
}

我跑gradle scalaTest,我得到:

* What went wrong:
Execution failed for task ':scalaTest'.
> java.lang.NoClassDefFoundError: scala/reflect/ClassManifest$

我正在使用 Scala 2.10.2 和 Gradle 1.7

dependencies {
    compile 'org.scala-lang:scala-library:2.10.2'   
    testCompile 'org.scalatest:scalatest:1.3'
    testCompile 'org.scalamock:scalamock_2.10:3.0.1'
}

怎么了??

4

5 回答 5

33

我不知道如何解决这个问题,但我可以为您提供一种解决方法。用 注释您的测试类@RunWith(classOf[JUnitRunner]),如下所示:

import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith

@RunWith(classOf[JUnitRunner])
class MyTest extends FunSpec{
}

然后,gradle test应该工作。

编辑:

我的依赖:

compile "org.scala-lang:scala-library:2.10.1"
testCompile "org.scalatest:scalatest_2.10:1.9.1"
于 2013-09-16T09:10:50.617 回答
24

您可以将以下内容放入您的build.gradle:

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

注意:路径可能会有所不同,具体取决于@MikeRylander 在评论中指出的 gradle 版本。在 gradle 4 之前,它曾经是“build/classes/test”。

然后只需运行gradle spec即可执行您的测试。我命名了这个任务spec,因为已经有一个test任务。我不知道你是否可以覆盖默认的测试任务。

您可以在此处查找可用选项。

于 2014-11-24T00:56:59.513 回答
5

这个回复可能有点晚了。但是对于使用带有 gradle (5.x) 的 scala 的人来说,以下是有效的。

将以下插件添加到 gradle。

plugins {
  id "com.github.maiflai.scalatest" version "0.25"
}

运行代码

> gradle test

作为奖励,上述测试结果的plugin报告也将比默认报告更好。

于 2019-06-27T15:54:31.610 回答
3

rary 是正确的。更好的是,您可以使用一次注释基测试,以使所有 ScalaTest 测试与 JUnit 运行器一起运行(假设它们扩展了基类),例如:@RunWith(classOf[JUnitRunner])

import org.junit.runner.RunWith
import org.scalatest._
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
abstract class UnitSpec extends FlatSpec with Matchers
于 2016-03-08T14:55:39.233 回答
3

从 Gradle 3.2.1 开始,跟随 rootbuild.gradle运行 JUnit 和 ScalaTest 代码。此代码用于多项目构建,通过 启用settings.gradle,这就是subprojects使用的原因。
另外,它故意使用明确的 Gradle API,以尽量减少 Gradle DSL 的魔力。

description = 'root project'

def enableScalaTest(Project project) {
    Task scalaTest = project.task(
            [
                    'dependsOn': 'testClasses',
                    'description': 'Runs ScalaTest tests in the project'
            ],
            'scalaTest',
            {
                ext.inputDir = project.tasks.getByName('compileTestScala').destinationDir
                inputs.dir(ext.inputDir)
            }
    )

    scalaTest.doLast({
        ant.taskdef(name: 'scalatest',
                classname: 'org.scalatest.tools.ScalaTestAntTask',
                classpath: project.sourceSets.test.runtimeClasspath.asPath)
        ant.scalatest(runpath: ext.inputDir,
                fork: 'false',
                haltonfailure: 'true')
                { reporter(type: 'stdout') }
    })

    project.getTasks().getByName('build').dependsOn(scalaTest)
}

subprojects {
    apply plugin: 'scala'

    repositories {
        mavenCentral()
    }

    dependencies {
        compile 'org.scala-lang:scala-library:2.12.1'

        testCompile 'junit:junit:4.12'
        testCompile 'org.scalatest:scalatest_2.12:3.0.1'
    }

    enableScalaTest(delegate)
}
于 2016-12-12T17:44:14.237 回答