Add variant testing to testWidgets (#45646)

This adds the ability to define variants of tests with different environmental values for a particular testWidgets test.

This allows you to run the same test multiple times with a different test environment. One test variant has been implemented that allows running a test with different settings of the TargetPlatform.
This commit is contained in:
Greg Spencer
2019-12-20 15:00:16 -08:00
committed by GitHub
parent cb98f7222f
commit 19b9206add
2 changed files with 219 additions and 54 deletions

View File

@@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
@@ -665,38 +666,85 @@ void main() {
await tester.showKeyboard(find.byType(TextField));
await tester.pump();
});
testWidgets('verifyTickersWereDisposed control test', (WidgetTester tester) async {
FlutterError error;
final Ticker ticker = tester.createTicker((Duration duration) {});
ticker.start();
try {
tester.verifyTickersWereDisposed('');
} on FlutterError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error.diagnostics.length, 4);
expect(error.diagnostics[2].level, DiagnosticLevel.hint);
expect(
error.diagnostics[2].toStringDeep(),
'Tickers used by AnimationControllers should be disposed by\n'
'calling dispose() on the AnimationController itself. Otherwise,\n'
'the ticker will leak.\n',
);
expect(error.diagnostics.last, isInstanceOf<DiagnosticsProperty<Ticker>>());
expect(error.diagnostics.last.value, ticker);
expect(error.toStringDeep(), startsWith(
'FlutterError\n'
' A Ticker was active .\n'
' All Tickers must be disposed.\n'
' Tickers used by AnimationControllers should be disposed by\n'
' calling dispose() on the AnimationController itself. Otherwise,\n'
' the ticker will leak.\n'
' The offending ticker was:\n'
' _TestTicker()\n',
));
FlutterError error;
final Ticker ticker = tester.createTicker((Duration duration) {});
ticker.start();
try {
tester.verifyTickersWereDisposed('');
} on FlutterError catch (e) {
error = e;
} finally {
expect(error, isNotNull);
expect(error.diagnostics.length, 4);
expect(error.diagnostics[2].level, DiagnosticLevel.hint);
expect(
error.diagnostics[2].toStringDeep(),
'Tickers used by AnimationControllers should be disposed by\n'
'calling dispose() on the AnimationController itself. Otherwise,\n'
'the ticker will leak.\n',
);
expect(error.diagnostics.last, isInstanceOf<DiagnosticsProperty<Ticker>>());
expect(error.diagnostics.last.value, ticker);
expect(error.toStringDeep(), startsWith(
'FlutterError\n'
' A Ticker was active .\n'
' All Tickers must be disposed.\n'
' Tickers used by AnimationControllers should be disposed by\n'
' calling dispose() on the AnimationController itself. Otherwise,\n'
' the ticker will leak.\n'
' The offending ticker was:\n'
' _TestTicker()\n',
));
}
ticker.stop();
});
group('testWidgets variants work', () {
int numberOfVariationsRun = 0;
testWidgets('variant tests run all values provided', (WidgetTester tester) async {
if (debugDefaultTargetPlatformOverride == null) {
expect(numberOfVariationsRun, equals(TargetPlatform.values.length));
} else {
numberOfVariationsRun += 1;
}
ticker.stop();
}, variant: TargetPlatformVariant(TargetPlatform.values.toSet()));
testWidgets('variant tests have descriptions with details', (WidgetTester tester) async {
if (debugDefaultTargetPlatformOverride == null) {
expect(tester.testDescription, equals('variant tests have descriptions with details'));
} else {
expect(tester.testDescription, equals('variant tests have descriptions with details ($debugDefaultTargetPlatformOverride)'));
}
}, variant: TargetPlatformVariant(TargetPlatform.values.toSet()));
});
group('TargetPlatformVariant', () {
int numberOfVariationsRun = 0;
TargetPlatform origTargetPlatform;
setUpAll((){
origTargetPlatform = debugDefaultTargetPlatformOverride;
});
tearDownAll((){
expect(debugDefaultTargetPlatformOverride, equals(origTargetPlatform));
});
testWidgets('TargetPlatformVariant.only tests given value', (WidgetTester tester) async {
expect(debugDefaultTargetPlatformOverride, equals(TargetPlatform.iOS));
expect(defaultTargetPlatform, equals(TargetPlatform.iOS));
}, variant: TargetPlatformVariant.only(TargetPlatform.iOS));
testWidgets('TargetPlatformVariant.all tests run all variants', (WidgetTester tester) async {
if (debugDefaultTargetPlatformOverride == null) {
expect(numberOfVariationsRun, equals(TargetPlatform.values.length));
} else {
numberOfVariationsRun += 1;
}
}, variant: TargetPlatformVariant.all());
});
}