Skip to content

Conversation

@pq
Copy link
Contributor

@pq pq commented Jul 19, 2017

Adds a --with-widget-test option to flutter create that produces a sample widget test in test/widget_test.dart.

We'll likely default to using this option in the IDE (flutter/flutter-intellij#1171). Alternatively, we could default this to true in create.dart (which I'm open to).

Template content was derived from https://flutter.io/testing/#unit-testing. Word and content-smithing welcome. 👍

@LarkAscending @Hixie @yjbanov

Running `flutter create —with-widget-test` produces a test/widget_test.dart sample widget test.
Copy link
Contributor

@yjbanov yjbanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but we should test that generated test passes.

@@ -0,0 +1,44 @@
// This is a basic Flutter widget test.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure that running flutter test on this test actually passes. Perhaps add it to https://github.com/flutter/flutter/blob/master/dev/bots/test.dart?

@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

Please just make the test always be included, rather than making it optional.

@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

Fixes #4080

@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

We should probably test the test in the same test that analyzes the output of flutter create.

@pq
Copy link
Contributor Author

pq commented Jul 20, 2017

@Hixie : would you mind taking another look?

Analysis of created bits should now be correct (if a little hacky) for plugins and package projects.

import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('my first widget test', (WidgetTester tester) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give it a more descriptive title

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Especially when we're testing something interesting!


// Verifies that the widget updated the value correctly
expect(value, 0.5);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this test actually test the app that we created?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. it could test that tapping on the FAB increments the counter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hixie : looking at improving the sample now. Are you OK with this landing and us continuing a separate PR w/ improvements? I suspect that we may want a bit of back and forth (and possibly other input). For example, should we bake a unique key into the sample app itself? Are there specific testing idioms we want to encourage?

Future<Null> _createAndAnalyzeProject(
Directory dir, List<String> createArgs, List<String> expectedPaths,
[List<String> unexpectedPaths = const <String>[]]) async {
{List<String> unexpectedPaths = const <String>[], bool plugin = false}) async {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spaces around named arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

));

final List<String> args = <String>[flutterToolsPath, 'analyze'];
if (target != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no braces around single-line if statement blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@Hixie
Copy link
Contributor

Hixie commented Jul 21, 2017

I think we should check this in with a test that tests the app. I don't think it's valuable to have a sample test if the sample test isn't testing the app, because it'll just confuse people.

The sample test would be really simple. Just pumpWidgets one of the widgets introduced in the main template, expect the counter number is 0 (and not 1), tap the FAB byType, pump, expect the new counter number is 1 (and not 0). Shouldn't need any changes to the main app.

@pq
Copy link
Contributor Author

pq commented Jul 21, 2017

@Hixie : here's a quick stab at more relevant test content. Plenty of room for improvement but it's a start... Input welcome!

void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: i would put this on one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Hixie
Copy link
Contributor

Hixie commented Jul 21, 2017

LGTM

@pq pq merged commit f0c2d5e into flutter:master Jul 21, 2017
goderbauer added a commit that referenced this pull request Jul 24, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants