gpt4 book ai didi

scala - Scala after() 方法中有没有办法知道测试是否失败?

转载 作者:行者123 更新时间:2023-12-04 17:54:33 24 4
gpt4 key购买 nike

我有一个实现了 BeforeAndAfter 的测试套件,理想情况下我希望能够在测试失败时检查我的 after() 方法,如果它确实计算了一些值并将其打印出来。有这样做的内置方法吗?

附言。我知道我可以围绕整个测试进行 try/catch,但我宁愿不必这样做。

最佳答案

我不认为你可以在 after() 中做到这一点,但是你可以通过覆盖 withFixture() 来做到这一点。此方法运行实际测试,之后您可以匹配结果并在失败时打印一些内容:

class ExampleSpec extends WordSpec with MustMatchers {

"Example" must {
"work correctly" in {
3 must be(3)
}

"not fail" in {
true must be(false)
}
}

override def withFixture(test: NoArgTest) = super.withFixture(test) match {
case failed: Failed =>
val nameLen = test.name.length
info(s"The test '${test.name}' failed")
info(s"The test name is $nameLen characters long")
failed
case other => other
}
}

参见 Sharing Fixtures ,尤其是“Overriding withFixture(NoArgTest)”部分

关于scala - Scala after() 方法中有没有办法知道测试是否失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com