From 3b067049adc14ffb300e78d9b0d0adf3d581de7c Mon Sep 17 00:00:00 2001 From: Katarina Sheremet Date: Wed, 29 Apr 2020 00:49:01 +0200 Subject: [PATCH] Support tags in testWidgets (#55141) --- AUTHORS | 1 + .../filtering_tag_widget_test.dart | 14 +++++++++ .../flutter_test/lib/src/widget_tester.dart | 5 ++++ .../commands.shard/permeable/test_test.dart | 29 +++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 dev/automated_tests/flutter_test/filtering_tag_widget_test.dart diff --git a/AUTHORS b/AUTHORS index 05962ad7c5..901b250547 100644 --- a/AUTHORS +++ b/AUTHORS @@ -53,3 +53,4 @@ Efthymios Sarpmpanis Cédric Wyss Michel Feinstein Michael Lee +Katarina Sheremet diff --git a/dev/automated_tests/flutter_test/filtering_tag_widget_test.dart b/dev/automated_tests/flutter_test/filtering_tag_widget_test.dart new file mode 100644 index 0000000000..80f2dc039f --- /dev/null +++ b/dev/automated_tests/flutter_test/filtering_tag_widget_test.dart @@ -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: ['include-tag']); + testWidgets('excluded', (WidgetTester tester) async { + throw 'this test should have been filtered out'; + }, tags: ['exclude-tag']); +} diff --git a/packages/flutter_test/lib/src/widget_tester.dart b/packages/flutter_test/lib/src/widget_tester.dart index aa74d833c5..feaea6bf9b 100644 --- a/packages/flutter_test/lib/src/widget_tester.dart +++ b/packages/flutter_test/lib/src/widget_tester.dart @@ -89,6 +89,9 @@ typedef WidgetTesterCallback = Future 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 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, ); } } diff --git a/packages/flutter_tools/test/commands.shard/permeable/test_test.dart b/packages/flutter_tools/test/commands.shard/permeable/test_test.dart index e2347d1308..5300af1276 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/test_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/test_test.dart @@ -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 ['--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 ['--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,