Sbt从src / main / scala而不是src / test / scala运行测试?

问题描述 投票:1回答:1

说我在main / scala中有一个scalatest类,例如

import org.scalatest.FunSuite

class q3 extends FunSuite {
    test("6 5 4 3 2 1") {
        val digits = Array(6,5,4,3,2,1)
        assert(digits.sorted === Array(1,2,3,4,5,6))
    }
}

如何使用sbt运行它?

我已经尝试过sbt testsbt testOnlysbt "testOnly *q3",它们都具有类似的输出

[info] Run completed in 44 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] No tests were executed.
[info] No tests to run for Test / testOnly

几年前的类似问题说他们成功使用了testOnly,但我无法使其正常工作。

VSCode上的metal扩展名在打开文件时显示“测试”链接,该链接成功运行了测试,但未显示它是如何执行的。我想知道如何通过sbt进行操作。

scala sbt scalatest runner scala-metals
1个回答
2
投票
将ScalaTest放在Compile中的build.sbt类路径上,像这样

libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0"

例如,然后从org.scalatest.run内部显式调用org.scalatest.run运行程序,

App

object MainTests extends App {
  org.scalatest.run(new ExampleSpec)
}
中有Putting it together

src/main/scala/example/MainTests.scala

并使用package example

import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import collection.mutable.Stack
import org.scalatest._

class ExampleSpec extends AnyFlatSpec with 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)
  }
}

object MainTests extends App {
  org.scalatest.run(new ExampleSpec)
}
运行它。此外,我们可以像这样收集所有在runMain example.MainTestsSuites中的测试]

Suites

© www.soinside.com 2019 - 2024. All rights reserved.