Search code examples
scalascalatest

Running scala test from command line without SBT


I am trying to run the scala test from the command line without SBT and I am failing. I followed the documentation line-by-line.

import collection.mutable.Stack
import org.scalatest._
import flatspec._
import matchers._

class FirstSpec extends AnyFlatSpec with should.Matchers {

  "A Stack" should "pop values in last-in-first-out order" in {
    val stack = new Stack[Int]
    stack.push(1)
    stack.push(2)
    stack.pop() should be (2)
    stack.pop() should be (1)
  }

}

Error message:

> scala -cp scalatest_2.13-3.2.5.jar org.scalatest.tools.Runner -R . -o -s FirstSpec.scala
No such file or class on classpath: org.scalatest.tools.Runner

Repository


Solution

  • ScalaTest has been modularised since 2.3.0 so just scalatest.jar artifact is not sufficient from raw shell. Build tools such as sbt would usually resolve all the transitive dependencies automatically, however if you are not using a build tool, then it is necessary to do that manually.

    So download all the transitive dependencies and run something like

    scala -cp scalatest_2.13-3.2.4.jar:scalatest-compatible-3.2.4.jar:scalatest-core_2.13-3.2.4.jar:scalactic_2.13-3.2.4.jar:scalatest-diagrams_2.13-3.2.4.jar:scalatest-matchers-core_2.13-3.2.4.jar:scalatest-shouldmatchers_2.13-3.2.4.jar:scalatest-flatspec_2.13-3.2.4.jar:scala-xml_2.13-1.3.0.jar org.scalatest.run ExampleSpec
    

    or given all the transitive jars are in the same directory

    scala -cp '*' org.scalatest.run ExampleSpec
    

    or coursier can help you fetch and build the correct classpath

    scala -cp "$(cs fetch --classpath org.scalatest:scalatest_2.13:3.2.4)" org.scalatest.run ExampleSpec
    

    or use coursier to launch the main class from directory containing compiled tests

    cs launch org.scalatest:scalatest_2.13:3.2.4 -M org.scalatest.run
    

    or launch the default main runner which provides basic GUI by providing the run path -R

    cs launch org.scalatest:scalatest_2.13:3.2.4 -- -R .
    

    For record here are all the transitive dependencies of scalatest.jar

    cs resolve org.scalatest:scalatest_2.13:3.2.5   
    
    org.scala-lang:scala-library:2.13.4:default
    org.scala-lang:scala-reflect:2.13.4:default
    org.scala-lang.modules:scala-xml_2.13:1.2.0:default
    org.scalactic:scalactic_2.13:3.2.5:default
    org.scalatest:scalatest-compatible:3.2.5:default
    org.scalatest:scalatest-core_2.13:3.2.5:default
    org.scalatest:scalatest-diagrams_2.13:3.2.5:default
    org.scalatest:scalatest-featurespec_2.13:3.2.5:default
    org.scalatest:scalatest-flatspec_2.13:3.2.5:default
    org.scalatest:scalatest-freespec_2.13:3.2.5:default
    org.scalatest:scalatest-funspec_2.13:3.2.5:default
    org.scalatest:scalatest-funsuite_2.13:3.2.5:default
    org.scalatest:scalatest-matchers-core_2.13:3.2.5:default
    org.scalatest:scalatest-mustmatchers_2.13:3.2.5:default
    org.scalatest:scalatest-propspec_2.13:3.2.5:default
    org.scalatest:scalatest-refspec_2.13:3.2.5:default
    org.scalatest:scalatest-shouldmatchers_2.13:3.2.5:default
    org.scalatest:scalatest-wordspec_2.13:3.2.5:default
    org.scalatest:scalatest_2.13:3.2.5:default