运行 在没有 SBT 的情况下从命令行进行 scala 测试

Running scala test from command line without SBT

我正在尝试 运行 在没有 SBT 的情况下从命令行进行 Scala 测试,但我失败了。我按照 documentation 一行一行。

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

}

错误信息:

> 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

ScalaTest 自 2.3.0 以来一直是 modularised,因此仅 scalatest.jar 原始 shell 工件是不够的。诸如 sbt 之类的构建工具通常会自动解析所有传递依赖项,但是如果您不使用构建工具,则有必要手动执行此操作。

所以下载所有的传递依赖和运行类似

的东西
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

或者假定所有传递 jar 都在同一目录中

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

coursier可以帮助您获取并构建正确的class路径

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

或使用 coursier 从包含已编译测试的目录中启动主要 class

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

或启动默认的主要 运行ner,它通过提供 运行 路径 -R

来提供基本的 GUI
cs launch org.scalatest:scalatest_2.13:3.2.4 -- -R .

这里记录的是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