Generate random string with FsCheck using C#
Asked Answered
I

1

7

I'd like to gradually integrate FsCheck in my C# test code (as first step).

I'd like to randomly generate part of my input data.

This is how I generate a random string:


static string RandomString() {
  var kgen = Gen.Constant(Gen.Sized(g => Gen.OneOf(Arb.Generate())));
  var sgen = Gen.Sample(1, 10, kgen).First();
  var str = Gen.Eval(10, Random.StdGen.NewStdGen(0, 1000), sgen);
  return str;
}

If I call it multiple times, I get each time the same string.

How can I get a different string each time and/or correctly writing this code?

Irrefutable answered 14/8, 2015 at 15:59 Comment(0)
B
9

You should replace your tests with properties, instead of trying to generate random strings and then use those in your tests manually. FsCheck is not geared towards using it as a random generator, although it is possible to coerce it to that. Something like:

var maxLength = 10
return Arb.Generate<string>().Sample(maxLength, 1).Single()

should generate a new random string of length up to 10 "most of the time", i.e. if I remember correctly the random seed is time based. So if you call it twice in the same interval it'll return the same string.

Doing it this way won't let you take advantage of shrinking and the API in Prop for example to observe and classify the generated data, and e.g. restrict it: https://fscheck.github.io/FsCheck/Properties.html

Bottoms answered 15/8, 2015 at 8:49 Comment(4)
+1 And this asnwers to my question, @Kurt Schelfthout. I understand the concept of testing by properties, but since I've already a whole test project. I thought starting adding random data in tests like this. Do you think that this is a bad path? Do you think it's better adding a new F# project to solution and think new tests by properties (e.g.: taking as "spec" old standard tests)?Irrefutable
I see - haven't tried that myself but yes, my advice is to leave the existing tests as is (this guarantees you won't lose any test coverage) and gradually augment the existing tests with "real" properties. Then, as you gain confidence with the property-based tests, you can consider deleting some of the existing ones that are superfluous.Bottoms
By the way, you should be able to mix both in the same project. I do this all the time. E.g. for specific bug repros I feel safer if I add a specific test with the failing example separately, even if that counter-example was found by a property based test first.Bottoms
thanks for suggestions. If I've to go side-by-side with new property based testing, I prefer code the a new test library in F# that's a more natural use for FsCheck (I guess). :)Irrefutable

© 2022 - 2024 — McMap. All rights reserved.