You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- ✅ Run a test multiple times based on provide parameter list.
29
-
- ✅ Uses [dart test package](https://pub.dev/packages/test) under the hood.
29
+
- ✅ Built on top of [dart test package](https://pub.dev/packages/test).
30
30
- ✅ Type cast test parameters used in the tests.
31
31
- ✅ Include test options for parameter_test.
32
-
- ✅ Include test options per parameters.
32
+
- ✅ Include test options per parameter.
33
33
34
-
- ❌ No CSV parsing is supported. Its only possible to use Lists with actual values.
35
-
36
-
## Installation
34
+
## 🛠 Installation
37
35
38
36
```yaml
39
37
dev_dependencies:
40
38
parameterized_test: [latest-version]
41
39
```
42
40
43
-
## Usage
44
-
45
-
Instead of using `groups` or `test` you can now use `parameterizedTest` and supply it list of test parameters to use in the same test.
46
-
To specify the test body use `TestParametersX` that matches the same amount of test parameters for 1 test. For example when the test has 2 parameters `actual` and `expected` use `TestParameters2` for supplying the test body.
47
-
The package offers `TestParameters` classes up to 10 parameters. Instead of writing `TestParameters` completely it also possible to use `typedef`'s `p1`..`p10`.
48
-
49
-
## Examples
50
-
51
-
Example parameterizedTest with 1 parameter:
52
-
53
-
```dart
54
-
parameterizedTest(
55
-
'Number are less than 4 tests',
56
-
[
57
-
1,
58
-
2,
59
-
3,
60
-
],
61
-
p1((int number) {
62
-
final result = number < 4;
63
-
expect(result, true);
64
-
}),
65
-
);
66
-
```
41
+
## ⚡️ Usage
67
42
68
-
Example parameterizedTest with 2 parameters:
43
+
Instead of creating a `group` test with multiple of the same `test` and different parameters, you can now use `parameterizedTest` and supply it list of test parameters to run the same test with multiple times.
44
+
Specifying a parameterized test is almost the same as normal test. It has all the same parameters as a normal test like: `skip`or `testOn` etc.
45
+
The only difference is that you need to provide a list of test values and a function that takes the same amount of arguments as the test values.
69
46
47
+
For example:
70
48
```dart
71
49
parameterizedTest(
72
-
'Amount of letters tests',
73
-
[
74
-
['kiwi', 4],
75
-
['apple', 5],
76
-
['banana', 6],
77
-
],
78
-
p2((String word, int length) {
79
-
expect(word.length, length);
80
-
}),
81
-
);
82
-
```
83
-
84
-
or
85
-
86
-
```dart
87
-
parameterizedTest2(
88
-
'Amount of letters tests',
50
+
'Fruit name matches length',
51
+
// List of values to test
89
52
[
53
+
// parameters for the test
90
54
['kiwi', 4],
91
55
['apple', 5],
92
56
['banana', 6],
57
+
['pineapple', 9],
93
58
],
94
-
(String word, int length) {
95
-
expect(word.length, length);
59
+
// Test function accepting the provided parameters
60
+
(String fruit, int length) {
61
+
expect(fruit.length, length);
96
62
},
97
63
);
98
64
```
99
-
100
-
Example parameterizedTest with extra test options for a value:
101
-
102
-
```dart
103
-
parameterizedTest(
104
-
'Amount of letters',
105
-
[
106
-
['kiwi', 4],
107
-
['apple', 5],
108
-
['banana', 6].withTestOptions(skip: true),
109
-
],
110
-
p2((String word, int length) {
111
-
expect(word.length, length);
112
-
}),
113
-
);
65
+
This `parameterizedTest` will create a `group` with 4 `test` inside it. Each test will run the test function with the provided parameters.
114
66
```
115
-
116
-
Example parameterizedTest with test enum values:
117
-
118
-
```dart
119
-
enum AwesomeEnum { such, woow, much, skill}
120
-
121
-
parameterizedTest(
122
-
'Doge enum tests',
123
-
AwesomeEnum.values,
124
-
p1((AwesomeEnum doge) {
125
-
final result = doge.name.length >= 4;
126
-
expect(result, true);
127
-
}),
128
-
);
67
+
- Fruit name matches length (group)
68
+
-[ 'kiwi', 4 ] (test)
69
+
-[ 'apple', 5 ] (test)
70
+
-[ 'banana', 6 ] (test)
71
+
-[ 'pineapple', 9 ] (test)
129
72
```
130
73
131
-
## How it works
74
+
There is also a `parameterizedGroup` which is basically the same as `parameterizedTest` but instead of creating a `group` that runs a `test` multiple times, it creates a `group` that runs a `group` multiple times.
75
+
Here you still need to provide a `test`. This can be useful if you want to have nested groups of tests.
76
+
77
+
Besides accepting a list of test values, you can also provide a `setUp` and `tearDown` function to run before and after each test.
132
78
133
-
`parameterized_test`is basically a wrapper that executes a `group` test and loops over the provide `List` of test values. Each set of values is cast to the specified type inside the body. Which is wrapped inside a `test`.
79
+
## 🔩 Add test options to parameter
80
+
If you want to add test options to a specific parameter you can do so by using the `options` extension on `List`. This will allow you to add test options like `skip` or `testOn` to a specific parameter.
134
81
82
+
For example:
135
83
```dart
136
84
parameterizedTest(
137
-
'Amount of letters',
85
+
'Fruit name matches length',
86
+
// List of values to test
138
87
[
88
+
// parameters for the test
139
89
['kiwi', 4],
140
-
['apple', 5],
90
+
['apple', 5].options(skip: 'Apple is not ripe yet'),
141
91
['banana', 6],
92
+
['pineapple', 9],
142
93
],
143
-
p2((String word, int length) {
144
-
expect(word.length, length);
145
-
}),
94
+
// Test function accepting the provided parameters
95
+
(String fruit, int length) {
96
+
expect(fruit.length, length);
97
+
},
146
98
);
147
99
```
148
100
149
-
The above example roughly translates to:
150
-
```dart
151
-
group('Amount of letter', () {
152
-
final testValues = [
153
-
['kiwi', 4],
154
-
['apple', 5],
155
-
['banana', 6],
156
-
];
101
+
This will create a `group` with 4 `test` inside it. The second test will receive the provided test options and will be skipped in this case.
157
102
158
-
for (final testValue in testValues) {
159
-
test(testValue.toString(), () {
160
-
final String word = testValue[0] as String;
161
-
final int length = testValue[1] as int;
162
-
163
-
expect(word.length, length);
164
-
});
165
-
}
166
-
});
167
-
```
168
-
169
-
## Changing test description output
170
-
By default the test description contains the test value used within the tests. This can be override by using `customDescriptionBuilder`.
103
+
## 📝 Changing test description output
104
+
By default, the test description contains the test value used within the tests. you can override this by using `customDescriptionBuilder`.
171
105
172
106
When normally running parameterized tests with description 'My parameterized test' and the values `[['first', 'second', true], ['third', 'fourth', false]]` the test description output looks something like this:
173
107
```
@@ -190,31 +124,135 @@ My parameterized test 🚀[2] My parameterized test: <<third|fourth|false>>
190
124
191
125
>Note: the first 'My parameterized test' is because parameterized tests make use of a group test. Most IDE's will group this for you and only show the second part.
192
126
193
-
## Extending parameters
127
+
## 📦 Examples
128
+
### Simple test containing a list of single values
129
+
```dart
130
+
parameterizedTest(
131
+
'Example of list of single values',
132
+
[
133
+
1,
134
+
2,
135
+
3,
136
+
],
137
+
(int value) {
138
+
final result = value < 4;
139
+
expect(result, true);
140
+
},
141
+
);
142
+
```
194
143
195
-
Currently the package supports `TestParameters` classes up to 10 arguments. If need to more arguments within a test than this is possible by implementing the `TestParameters` class.
144
+
### Simple test containing a list of multiple values
145
+
```dart
146
+
parameterizedTest('Example of list of multiple values', [
147
+
[0, 1, 1],
148
+
[1, 1, 2],
149
+
[1, 2, 3],
150
+
[2, 2, 4],
151
+
], (int value1, int value2, int sum) {
152
+
expect(value1 + value2, sum);
153
+
});
154
+
```
155
+
### Test containing a list with complex objects
156
+
```dart
157
+
parameterizedTest('Example of a list with complex object', [
158
+
[DateTime(2024, 4, 12), 5],
159
+
[DateTime(1969, 07, 20), 7],
160
+
], (DateTime dateTime, int expectedWeekday) {
161
+
expect(dateTime.weekday, expectedWeekday);
162
+
});
163
+
```
196
164
197
-
For example:
165
+
### Test containing a list of enums
198
166
```dart
199
-
class MyParameters<A1, A2> implements TestParameters {
200
-
const MyParameters(this.body);
167
+
enum TestEnum {
168
+
one(3),
169
+
two(3),
170
+
three(5);
201
171
202
-
@override
203
-
final dynamic Function(A1, A2) body;
172
+
const TestEnum(this.myLength);
204
173
205
-
@override
206
-
final int count = 2;
174
+
final int myLength;
175
+
}
207
176
208
-
@override
209
-
dynamic mapBody<R>(Iterable<R> values) {
210
-
final A1 a1 = values.elementAt(0) as A1;
211
-
final A2 a2 = values.elementAt(1) as A2;
212
-
return body(a1, a2);
213
-
}
177
+
parameterizedTest(
178
+
'Example using enum as value',
179
+
TestEnum.values,
180
+
(TestEnum testEnum) {
181
+
expect(testEnum.name.length, testEnum.myLength);
182
+
},
183
+
);
184
+
```
185
+
### Test retreiving the list of values from a function
186
+
```dart
187
+
List<dynamic> provideData() {
188
+
return [
189
+
[0, 1, 1],
190
+
[1, 1, 2],
191
+
[1, 2, 3],
192
+
[2, 2, 4],
193
+
];
214
194
}
195
+
196
+
parameterizedTest(
197
+
'Example of list of values from function',
198
+
provideData(),
199
+
(int value1, int value2, int sum) {
200
+
expect(value1 + value2, sum);
201
+
},
202
+
);
203
+
```
204
+
205
+
### Simple test with setup and teardown
206
+
```dart
207
+
parameterizedTest(
208
+
'Example with setup and teardown ',
209
+
[
210
+
['kiwi', 4],
211
+
['apple', 5],
212
+
['banana', 6],
213
+
],
214
+
(String word, int length) {
215
+
expect(word.length, length);
216
+
},
217
+
setUp: () {
218
+
print('Setup everything I need for testing');
219
+
},
220
+
tearDown: () {
221
+
print('tear it down again');
222
+
},
223
+
);
224
+
```
225
+
226
+
### Test which is an async test
227
+
```dart
228
+
parameterizedTest(
229
+
'Example using a async test',
230
+
[
231
+
100,
232
+
200,
233
+
300,
234
+
],
235
+
(int value) async {
236
+
final millis = DateTime.now().millisecondsSinceEpoch;
0 commit comments