2

我正在使用 Hspec 和 Quickcheck http://hspec.github.io/运行测试

提供的执行随机测试用例的示例是

it "returns the first element of an *arbitrary* list" $
      property $ \x xs -> head (x:xs) == (x :: Int)

与相关的输出:

returns the first element of an *arbitrary* list 

如何查看为测试生成的实际运行时值?因此,在上面的示例中,示例所需的输出将包括为 x 和 xs 传递的值,例如:

returns the first element of an *arbitrary* list 
    \x xy head (x:xs) == (x :: Int) with x = 'a' and xs = "bc" holds 
4

2 回答 2

1

不会。Hspec 的运行程序会禁用任何 QuickCheck 输出。此外,随机测试会产生很多噪音。但是,有一个解决方法:

import Test.Hspec
import Test.QuickCheck
import Test.QuickCheck.Test (isSuccess)

verboseProperty :: Testable prop => prop -> Expectation
verboseProperty p = verboseCheckResult p >>= (`shouldBe` True) . isSuccess

main = hspec $ describe "head" $
  it "returns the first element of an *arbitrary* list" $
    verboseProperty $ \x xs -> head (x:xs) == (x :: Int)

但是,格式会有所不同:

returns the first element of an *arbitrary* list
Passed:  
0
[]
Passed: 
1
[-1]
Passed:  
2
[-2]
Passed:  
3
[]
Passed:  
0
[]
Passed:  
1
[2,-5,5,5]
Passed:  
0
[-3,-1,-5,3]
…

当然还有更多的缺点,但这可能是一个简单的出路。但是通过测试并不是那么有趣,更重要的是反例——默认显示。

于 2017-03-13T09:53:56.047 回答
1

使用新版本的 HSpec (>= 2.5.0) 你只需要这样写:

it "returns the first element of an *arbitrary* list" $
  verbose $ \x xs -> head (x:xs) == (x :: Int)

看看这里的讨论:https ://github.com/hspec/hspec/issues/257

于 2018-04-11T17:33:14.483 回答