FsCheck test change the range of values used for testing

1.2k views Asked by At

My code is automatically testing for values from -99 to 99 while using FsCheck.

Check.Quick test

where my test function takes integer values.

I would like to test using values from 1 to 4999.

2

There are 2 answers

1
Mark Seemann On BEST ANSWER

You can use Gen.elements combined with 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 takes a sequence of valid values and creates a uniform generator from that sequence. Prop.forAll defines a property with that custom generator.

You can combine it with FsCheck's Glue Library for xUnit.net, which is my preferred method:

[<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))
1
Richard Polton On

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