我的代码在使用 FsCheck 时自动测试从 -99 到 99 的值。
Check.Quick test
我的测试函数采用整数值。
我想使用从 1 到 4999 的值进行测试。
您可以Gen.elements
结合使用Prop.forAll
:
let n = Gen.elements [-99..99] |> Arb.fromGen
let prop = Prop.forAll n (fun number ->
// Test goes here - e.g.:
Assert.InRange(number, -99, 99))
prop.QuickCheck()
Gen.elements
接受一系列有效值并从该序列创建一个统一的生成器。Prop.forAll
使用该自定义生成器定义一个属性。
您可以将它与 xUnit.net 的 FsCheck 的 Glue 库结合使用,这是我的首选方法:
[<Property>]
let ``Number is between -99 and 99`` () =
let n = Gen.elements [-99..99] |> Arb.fromGen
Prop.forAll n (fun number ->
// Test goes here - e.g.:
Assert.InRange(number, -99, 99))
By default, FsCheck generates integers between 1 and 100. You can change this by supplying a Config object to the Check.
let config = {
Config.Quick with
EndSize = 4999
}
Check.One(config,test)
EndSize indicates the size to use for the last test, when all the tests are passing. The size increases linearly between StartSize, which you can also set should you wish to generate test data in a range starting from some value other than 1, and EndSize. See the implementation of type Config
in https://github.com/fscheck/FsCheck/blob/master/src/FsCheck/Runner.fs