尽管失败,为什么测试标记为通过
Why are test marked as passed in spite of failures
我正在使用 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
由于您使用的是基于 属性 测试的 ScalaTest 风格,而不是 属性 测试的 ScalaCheck 风格,因此您的属性需要 return Matcher
或Assertion
而不是 Boolean
表达式。
In the ScalaTest property style you use the word whenever instead of ==> and either an assertion or matcher expression instead of a boolean expression
您可以在下面的示例中对此进行测试。该测试测试 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
}
}
}
我正在使用 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
由于您使用的是基于 属性 测试的 ScalaTest 风格,而不是 属性 测试的 ScalaCheck 风格,因此您的属性需要 return Matcher
或Assertion
而不是 Boolean
表达式。
In the ScalaTest property style you use the word whenever instead of ==> and either an assertion or matcher expression instead of a boolean expression
您可以在下面的示例中对此进行测试。该测试测试 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
}
}
}