Can't run Scalatest with Gradle

不打扰是莪最后的温柔 提交于 2020-01-12 02:56:06

问题


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 scalaTest and I get:

* 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

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??


回答1:


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:

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

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

and then, gradle test should work.

Edit:

My dependencies:

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



回答2:


You can put the following in your build.gradle:

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

Then just run gradle spec to execute your tests. I named the task spec because there already is a test task. Dunno if you can override the default test task.

You can look up the available options here.




回答3:


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

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

@RunWith(classOf[JUnitRunner])
abstract class UnitSpec extends FlatSpec with Matchers



回答4:


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

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



回答5:


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

Add the following plugin to 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 plugin would also be reported better than the default report.



来源:https://stackoverflow.com/questions/18823855/cant-run-scalatest-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!