scala 无法使用 Gradle 运行 Scalatest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18823855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:40:26  来源:igfitidea点击:

Can't run Scalatest with Gradle

scalagradlescalatest

提问by Joshua MN

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')
    }
}

I run gradle scalaTestand I get:

我跑gradle scalaTest,我得到:

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

I am using Scala 2.10.2 and Gradle 1.7

我正在使用 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'
}

What's wrong??

怎么了??

回答by rarry

I do not know how to solve this one, but I can offer you a workaround. Annotate your test classes with @RunWith(classOf[JUnitRunner]), like this:

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

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

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

and then, gradle testshould work.

然后,gradle test应该工作。

Edit:

编辑:

My dependencies:

我的依赖:

compile "org.scala-lang:scala-library:2.10.1"
testCompile "org.scalatest:scalatest_2.10:1.9.1"

回答by Machisuji

You can put the following in your build.gradle:

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

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

Note:The path my be different depending on the gradle version as pointed out in the comments by @MikeRylander. Before gradle 4 it used to be 'build/classes/test'.

注意:根据@MikeRylander 的评论中指出的 gradle 版本,路径可能会有所不同。在 gradle 4 之前,它曾经是“构建/类/测试”。

Then just run gradle specto execute your tests. I named the task specbecause there already is a testtask. Dunno if you can override the default test task.

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

You can look up the available options here.

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

回答by voxoid

rarry is correct. And even better, you can annotate a base test classwith @RunWith(classOf[JUnitRunner])onceto cause all of your ScalaTest tests to run with the JUnit runner (assuming they extend the base class), e.g.:

拉里是对的。更好的是,您可以使用一次注释基本测试类,以使所有 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

回答by Victor Sorokin

As of Gradle 3.2.1, following root build.gradleruns both JUnit and ScalaTest code. This code is for multi-project build, enabled via settings.gradle, that's why subprojectsused.
Plus, it's on purpose uses explicit Gradle API, to minimize Gradle DSL magic.

从 Gradle 3.2.1 开始,root 会同时build.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)
}

回答by Developer

This response may be a bit late. But for one using scala with gradle (5.x), the following works.

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

Add the following plugin to gradle.

将以下插件添加到 gradle。

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

To run the code

运行代码

> gradle test

As a bonus the test results from the above pluginwould also be reported better than the default report.

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