4

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.

Philip John
  • 5,275
  • 10
  • 43
  • 68

2 Answers2

6

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))
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    Also consider using `Gen.choose` here - would be more efficient (doesn't allocate the list of integers). But `Gen.elements` generalizes to other types more straightforwardly. – Kurt Schelfthout Sep 22 '15 at 12:23
0

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

Richard Polton
  • 101
  • 1
  • 5
  • This is somewhat misleading. The size is a positive integer, which limits the size of the type being generated - but each type has its own interpretation of what size means. For an `int` i, it means `abs(i) <= size`. For a list, it means the maximum length of the generated list. Etc. – Kurt Schelfthout Sep 22 '15 at 12:26