【问题标题】:Why are test marked as passed in spite of failures为什么尽管失败,测试仍被标记为通过
【发布时间】:2017-09-11 13:42:32
【问题描述】:

我正在使用带有自定义生成器的 Scalatest/Scalacheck。我观察到即使某些测试失败,测试也会被标记为成功。在下面的示例测试中,“应该添加处理时间戳”是伪造的。然而 sbt 测试通过了。

+ OK, passed 100 tests.
[info] - should add product info to event 
[info] - should not alter rest of event
+ OK, passed 100 tests.
! Falsified after 0 passed tests.
> ARG_0: List("([B@27d10fe1,...)")
> ARG_0_ORIGINAL: List("([B@3c8057ce,...)")
[info] - should add processing timestamp
[info] ScalaTest
[info] Run completed in 4 seconds, 792 milliseconds.
[info] Total number of tests run: 3
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 3, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 3, Failed 0, Errors 0, Passed 3
[success] Total time: 8 s, completed Sep 11, 2017 6:54:28 PM

为什么测试没有失败??

更新: sbt 0.13,scalatest 3.0.1,scalacheck 1.13.4,测试用例是

  it should "add processing timestamp" in {
    Prop.forAll(sizedGen(TestInputGen.Generate)) { in => 
      val out = processor.input(in)

      out.forall(o => {
        val outTS = o._2.get("timestamps")
        (outTS.getModule() == "PrimaryProcessor")
      })
    }
  }.check

【问题讨论】:

  • 测试失败。任务成功。
  • """[info] 测试:成功 3,失败 0,取消 0,忽略 0,待处理 0""" --- 失败测试为 0。
  • 好的。你能提供失败但通过的测试吗?您用来运行的命令(我假设是 sbt 测试)以及 sbt 和 scalatest 的版本。
  • 我已经用您询问的信息更新了帖子。是的,我运行 sbt test 或 testOnly。

标签: scala scalatest scalacheck


【解决方案1】:

由于您使用 ScalaTest 样式的基于属性的测试,而不是 ScalaCheck 样式的属性测试,因此您的属性需要返回 MatcherAssertion 而不是 Boolean 表达式。

来自documentation

在 ScalaTest 属性样式中,您使用单词无论何时代替 ==> 以及断言或匹配器表达式而不是布尔表达式

在下面的示例中,您可以使用该示例进行测试。该测试测试 2 个连接字符串的长度是否始终大于连接中使用的任何字符串的长度。当两个字符串都为空时,这应该会失败

编译但测试通过,因为我们使用的是布尔表达式

import org.scalatest.prop.PropertyChecks
import org.scalatest.{FlatSpec, Matchers}

final class StringSpec extends FlatSpec with Matchers with PropertyChecks {

  behavior of "String"

  it should "concatenate" in {
    forAll { (a: String, b: String) =>
      (a + b).length > a.length && (a + b).length > b.length
    }
  }
}

按预期编译并失败,因为它使用了 `Matchers

import org.scalatest.prop.PropertyChecks
import org.scalatest.{Assertion, FlatSpec, Matchers}

final class StringSpec extends FlatSpec with Matchers with PropertyChecks {

  behavior of "String"

  it should "concatenate" in {
    forAll { (a: String, b: String) =>
      (a + b).length should be > a.length
      (a + b).length should be >= b.length
    }
  }
}

【讨论】:

  • 感谢您的回复。虽然这是正确的方向,但我发现另一个 SO question 已经给出了更准确的答案。我已将此标记为另一个 SO 问题的副本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多