Can't run Scalatest with Gradle

前端 未结 5 434
醉梦人生
醉梦人生 2021-02-02 09:28
task scalaTest(dependsOn: testClasses) << {
    description = \'Runs Scalatest suite\'
    ant.taskdef(name: \'scalatest\',
            classname: \'org.scalatest.         


        
相关标签:
5条回答
  • 2021-02-02 09:37

    You can put the following in your 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'.

    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.

    0 讨论(0)
  • 2021-02-02 09:39

    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
    
    0 讨论(0)
  • 2021-02-02 09:40

    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"
    
    0 讨论(0)
  • 2021-02-02 09:44

    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)
    }
    
    0 讨论(0)
  • 2021-02-02 09:51

    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.

    0 讨论(0)
提交回复
热议问题