registerApplicationTests function

Future<void> registerApplicationTests({
  1. required ApplicationTestDriver driver,
  2. required ExecutionMode mode,
  3. String? testsAssetPrefix,
})

Registers a host suite after the caller selected the Flutter test binding.

Implementation

Future<void> registerApplicationTests({
  required ApplicationTestDriver driver,
  required ExecutionMode mode,
  String? testsAssetPrefix,
}) async {
  final prefix = normalizedTestAssetPrefix(
    testsAssetPrefix ??
        const String.fromEnvironment(
          'ensembleTestTestsAssetPrefix',
          defaultValue: 'tests/',
        ),
  );

  final resolved = resolveExecutionMode(mode);
  // Match the standard Ensemble entry point: Firebase is ready by default for
  // widget-mode host apps. Integration mode uses the native host plugins.
  EnsembleTestHarness.configureTestIntegrations(
    resolved == ExecutionMode.widget
        ? const [firebaseTestIntegration]
        : const [],
  );
  if (resolved == ExecutionMode.integration) {
    // Keep integration runs deterministic and offline-capable for Remote
    // Config, just like widget runs. The adapter preserves app defaults and
    // avoids Firebase Installations network calls that are unrelated to the
    // UI under test.
    ensureRemoteConfigForTest();
  }
  configureArtifactTransport(resolved);
  final transport = resolved == ExecutionMode.integration;
  var transportCompleted = false;
  void completeTransportIfNeeded() {
    if (!transport || transportCompleted) return;
    transportCompleted = true;
    emitEnsembleTestArtifactTransportComplete();
  }

  // Uncaught plugin errors can finish the Flutter test before the suite
  // `finally` runs. tearDown still emits complete so the CLI gets a report.
  tearDown(() {
    completeTransportIfNeeded();
    EnsembleTestHarness.disposeTestStorage();
  });

  testWidgets('Application *.test.yaml', (tester) async {
    await EnsembleTestHarness.runWithRealNetworkHttpOverrides(() =>
        withFlutterErrorIsolation(() async {
          if (transport) {
            emitEnsembleTestArtifactTransportBegin();
          }
          var emittedResult = false;
          try {
            final workerBatch = await workerBatchFromEnvironment();
            final plan = workerBatch?.plan ??
                await FlutterTestExecutionPlanner.build(
                  testsAssetPrefix: prefix,
                  inputs: testInputsFromEnvironment(),
                  selection: testSelectionFromEnvironment(),
                );
            final serializedPlans = workerBatch?.serializedPlans ?? const {};
            final runResult = await runApplicationTestPlan(
              driver: driver,
              plan: plan,
              tester: tester,
              mode: resolved,
              serializedPlans: serializedPlans,
              onTestStart: emitTestProgressStart,
              onTestComplete: emitTestProgressComplete,
            );
            final reporter = TestReporter();
            print(
              reporter.formatSummary(runResult, testFile: '$prefix*.test.yaml'),
            );
            emitMachineTestReport(runResult);
            emittedResult = true;
            if (runResult.failedCount > 0) {
              fail(reporter.formatFailureSummary(runResult));
            }
          } catch (error, stackTrace) {
            // A failing case is intentionally surfaced to flutter_test after its
            // detailed result has been emitted. Do not replace that report with a
            // synthetic test-process row when catching the assertion used to fail
            // the suite.
            if (emittedResult) rethrow;
            final runResult = EnsembleTestRunResult(
              results: [
                EnsembleSingleTestResult.failed(
                  testId: 'test-process',
                  durationMs: 0,
                  message: error.toString(),
                  stackTrace: stackTrace.toString(),
                ),
              ],
              suiteLogs: const [],
              metadata: {
                'mode': resolved.name,
                'launchKind': 'applicationProvided',
              },
            );
            final reporter = TestReporter();
            print(reporter.formatSummary(runResult,
                testFile: '$prefix*.test.yaml'));
            emitMachineTestReport(runResult);
            fail(reporter.formatFailureSummary(runResult));
          } finally {
            completeTransportIfNeeded();
          }
        }));
  });
}