Search code examples
haskellquickcheck

Custom QuickCheck failed message


test1 = hspec $ do
  describe "blabla" $ do
    it "should be equl" $ verbose $
      \input-> ...

In the above code, when a test failed, it prints the failed input. But I'm actually interested in another value that can be calculated from input. Can I ask QuickCheck to print the other value?


Solution

  • Somehow I've never seen it advertised, but you can use hspecs expectations inside of QuickCheck properties. Here is an example:

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

    Above property is clearly not true, so it should fail. Since we are not only interested input, but also want to know the computed values from it, shouldBe will give us just that:

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

    Naturally, due to verbose, only input will be printed for passing tests, while computed value (eg. round input) will only be printed for a failed test case, which is what you were looking for it seems anyways.