testRandom function

  1. @isTest
void testRandom(
  1. String name,
  2. void body(
    1. Random random
    ), {
  3. int? seed,
  4. String? testOn,
  5. Timeout? timeout,
  6. dynamic skip,
  7. dynamic tags,
  8. Map<String, dynamic>? onPlatform,
  9. int? retry,
  10. int repeatCount = 1,
})

This function is equivalent to test(name, body), except that it is better suited for randomized testing: it will create a Random generator and pass it to the test body, but also record the seed that was used for creating the random generator. Thus, if a test fails for a specific rare seed, it would be easy to reproduce this failure.

In order for this to work properly, all random numbers used within testRandom() must be obtained through the provided random generator.

Example of use:

testRandom('description', (Random random) {
  expect(random.nextDouble() == random.nextDouble(), false);
});

Then if the test output shows that the test failed with seed s, simply adding parameter seed=s into the function will force it to use that specific seed.

Optional parameter repeatCount allows the test to be repeated multiple times, each time with a different seed.

Implementation

@isTest
void testRandom(
  String name,
  void Function(Random random) body, {
  int? seed,
  String? testOn,
  Timeout? timeout,
  dynamic skip,
  dynamic tags,
  Map<String, dynamic>? onPlatform,
  int? retry,
  int repeatCount = 1,
}) {
  assert(repeatCount > 0, 'repeatCount needs to be a positive number');
  for (var i = 0; i < repeatCount; i++) {
    final seed0 = seed ?? _seedGenerator.nextInt(_maxSeed);
    test(
      '$name [seed=$seed0]',
      () => body(Random(seed0)),
      testOn: testOn,
      timeout: timeout,
      skip: skip,
      tags: tags,
      onPlatform: onPlatform,
      retry: retry,
    );
  }
}