testWidgetsRandom function

  1. @isTest
void testWidgetsRandom(
  1. String description,
  2. TestWidgetsCallback callback, {
  3. int? seed,
  4. bool? skip,
  5. Timeout? timeout,
  6. bool semanticsEnabled = true,
  7. dynamic tags,
})

This function is equivalent to testWidgets(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 number generation within testRandomWidgets() must be performed through the provided random generator.

Example of use:

testRandomWidgets(
  'description',
  (Random random, WidgetTester tester) async {
    ...
  },
);

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 run for that specific seed.

Implementation

@isTest
void testWidgetsRandom(
  String description,
  TestWidgetsCallback callback, {
  int? seed,
  bool? skip,
  Timeout? timeout,
  bool semanticsEnabled = true,
  dynamic tags,
}) {
  seed ??= _seedGenerator.nextInt(_maxSeed);
  testWidgets(
    '$description [seed=$seed]',
    (WidgetTester widgetTester) => callback(Random(seed), widgetTester),
    skip: skip,
    timeout: timeout,
    semanticsEnabled: semanticsEnabled,
    tags: tags,
  );
}