test method

void test(
  1. String name,
  2. FutureOr body(), {
  3. String? testOn,
  4. Timeout? timeout,
  5. Object? skip,
  6. Map<String, dynamic>? onPlatform,
  7. Object? tags,
  8. TestLocation? location,
  9. int? retry,
  10. bool solo = false,
})

Defines a test case with the given name and body.

Implementation

void test(
  String name,
  FutureOr<dynamic> Function() body, {
  String? testOn,
  Timeout? timeout,
  Object? skip,
  Map<String, dynamic>? onPlatform,
  Object? tags,
  TestLocation? location,
  int? retry,
  bool solo = false,
}) {
  _checkNotBuilt('test');

  final fullName = _prefix(name);
  if (_fullTestName != null && fullName != _fullTestName) {
    return;
  }

  var newMetadata = Metadata.parse(
    testOn: testOn,
    timeout: timeout,
    skip: skip,
    onPlatform: onPlatform,
    tags: tags,
    retry: _noRetry ? 0 : retry,
  );
  newMetadata.validatePlatformSelectors(_platformVariables);
  var metadata = _metadata.merge(newMetadata);
  _addEntry(
    LocalTest(
      fullName,
      metadata,
      () async {
        var parents = <Declarer>[];
        for (
          Declarer? declarer = this;
          declarer != null;
          declarer = declarer._parent
        ) {
          parents.add(declarer);
        }

        // Register all tear-down functions in all declarers. Iterate through
        // parents outside-in so that the Invoker gets the functions in the order
        // they were declared in source.
        for (var declarer in parents.reversed) {
          for (var tearDown in declarer._tearDowns) {
            Invoker.current!.addTearDown(tearDown);
          }
        }

        await runZoned(
          () async {
            await _runSetUps();
            await body();
          },
          // Make the declarer visible to running tests so that they'll throw
          // useful errors when calling `test()` and `group()` within a test.
          zoneValues: {#test.declarer: this},
        );
      },
      trace: _collectTraces ? Trace.current(2) : null,
      location: location,
      guarded: false,
    ),
  );

  if (solo) {
    _soloEntries.add(_entries.last);
  }
}