为什么测试标记为尽管失败了

时间:2017-09-11 13:42:33

标签: scala scalatest scalacheck

我使用Scalatest / Scalacheck和自定义生成器。我观察到,即使某些测试失败,测试也会被标记为成功。在下面的示例test" 应该添加处理时间戳"是伪造。然而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

1 个答案:

答案 0 :(得分:0)

由于您使用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
    }
  }
}