自定义快速检查失败消息

McB*_*den 5 haskell quickcheck

test1 = hspec $ do
  describe "blabla" $ do
    it "should be equl" $ verbose $
      \input-> ...
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当测试失败时,它会打印失败的input. 但我实际上对可以从 计算出的另一个值感兴趣input。我可以要求QuickCheck打印其他值吗?

leh*_*ins 3

不知何故,我从未见过它的广告,但您可以在属性hspec内使用 s 期望QuickCheck。这是一个例子:

  describe "blabla" $ do
    it "should be equl" $ verbose $ \input ->
      round input `shouldBe` floor (input :: Double)
Run Code Online (Sandbox Code Playgroud)

上述属性显然不正确,因此它应该失败。由于我们不仅感兴趣input,而且还想知道从中计算出的值,因此shouldBe我们将得到:

  3) blabla should be equl
       Falsifiable (after 2 tests and 4 shrinks):
         0.6
       expected: 0
        but got: 1
Run Code Online (Sandbox Code Playgroud)

当然,由于verbose, 仅input会在通过测试时打印,而计算值(例如round input)只会在失败的测试用例中打印,无论如何,这似乎就是您所寻找的。