After gaving my talk about Property Based Testing, I was contacted by one of the participants with the following question:
How to change the number of test runs?
In case you have no idea what I’m talking about; by default the property based testing library(FSCheck in my case) will generate a number of inputs and run the tests for all these inputs. By default 100 inputs are generated and tested:
You can change the number of test runs either by setting the MaxTest
property on the [Property]
attribute:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Property(MaxTest =200)] | |
public Property Adding_Two_Numbers_Doesnt_Depend_On_Parameter_Order_3(Tuple<int, int> values) | |
{ | |
return (Add(values.Item1, values.Item2) == Add(values.Item1, values.Item2)).ToProperty(); | |
} |
Or by passing a configuration object when calling Prop.ForAll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void WhenIAddTwoRandomNumbersTheResultShouldNotDependOnParameterOrder_2() | |
{ | |
var config = Configuration.Default; | |
config.MaxNbOfTest = 200; | |
var addingTwoRandomNumbers = (int x, int y) => Add(x, y) == Add(y, x); | |
Prop.ForAll(new Func<int, int, bool>(addingTwoRandomNumbers)) | |
.Check(config); | |
} |