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
[developing packages and plugins](https://flutter.dev/developing-packages).
12
12
-->
13
13
14
-
# Parameterized test
14
+
# 🧪 Parameterized test
15
15
16
-
Simple package that helps with executing parameterized tests. Inspired by [JUnit ParameterizedTest CsvValues](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvSource). This package is a wrapper around `groups` and `test`.
16
+
Supercharge your Dart testing with **parameterized_test**! Built on top of the [dart test package](https://pub.dev/packages/test), this [JUnit ParameterizedTest](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests) inspired package wrap around `group` and `test` to take your testing to the next level!
This package helps executing a test multiple times with different parameters.
21
-
Currently parameters can be specified as:
22
-
- ParameterizedSource.csv
23
-
- ParameterizedSource.value
24
-
- ParameterizedSource.values
26
+
## Features ✨
25
27
26
-
When using `ParameterizedSource.csv` the library tries to parse the values to `int`, `double`, `bool` or `String?`. If the csv value contains a empty string it will be parsed to null.
27
-
This parsing can be escaped by wrapping in the values in `'` or `"`.
28
+
- ✅ Run a test multiple times based on provide parameter list.
29
+
- ✅ Uses [dart test package](https://pub.dev/packages/test) under the hood.
30
+
- ✅ Type cast test parameters used in the tests.
31
+
- ✅ Include test options for parameter_test.
32
+
- ✅ Include test options per parameters.
28
33
29
-
For example:
34
+
- ❌ No CSV parsing is supported. Its only possible to use Lists with actual values.
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
+
```
67
+
68
+
Example parameterizedTest with 2 parameters:
69
+
70
+
```dart
71
+
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
+
Example parameterizedTest with extra test options for a value:
39
85
40
-
Include `parameterized_test` or when using `flutter_test` include `flutter_parameterized_test` in
41
-
your projects `pubspec.yaml`.
86
+
```dart
87
+
parameterizedTest(
88
+
'Amount of letters',
89
+
[
90
+
['kiwi', 4],
91
+
['apple', 5],
92
+
['banana', 6].withTestOptions(skip: true),
93
+
],
94
+
p2((String word, int length) {
95
+
expect(word.length, length);
96
+
}),
97
+
);
98
+
```
42
99
43
-
## Usage
100
+
Example parameterizedTest with test enum values:
44
101
45
-
Instead of using `groups` or `test` you can now use `parameterizedTest` and supply it with multiple test parameters.
102
+
```dart
103
+
enum AwesomeEnum { such, woow, much, skill}
104
+
105
+
parameterizedTest(
106
+
'Doge enum tests',
107
+
AwesomeEnum.values,
108
+
p1((AwesomeEnum doge) {
109
+
final result = doge.name.length == 4;
110
+
expect(result, true);
111
+
}),
112
+
);
113
+
```
114
+
115
+
## How it works
116
+
117
+
`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`.
46
118
47
-
Csv source example:
48
119
```dart
49
120
parameterizedTest(
50
121
'Amount of letters',
51
-
ParameterizedSource.csv([
52
-
'kiwi, 4',
53
-
'apple, 5',
54
-
'banana, 6',
55
-
]),
56
-
(List<dynamic> values) {
57
-
final String input = values[0];
58
-
final expected = values[1];
59
-
60
-
final actual = input.length;
61
-
62
-
expect(actual, expected);
63
-
},
122
+
[
123
+
['kiwi', 4],
124
+
['apple', 5],
125
+
['banana', 6],
126
+
],
127
+
p2((String word, int length) {
128
+
expect(word.length, length);
129
+
}),
64
130
);
65
131
```
66
132
67
-
Values source example:
133
+
The above example roughly translates to:
134
+
```dart
135
+
group('Amount of letter', () {
136
+
final testValues = [
137
+
['kiwi', 4],
138
+
['apple', 5],
139
+
['banana', 6],
140
+
],
141
+
142
+
for(final testValue in testValues){
143
+
test(testValue.toString(), () {
144
+
final String word = testValue[0] as String;
145
+
final int length = testValue[1] as int;
146
+
147
+
expect(word.length, length);
148
+
});
149
+
}
150
+
});
151
+
```
152
+
153
+
## Extending parameters
154
+
155
+
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.
156
+
157
+
For example:
68
158
```dart
69
-
parameterizedTest(
70
-
'Amount of letters',
71
-
ParameterizedSource.values([
72
-
['kiwi', 4],
73
-
['apple', 5],
74
-
['banana', 6],
75
-
]),
76
-
(List<dynamic> values) {
77
-
final String input = values[0];
78
-
final expected = values[1];
79
-
80
-
final actual = input.length;
81
-
82
-
expect(actual, expected);
83
-
},
84
-
);
159
+
class MyParameters implements TestParameters{
160
+
const MyParameters(this.body);
161
+
162
+
@override
163
+
final dynamic Function(A1, A2) body;
164
+
165
+
@override
166
+
final int count = 2;
167
+
168
+
@override
169
+
void mapBody<R>(Iterable<R> values) {
170
+
final A1 a1 = values.elementAt(0) as A1;
171
+
final A2 a2 = values.elementAt(1) as A2;
172
+
body(a1, a2);
173
+
}
174
+
}
175
+
176
+
}
85
177
```
86
178
87
179
## Additional information
88
180
89
-
Its just a simple wrapper to easily execute tests multiple times with different values. Feel free to leave some feedback or open an pull request :)
181
+
Its just a simple wrapper to easily execute tests multiple times with different values. Feel free to
[developing packages and plugins](https://flutter.dev/developing-packages).
12
-
-->
13
-
14
-
# Parameterized test
15
-
16
-
Simple package that helps with executing parameterized tests. Inspired by [JUnit ParameterizedTest CsvValues](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests-sources-CsvSource). This package is a wrapper around `groups` and `test`.
17
-
18
-
## Features
19
-
20
-
This package helps executing a test multiple times with different parameters.
21
-
Currently parameters can be specified as:
22
-
- ParameterizedSource.csv
23
-
- ParameterizedSource.value
24
-
- ParameterizedSource.values
25
-
26
-
When using `ParameterizedSource.csv` the library tries to parse the values to `int`, `double`, `bool` or `String?`. If the csv value contains a empty string it will be parsed to null.
27
-
This parsing can be escaped by wrapping in the values in `'` or `"`.
0 commit comments