Questions tagged [fscheck]

FsCheck is a framework for random testing of .NET programs. FsCheck is a port of Haskell's QuickCheck written in F#.

FsCheck is a framework for random testing of .NET programs. FsCheck is a port of Haskell's QuickCheck written in F#.

To test a program with FsCheck, you provide a specification of the program, in the form of properties which functions, methods or objects should satisfy, and FsCheck then tests that the properties hold in a large number of randomly generated cases. While writing the properties, you are actually writing a testable specification of your program. Specifications are expressed in F#, C# or VB, using a flexible and clean API. It sounds lofty, but it's more fun than unit testing!

You can write properties, observe the distribution of test data, and define test data generators. When a property fails, FsCheck automatically displays a minimal counter example - this process, called shrinking, is very useful to quickly find the bug. Instead of digging through a potentially large randomly generated test case to find the part that actually triggers the bug, FsCheck can do that work for you!

151 questions
3
votes
1 answer

Best way to generate random indices into an array?

Good afternoon. I have a set of values from which I'd like to draw a random subset. My first thought was this: let getRandomIndices size count = if size >= count then let r = System.Random() r.GetValues(0,size) |> Seq.take count |>…
StevePoling
  • 319
  • 1
  • 15
3
votes
1 answer

How do I use a custom generator for IDisposable types?

Please note: This is a question regarding fscheck and not on the general use of IDisposable in C# Let's say I have the following class (exaggeration for illustration purposes): public class MyDataTypeWrapper : IDisposable { private MyDataType…
cristobalito
  • 4,192
  • 1
  • 29
  • 41
3
votes
2 answers

How to generate tuples by FsCheck

This is a json generation : let strGen = Arb.Default.String() |> Arb.toGen strGen |> Gen.arrayOf |> Gen.map (String.concat "\", \"") |> Gen.map (fun strs -> "[\"" + strs + "\"]") How can I have the string that the json…
Mohsen
  • 4,000
  • 8
  • 42
  • 73
3
votes
1 answer

Generate Json array by FsCheck

I'm new to FsCheck and I need an array of json to perform my tests. I want a simple solution like the following: let commaSeparated = Gen.arrayOf Arb.generate |> Gen.fold(fun (acc,i)-> i+="\",\"") …
Mohsen
  • 4,000
  • 8
  • 42
  • 73
3
votes
1 answer

How do I make use of FsCheck custom generators from C#?

I have the following code: var gen = from x in Arb.Generate() from int y in Gen.Choose(5, 10) where x > 5 select new tuple { Fst = x, Snd = y }; And I can run Prop.ForAll(c => Console.WriteLine($"{c.Fst},…
Arturo Hernandez
  • 2,749
  • 3
  • 28
  • 36
3
votes
1 answer

Json Provided type gens don't work with "with"

Here is a snippet that demonstrates what I'll talk about well: open FsCheck open FsCheck.Gen open FSharp.Data type Test = JsonProvider<"""{"collection": [ { "Name": "Rob", "Age": 3 } ] } """> let testGen () = gen { let! name =…
Hohohodown
  • 718
  • 1
  • 8
  • 23
3
votes
1 answer

How do I define an FSCheck generator so that it can be discovered

I'm writing an FSCheck generator to create strings having the following properties: They are non-null Trimming them won't affect the length They contain no spaces. Here's my generator code: namespace Example open FsCheck.Arb module public…
Kit
  • 2,089
  • 1
  • 11
  • 23
3
votes
1 answer

FsCheck c# When property combinator

I'm trying to adopt fscheck, but have a very hard time as there is no much documentation for C#. Can you explain, why the following example of using When combinator for properties fails (evidently, I don't understand how to use it properly)? …
user3101007
  • 691
  • 2
  • 7
  • 18
3
votes
1 answer

How to combine 2 Arbitrary instances to match test method signature

I have a function that should get two actual params for testing. Both values shall be created by Arbitrary instances as they need to be of some well formdness that cant be totally arbitrary. So I create the following code let updating…
robkuz
  • 9,488
  • 5
  • 29
  • 50
3
votes
3 answers

Why does using a backwards pipeline operator resolve a compilation error?

The following line is accepted by the compiler: input |> Prop.forAll <| fun (a , b) -> add a b = add b a However, when I replace the backwards pipline operator with parentheses, I receive an error: input |> Prop.forAll ( fun (a , b) -> add a b =…
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
3
votes
2 answers

How to recursively use FsCheck generators?

I use FsCheck for property-based testing, so I defined a set a generators for custom types. Some of types are composed of others, and there are generators for all of them. Having defined a generator for Alphanumeric type, I want to define a…
Vagif Abilov
  • 9,835
  • 8
  • 55
  • 100
3
votes
3 answers

Generating unique strings in FsCheck

I need generate unique non-null strings to be used as Dictionary keys. I tried something like: public static Gen> UniqueStrings() { return from s in Arb.Default.NonNull().Generator select s; } Then I use…
rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
3
votes
1 answer

Negative Property-Based Tests

Property-based testing is good when you can express simple and well-defined properties. I've also had luck with "negative properties" in the case of testing parsers, e.g. by generating invalid identifiers or mismatching indentation for…
sshine
  • 15,635
  • 1
  • 41
  • 66
3
votes
1 answer

Turning a generator based on System.Random into an FsCheck generator

Suppose I am given a generator based on System.Random and I want to turn it into an FsCheck generator: let myGen = MyGen(System.Random()) let fsGen = gen { return myGen.Generate() } There are a couple of issues with this easy solution: the first…
Giacomo Citi
  • 198
  • 1
  • 8
3
votes
1 answer

How to generate probability rates in FsCheck

I would like to test a property where I use 2 probability rates p1 and p2 that must satisfy 0 < p1 < p2 < 1 let arraySizeCheck (p1:float, p2:float, xs:list) = (p1 < p2 && p1 > 0.0 && p1 < 1.0 && p2 > 0.0 && p2 < 1.0 && Seq.length xs > 0)…
carstenj
  • 693
  • 5
  • 13