Skip to content

Commit fbb323d

Browse files
committed
✅ Improved bad example test case and added clearer descriptions
1 parent 26b03c8 commit fbb323d

File tree

3 files changed

+40
-47
lines changed

3 files changed

+40
-47
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ parameterizedTest(
240240
},
241241
);
242242
```
243+
> Note: This is a example test to showcase async tests are also possible.
244+
> But this is not a good practice to use a delay like
245+
> this in a test. Running this test will take longer. This could be
246+
> fixed by using a package like [fake_async](https://pub.dev/packages/fake_async).
247+
243248
### Test with CSV data
244249
Its also possible to combine parameterizedTest for example with the [csv](https://pub.dev/packages/csv) package.
245250

example/parameterized_test_example.dart

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313
2,
1414
3,
1515
],
16-
(int value) {
16+
(int value) {
1717
final result = value < 4;
1818
expect(result, true);
1919
},
@@ -38,22 +38,11 @@ void main() {
3838
});
3939

4040
// Test containing a list of enums
41-
42-
// enum TestEnum {
43-
// one(3),
44-
// two(3),
45-
// three(5);
46-
//
47-
// const TestEnum(this.myLength);
48-
//
49-
// final int myLength;
50-
// }
51-
5241
parameterizedTest(
5342
'Example using enum as value',
54-
TestEnum.values,
55-
(TestEnum testEnum) {
56-
expect(testEnum.name.length, testEnum.myLength);
43+
FruitEnum.values,
44+
(FruitEnum testEnum) {
45+
expect(testEnum.name.length, testEnum.wordLength);
5746
},
5847
);
5948

@@ -70,7 +59,7 @@ void main() {
7059
parameterizedTest(
7160
'Example of list of values from function',
7261
provideData(),
73-
(int value1, int value2, int sum) {
62+
(int value1, int value2, int sum) {
7463
expect(value1 + value2, sum);
7564
},
7665
);
@@ -83,7 +72,7 @@ void main() {
8372
['apple', 5],
8473
['banana', 6],
8574
],
86-
(String word, int length) {
75+
(String word, int length) {
8776
expect(word.length, length);
8877
},
8978
setUp: () {
@@ -95,14 +84,18 @@ void main() {
9584
);
9685

9786
// Test which is a async test
87+
// Note: This is a example test to showcase async tests are also possible.
88+
// But this is not a good practice to use a delay like
89+
// this in a test. Running this test will take longer. This could be
90+
// fixed by using a package like fake_async.
9891
parameterizedTest(
9992
'Example using a async test',
10093
[
10194
100,
10295
200,
10396
300,
10497
],
105-
(int value) async {
98+
(int value) async {
10699
final millis = DateTime.now().millisecondsSinceEpoch;
107100
await Future<void>.delayed(Duration(milliseconds: value));
108101
final passed = DateTime.now().millisecondsSinceEpoch - millis;
@@ -114,17 +107,18 @@ void main() {
114107
// Test with CSV data
115108
parameterizedTest('Example of CSV data',
116109
const CsvToListConverter().convert('kiwi,4\r\napple,5\r\nbanana,6'),
117-
(String fruit, int length) {
118-
expect(fruit.length, length);
119-
});
110+
(String fruit, int length) {
111+
expect(fruit.length, length);
112+
});
120113
}
121114

122-
enum TestEnum {
123-
one(3),
124-
two(3),
125-
three(5);
115+
enum FruitEnum {
116+
kiwi(4),
117+
apple(5),
118+
banana(6),
119+
pineapple(9);
126120

127-
const TestEnum(this.myLength);
121+
const FruitEnum(this.wordLength);
128122

129-
final int myLength;
123+
final int wordLength;
130124
}

test/parameterized_test_example_test.dart

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,11 @@ void main() {
3838
});
3939

4040
// Test containing a list of enums
41-
42-
// enum TestEnum {
43-
// one(3),
44-
// two(3),
45-
// three(5);
46-
//
47-
// const TestEnum(this.myLength);
48-
//
49-
// final int myLength;
50-
// }
51-
5241
parameterizedTest(
5342
'Example using enum as value',
54-
TestEnum.values,
55-
(TestEnum testEnum) {
56-
expect(testEnum.name.length, testEnum.myLength);
43+
FruitEnum.values,
44+
(FruitEnum testEnum) {
45+
expect(testEnum.name.length, testEnum.wordLength);
5746
},
5847
);
5948

@@ -95,6 +84,10 @@ void main() {
9584
);
9685

9786
// Test which is a async test
87+
// Note: This is a example test to showcase async tests are also possible.
88+
// But this is not a good practice to use a delay like
89+
// this in a test. Running this test will take longer. This could be
90+
// fixed by using a package like fake_async.
9891
parameterizedTest(
9992
'Example using a async test',
10093
[
@@ -119,12 +112,13 @@ void main() {
119112
});
120113
}
121114

122-
enum TestEnum {
123-
one(3),
124-
two(3),
125-
three(5);
115+
enum FruitEnum {
116+
kiwi(4),
117+
apple(5),
118+
banana(6),
119+
pineapple(9);
126120

127-
const TestEnum(this.myLength);
121+
const FruitEnum(this.wordLength);
128122

129-
final int myLength;
123+
final int wordLength;
130124
}

0 commit comments

Comments
 (0)