-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathparameterized_test_example.dart
More file actions
119 lines (105 loc) · 2.85 KB
/
parameterized_test_example.dart
File metadata and controls
119 lines (105 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// ignore this lint for example reasons.
// ignore_for_file: avoid_print
import 'package:csv/csv.dart';
import 'package:parameterized_test/parameterized_test.dart';
import 'package:test/test.dart';
void main() {
// Simple test containing a list of single values
parameterizedTest('Example of list of single values', [1, 2, 3], (int value) {
final result = value < 4;
expect(result, true);
});
// Simple test containing a list of multiple values
parameterizedTest(
'Example of list of multiple values',
[
[0, 1, 1],
[1, 1, 2],
[1, 2, 3],
[2, 2, 4],
],
(int value1, int value2, int sum) {
expect(value1 + value2, sum);
},
);
// Test containing a list with complex objects
parameterizedTest(
'Example of a list with complex object',
[
[DateTime(2024, 4, 12), 5],
[DateTime(1969, 07, 20), 7],
],
(DateTime dateTime, int expectedWeekday) {
expect(dateTime.weekday, expectedWeekday);
},
);
// Test containing a list of enums
parameterizedTest('Example using enum as value', FruitEnum.values, (
FruitEnum testEnum,
) {
expect(testEnum.name.length, testEnum.wordLength);
});
// Test retreiving the list of values from a function
List<dynamic> provideData() {
return [
[0, 1, 1],
[1, 1, 2],
[1, 2, 3],
[2, 2, 4],
];
}
parameterizedTest('Example of list of values from function', provideData(), (
int value1,
int value2,
int sum,
) {
expect(value1 + value2, sum);
});
// Simple test with setup and teardown
parameterizedTest(
'Example with setup and teardown ',
[
['kiwi', 4],
['apple', 5],
['banana', 6],
],
(String word, int length) {
expect(word.length, length);
},
setUp: () {
print('Setup everything I need for testing');
},
tearDown: () {
print('tear it down again');
},
);
// Test which is a async test
// Note: This is a example test to showcase async tests are also possible.
// But this is not a good practice to use a delay like
// this in a test. Running this test will take longer. This could be
// fixed by using a package like fake_async.
parameterizedTest('Example using a async test', [100, 200, 300], (
int value,
) async {
final millis = DateTime.now().millisecondsSinceEpoch;
await Future<void>.delayed(Duration(milliseconds: value));
final passed = DateTime.now().millisecondsSinceEpoch - millis;
expect(passed >= value, true);
});
// Test with CSV data
parameterizedTest(
'Example of CSV data',
const CsvToListConverter().convert('kiwi,4\r\napple,5\r\nbanana,6'),
(String fruit, int length) {
expect(fruit.length, length);
},
);
}
enum FruitEnum {
kiwi(4),
apple(5),
banana(6),
pineapple(9);
const FruitEnum(this.wordLength);
final int wordLength;
}