From 38fa73c007a121c59850d7b2435e33d27f52c432 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 11:47:23 +0100 Subject: [PATCH 01/12] docs: add "Use string expression with types" to testing practices --- src/content/docs/testing/testing.md | 34 ++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 5922595..25a4733 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -108,6 +108,22 @@ expect(people, hasLength(3)); expect(valid, isTrue); ``` +## Use string expression with types + +Test names are [Strings](https://dart.dev/language/built-in-types#strings), if you're referencing a type, use a string expression to ease renaming the type. + +Bad ❗️ + +```dart +testWidgets('renders YourView', (tester) async {}); +``` + +Good ✅ + +```dart +testWidgets('renders $YourView', (tester) async {}); +``` + ## Descriptive test Don't be afraid of being verbose in your tests. Make sure everything is readable, which can make it easier to maintain over time. @@ -123,12 +139,10 @@ blocTest('emits',); Good ✅ ```dart -testWidgets('renders YourView', (tester) async {}); -testWidgets('renders YourView for YourState', (tester) async {}); +testWidgets('renders $YourView', (tester) async {}); +testWidgets('renders $YourView for $YourState', (tester) async {}); test('given an [input] is returning the [output] expected', () async {}); -blocTest('emits StateA if ...',); - - +blocTest('emits $StateA if ...',); ``` ## Test with a single purpose @@ -138,14 +152,14 @@ Aim to test one scenario per test. You might end up with more tests in the codeb Bad ❗️ ```dart -testWidgets('renders widgetA and widgetB', (tester) async {}); +testWidgets('renders $WidgetA and $WidgetB', (tester) async {}); ``` Good ✅ ```dart -testWidgets('renders widgetA', (tester) async {}); -testWidgets('renders widgetB', (tester) async {}); +testWidgets('renders $WidgetA', (tester) async {}); +testWidgets('renders $WidgetB', (tester) async {}); ``` ## Use keys carefully @@ -211,7 +225,7 @@ void main() { // mock api client methods... }); - group('UserRepository', () { + group('$UserRepository', () { // Tests... }); } @@ -221,7 +235,7 @@ Good ✅ ```dart void main() { - group('UserRepository', () { + group('$UserRepository', () { late ApiClient apiClient; setUp(() { From e224539369f17bd63923955f9e11d0b802799613 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 15:54:36 +0100 Subject: [PATCH 02/12] docs: update description --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 25a4733..4951fc2 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -110,7 +110,7 @@ expect(valid, isTrue); ## Use string expression with types -Test names are [Strings](https://dart.dev/language/built-in-types#strings), if you're referencing a type, use a string expression to ease renaming the type. +[Test descriptions](https://pub.dev/documentation/test/latest/test/test.html) are usually `String`s. If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. Bad ❗️ From a8a1f7e50c2e1bd12c9355447416b0d69ebef7b5 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:01:08 +0100 Subject: [PATCH 03/12] docs: update docs --- src/content/docs/testing/testing.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 4951fc2..06faf35 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -110,7 +110,7 @@ expect(valid, isTrue); ## Use string expression with types -[Test descriptions](https://pub.dev/documentation/test/latest/test/test.html) are usually `String`s. If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. +[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted to a string. If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. Bad ❗️ @@ -124,6 +124,20 @@ Good ✅ testWidgets('renders $YourView', (tester) async {}); ``` +If your (https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: + +Bad ❗️ + +```dart +group('$YourView', () {}); +``` + +Good ✅ + +```dart +group(YourView, () {}); +``` + ## Descriptive test Don't be afraid of being verbose in your tests. Make sure everything is readable, which can make it easier to maintain over time. From d8abf37a3936c7c94e0b272288ebdcfcbe612b54 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:02:49 +0100 Subject: [PATCH 04/12] docs: added toString documentation --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 06faf35..4e2a78d 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -110,7 +110,7 @@ expect(valid, isTrue); ## Use string expression with types -[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted to a string. If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. +[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted [to a string](https://api.flutter.dev/flutter/dart-core/Object/toString.html). If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. Bad ❗️ From 1b58ac7cfd651b1f040485ca4f49caef8f93048b Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:03:06 +0100 Subject: [PATCH 05/12] docs: capitalize String --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 4e2a78d..9471466 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -110,7 +110,7 @@ expect(valid, isTrue); ## Use string expression with types -[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted [to a string](https://api.flutter.dev/flutter/dart-core/Object/toString.html). If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. +[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted [to a String](https://api.flutter.dev/flutter/dart-core/Object/toString.html). If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. Bad ❗️ From 737f6350d74363bd03b382cee3046adf9d1c8a39 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:03:55 +0100 Subject: [PATCH 06/12] docs: typo --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 9471466..e1d3fd0 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -124,7 +124,7 @@ Good ✅ testWidgets('renders $YourView', (tester) async {}); ``` -If your (https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: +If your [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: Bad ❗️ From 89829ca09e1ec67af737704bf13eaa75b47b6bb1 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:05:26 +0100 Subject: [PATCH 07/12] docs: update testing doc --- src/content/docs/testing/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index e1d3fd0..7f68ba7 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -239,7 +239,7 @@ void main() { // mock api client methods... }); - group('$UserRepository', () { + group(UserRepository, () { // Tests... }); } @@ -249,7 +249,7 @@ Good ✅ ```dart void main() { - group('$UserRepository', () { + group(UserRepository, () { late ApiClient apiClient; setUp(() { From e178d622d262e20410923c511f48666a33e69811 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:06:57 +0100 Subject: [PATCH 08/12] docs: update MySubject --- src/content/docs/testing/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 7f68ba7..eab4132 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -285,7 +285,7 @@ class _MySubject { } void main() { - group('$_MySubject', () { + group(_MySubject, () { final _MySubjectDependency myDependency = _MySubjectDependency(); test('value starts at 0', () { @@ -311,7 +311,7 @@ Good ✅ ```dart void main() { - group('$_MySubject', () { + group(_MySubject, () { late _MySubjectDependency myDependency; setUp(() { From 97a39bc69dfcfdd370a04eb0810693d4eb0eed41 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 12 Jul 2024 16:07:49 +0100 Subject: [PATCH 09/12] docs: use however and test --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index eab4132..6780598 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -124,7 +124,7 @@ Good ✅ testWidgets('renders $YourView', (tester) async {}); ``` -If your [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: +However, if your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: Bad ❗️ From cd86d8cf594e53864da2c88c89a7d16ea8fcb3d4 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Tue, 16 Jul 2024 09:58:33 +0100 Subject: [PATCH 10/12] docs: use consider --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 6780598..061e3ec 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -124,7 +124,7 @@ Good ✅ testWidgets('renders $YourView', (tester) async {}); ``` -However, if your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, you can omit the string expression: +However, if your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, consider omitting the string expression: Bad ❗️ From ef1e22a479a9ff6bbe312afe2305f1b3d5328258 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Tue, 16 Jul 2024 10:01:30 +0100 Subject: [PATCH 11/12] docs: small adjustmenets --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 061e3ec..891b1bb 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -110,7 +110,7 @@ expect(valid, isTrue); ## Use string expression with types -[Test](https://pub.dev/documentation/test/latest/test/test.html) and [group](https://pub.dev/documentation/test/latest/test/group.html) descriptions are converted [to a String](https://api.flutter.dev/flutter/dart-core/Object/toString.html). If you're referencing a type, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type. +If you're referencing a type within a test description, use a [string expression](https://dart.dev/language/built-in-types#string) to ease renaming the type: Bad ❗️ From da4b7353b70f4a57bdc1d01aaa965326474a356a Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Tue, 16 Jul 2024 10:07:05 +0100 Subject: [PATCH 12/12] docs: remove however --- src/content/docs/testing/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/testing/testing.md b/src/content/docs/testing/testing.md index 891b1bb..4e58a9c 100644 --- a/src/content/docs/testing/testing.md +++ b/src/content/docs/testing/testing.md @@ -124,7 +124,7 @@ Good ✅ testWidgets('renders $YourView', (tester) async {}); ``` -However, if your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, consider omitting the string expression: +If your [test](https://pub.dev/documentation/test/latest/test/test.html) or [group](https://pub.dev/documentation/test/latest/test/group.html) description only contains a type, consider omitting the string expression: Bad ❗️