有没有办法 运行 测试,但如果失败则不将整个套件标记为失败?

Is there a way to run a test, but not mark the whole suite as failed if it fails?

使用 Scalatest 可以将测试标记为 "optional"。我的意思是 运行 无论如何,但如果它失败了,不要将整个测试执行标记为失败(Return 0 而不是 1 到 shell)。

我知道 ignore 作为 in 的替代品存在,但这并不完全符合我的意愿。

我想这样做的原因是我有一个项目,其中一些测试可能 运行 在某些网络中而在其他一些网络中失败。

这可能吗?

您可以使用 tags to define groups of tests. You could either whitelist or blacklist tests. If you want to whitelist tests, define a tag for each environment, and if a test should pass in that environment, give it that tag. Then, in each environment, include tests with the tag for that environment with -n. If you want to blacklist tests, tag tests that should fail in each environment, and then exclude tests with that tag using -l. Filtering tests is done with runner configuration

这是一个需要外部服务的黑名单测试示例。

object ServiceTest extends Tag("com.mycompany.tags.ServiceTest")

class MyTest extends WordSpec {
  "a foo" should {
    "connect to the service" taggedAs ServiceTest in {
      // ...
    }

    "do something without the service" in {
      // ...
    }
  }
}

可以也可以使用cancel()来中止测试。我不知道你所说的 "aborts everything" 是什么意思,只是取消了当前的测试用例。但我认为最好设定测试在给定环境中通过或失败的预期,如果没有,请进行调查。否则,如果网络不可用并且存在真正的问题,则很容易忽略取消的测试,比如 url 已更改。

class MyTest extends WordSpec {
  "a foo" should {
    "connect to the service" in {
      if (serviceUnavailable()) cancel("Can't connect to service.")

      // ...
    }

    "do something without the network" in {
      // ...
    }
  }

  def serviceUnavailable(): Boolean = ???
}