Support tags in testWidgets (#55141)

This commit is contained in:
Katarina Sheremet 2020-04-29 00:49:01 +02:00 committed by GitHub
parent 7b1d24216e
commit 3b067049ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 0 deletions

View File

@ -53,3 +53,4 @@ Efthymios Sarpmpanis <e.sarbanis@gmail.com>
Cédric Wyss <cedi.wyss@gmail.com>
Michel Feinstein <michel@feinstein.com.br>
Michael Lee <ckmichael8@gmail.com>
Katarina Sheremet <katarina@sheremet.ch>

View File

@ -0,0 +1,14 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('included', (WidgetTester tester) async {
expect(2 + 2, 4);
}, tags: <String>['include-tag']);
testWidgets('excluded', (WidgetTester tester) async {
throw 'this test should have been filtered out';
}, tags: <String>['exclude-tag']);
}

View File

@ -89,6 +89,9 @@ typedef WidgetTesterCallback = Future<void> Function(WidgetTester widgetTester);
/// each value of the [TestVariant.values]. If [variant] is not set, the test
/// will be run once using the base test environment.
///
/// If the [tags] are passed, they declare user-defined tags that are implemented by
/// the `test` package.
///
/// See also:
///
/// * [AutomatedTestWidgetsFlutterBinding.addTime] to learn more about
@ -112,6 +115,7 @@ void testWidgets(
Duration initialTimeout,
bool semanticsEnabled = true,
TestVariant<Object> variant = const DefaultTestVariant(),
dynamic tags,
}) {
assert(variant != null);
assert(variant.values.isNotEmpty, 'There must be at least on value to test in the testing variant');
@ -150,6 +154,7 @@ void testWidgets(
},
skip: skip,
timeout: timeout ?? binding.defaultTestTimeout,
tags: tags,
);
}
}

View File

@ -125,6 +125,35 @@ void main() {
expect(result.exitCode, 1);
});
testUsingContext('flutter test should run a widgetTest with a given tag', () async {
Cache.flutterRoot = '../..';
final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory,
extraArguments: const <String>['--tags', 'include-tag']);
if (!(result.stdout as String).contains('+1: All tests passed')) {
fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
}
expect(result.exitCode, 0);
});
testUsingContext('flutter test should not run a widgetTest with excluded tag', () async {
Cache.flutterRoot = '../..';
final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory,
extraArguments: const <String>['--exclude-tags', 'exclude-tag']);
if (!(result.stdout as String).contains('+1: All tests passed')) {
fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
}
expect(result.exitCode, 0);
});
testUsingContext('flutter test should run all widgetTest when tags are unspecified', () async {
Cache.flutterRoot = '../..';
final ProcessResult result = await _runFlutterTest('filtering_tag_widget', automatedTestsDirectory, flutterTestDirectory);
if (!(result.stdout as String).contains('+1 -1: Some tests failed')) {
fail('unexpected output from test:\n\n${result.stdout}\n-- end stdout --\n\n');
}
expect(result.exitCode, 1);
});
testUsingContext('flutter test should test runs to completion', () async {
Cache.flutterRoot = '../..';
final ProcessResult result = await _runFlutterTest('trivial', automatedTestsDirectory, flutterTestDirectory,