Changing Probabilities
Explore how changing probabilities in custom generators allows precise control over data generation in property-based testing. Understand why default filters and transformations fall short and how to use probabilistic generators like frequency() to create more realistic test data, enhancing your testing strategies with PropEr in Erlang.
We'll cover the following...
Why is changing probabilities required?
To understand why and when changing probabilities is useful, let’s take a look at an example. Let’s imagine that we have a generator that looks for ISO Latin1 strings by using the io_lib:printable_latin1_list function, which will let us restrict the range of string():
latin1_string() ->
?SUCHTHAT(S, string(), io_lib:printable_latin1_list(S)).
Similarly as with Unicode, ensuring that nothing goes out of range looks like the following:
unicode_string() ->
?SUCHTHAT(S, string(), io_lib:printable_unicode_list(S)).
With transforms, filters, and resizes, we can get pretty far in terms of retargeting our generators to do what we want. The latin1 generator shows something interesting, though. The default string() generator has a large search space, and therefore filtering out the unwanted data can be expensive. On the other hand, most Unicode characters can’t be represented within latin1, and ...