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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

+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)?
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.
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.
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). :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.