如何通过 sbt 使用 flatspec 仅 运行 测试在 scala 中没有特定标签?

How to only run tests not having a certain tag in scala using flatspec through sbt?

作为 scala 初学者,我想标记集成测试,以便在某些情况下将它们从 运行ning 中排除(因为它们可能非常慢并且可能由于外部 changes/problems ).

我是这样创建标签 integration 的:

import org.scalatest.{FlatSpec, Matchers, Tag}
object integration extends Tag("com.dreamlines.tags.integration")

在我的测试中,我这样标记了一个测试:

class SchemaValidation extends FlatSpec with Matchers {
  it should "return valid json" taggedAs (integration) in {
    ...
    assertSchema(response, endpointSchema)
  }
}

然而,当我阅读 how to filter certain tests based on the tag in the docs 时,我感到非常困惑,因为我突然读到我应该使用 org.scalatest.tools.Runner

scala [-cp scalatest-<version>.jar:...] org.scalatest.tools.Runner [arguments]

其中有我正在寻找的标志:

-l specifies a tag to exclude (Note: only one tag name allowed per -l) -l SlowTests -l PerfTests

然而我只习惯 运行 我的测试通过:

sbt test

我不知道这里的 scala 测试 运行ner 指的是什么,也不知道当我 运行 sbt test 时幕后发生了什么。我真的迷路了。

我期待执行与此完全不同的测试:

sbt test --ignore-tag integration

根据 sbt documentation on test options 以下内容应该适合您:

testOnly -- -l integration

或者如果 ScalaTest 使用完全限定的标签 ID:

testOnly -- -l com.dreamlines.tags.integration

如果您想默认执行此操作,您可以在 build.sbt 文件中调整 testOptions

编辑:

您可以考虑 a different approach for integration test。您可能不想标记它们,而是将它们放在 src/it/{scala|resources} 文件夹中。