Skip to content

Commit ce147e2

Browse files
authored
Merge pull request #17 from DutchCodingCompany/feature/custom_test_naming
Adding custom description builder
2 parents 6364631 + c3a2f99 commit ce147e2

12 files changed

+169
-18
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## 1.1.3
2+
- Added `CustomDescriptionBuilder` to build custom test description for all executed tests.
3+
For example:
4+
```dart
5+
parameterizedTest2(
6+
'Test with custom description',
7+
[
8+
[1, 2],
9+
[3, 4],
10+
],
11+
(int a, int b) => expect(a+b, isPositive),
12+
customDiscriptionBuilder: (groupDescription, index, values) => '🚀[$index] $groupDescription: <<${values.join('|')}>>',
13+
);
14+
```
15+
16+
Prints:
17+
```
18+
Test with custom description 🚀[1] Test with custom description: <<1|2>>
19+
Test with custom description 🚀[2] Test with custom description: <<3|4>>
20+
```
21+
122
## 1.1.2
223
- Added #13: String values are wrapped with quotes in test output. This makes it easier to see if a string value is empty or contains spaces.
324
- Updated lints package

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ group('Amount of letter', () {
166166
});
167167
```
168168

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`.
171+
172+
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+
```
174+
My parameterized test [ 'first', 'second', true ]
175+
My parameterized test [ 'third', 'fourth', false ]
176+
```
177+
178+
When defining a `customDescriptionBuilder` like this:
179+
```dart
180+
...
181+
customDiscriptionBuilder: (groupDescription, index, values) => '🚀[$index] $groupDescription: <<${values.join('|')}>>',
182+
...
183+
```
184+
185+
The output will look like this:
186+
```
187+
My parameterized test 🚀[1] My parameterized test: <<first|second|true>>
188+
My parameterized test 🚀[2] My parameterized test: <<third|fourth|false>>
189+
```
190+
191+
>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+
169193
## Extending parameters
170194

171195
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.

lib/src/parameterized_group_base.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ void parameterizedGroup(
6060
onPlatform: onPlatform,
6161
retry: retry,
6262
),
63+
null,
6364
).executeGroup(body);
6465
}

lib/src/parameterized_test_base.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:meta/meta.dart';
22
import 'package:test/test.dart';
33

44
import 'test_options/group_test_options.dart';
5+
import 'test_options/value_with_test_options.dart';
56
import 'test_parameters.dart';
67
import 'value_source.dart';
78

@@ -34,6 +35,8 @@ void parameterizedTest(
3435
/// The test body which is executed for each test value.
3536
/// See [TestParameters] for more info on different bodies.
3637
TestParameters body, {
38+
/// Provide a custom description builder which will build the description for all the test values test executed.
39+
CustomDescriptionBuilder? customDescriptionBuilder,
3740
dynamic Function()? setUp,
3841

3942
/// Provide a tearDown function to the `group` test.
@@ -58,5 +61,6 @@ void parameterizedTest(
5861
onPlatform: onPlatform,
5962
retry: retry,
6063
),
64+
customDescriptionBuilder,
6165
).executeTests(body);
6266
}

lib/src/parameterized_test_numeric.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:meta/meta.dart';
22
import 'package:test/test.dart';
33

44
import '../parameterized_test.dart';
5+
import 'test_options/value_with_test_options.dart';
56

67
/// Parameterized test with 1 input arguments. See [parameterizedTest] for more info.
78
@isTestGroup
@@ -15,6 +16,8 @@ void parameterizedTest1<A1>(
1516
/// The test body which is executed for each test value.
1617
/// See [TestParameters] for more info on different bodies.
1718
dynamic Function(A1) body, {
19+
/// Provide a custom description builder which will build the description for all the test values test executed.
20+
CustomDescriptionBuilder? customDescriptionBuilder,
1821
dynamic Function()? setUp,
1922

2023
/// Provide a tearDown function to the `group` test.
@@ -30,6 +33,7 @@ void parameterizedTest1<A1>(
3033
description,
3134
values,
3235
p1(body),
36+
customDescriptionBuilder: customDescriptionBuilder,
3337
setUp: setUp,
3438
tearDown: tearDown,
3539
testOn: testOn,
@@ -52,6 +56,8 @@ void parameterizedTest2<A1, A2>(
5256
/// The test body which is executed for each test value.
5357
/// See [TestParameters] for more info on different bodies.
5458
dynamic Function(A1, A2) body, {
59+
/// Provide a custom description builder which will build the description for all the test values test executed.
60+
CustomDescriptionBuilder? customDescriptionBuilder,
5561
dynamic Function()? setUp,
5662

5763
/// Provide a tearDown function to the `group` test.
@@ -67,6 +73,7 @@ void parameterizedTest2<A1, A2>(
6773
description,
6874
values,
6975
p2(body),
76+
customDescriptionBuilder: customDescriptionBuilder,
7077
setUp: setUp,
7178
tearDown: tearDown,
7279
testOn: testOn,
@@ -89,6 +96,8 @@ void parameterizedTest3<A1, A2, A3>(
8996
/// The test body which is executed for each test value.
9097
/// See [TestParameters] for more info on different bodies.
9198
dynamic Function(A1, A2, A3) body, {
99+
/// Provide a custom description builder which will build the description for all the test values test executed.
100+
CustomDescriptionBuilder? customDescriptionBuilder,
92101
dynamic Function()? setUp,
93102

94103
/// Provide a tearDown function to the `group` test.
@@ -104,6 +113,7 @@ void parameterizedTest3<A1, A2, A3>(
104113
description,
105114
values,
106115
p3(body),
116+
customDescriptionBuilder: customDescriptionBuilder,
107117
setUp: setUp,
108118
tearDown: tearDown,
109119
testOn: testOn,
@@ -126,6 +136,8 @@ void parameterizedTest4<A1, A2, A3, A4>(
126136
/// The test body which is executed for each test value.
127137
/// See [TestParameters] for more info on different bodies.
128138
dynamic Function(A1, A2, A3, A4) body, {
139+
/// Provide a custom description builder which will build the description for all the test values test executed.
140+
CustomDescriptionBuilder? customDescriptionBuilder,
129141
dynamic Function()? setUp,
130142

131143
/// Provide a tearDown function to the `group` test.
@@ -141,6 +153,7 @@ void parameterizedTest4<A1, A2, A3, A4>(
141153
description,
142154
values,
143155
p4(body),
156+
customDescriptionBuilder: customDescriptionBuilder,
144157
setUp: setUp,
145158
tearDown: tearDown,
146159
testOn: testOn,
@@ -163,6 +176,8 @@ void parameterizedTest5<A1, A2, A3, A4, A5>(
163176
/// The test body which is executed for each test value.
164177
/// See [TestParameters] for more info on different bodies.
165178
dynamic Function(A1, A2, A3, A4, A5) body, {
179+
/// Provide a custom description builder which will build the description for all the test values test executed.
180+
CustomDescriptionBuilder? customDescriptionBuilder,
166181
dynamic Function()? setUp,
167182

168183
/// Provide a tearDown function to the `group` test.
@@ -178,6 +193,7 @@ void parameterizedTest5<A1, A2, A3, A4, A5>(
178193
description,
179194
values,
180195
p5(body),
196+
customDescriptionBuilder: customDescriptionBuilder,
181197
setUp: setUp,
182198
tearDown: tearDown,
183199
testOn: testOn,
@@ -200,6 +216,8 @@ void parameterizedTest6<A1, A2, A3, A4, A5, A6>(
200216
/// The test body which is executed for each test value.
201217
/// See [TestParameters] for more info on different bodies.
202218
dynamic Function(A1, A2, A3, A4, A5, A6) body, {
219+
/// Provide a custom description builder which will build the description for all the test values test executed.
220+
CustomDescriptionBuilder? customDescriptionBuilder,
203221
dynamic Function()? setUp,
204222

205223
/// Provide a tearDown function to the `group` test.
@@ -215,6 +233,7 @@ void parameterizedTest6<A1, A2, A3, A4, A5, A6>(
215233
description,
216234
values,
217235
p6(body),
236+
customDescriptionBuilder: customDescriptionBuilder,
218237
setUp: setUp,
219238
tearDown: tearDown,
220239
testOn: testOn,
@@ -237,6 +256,8 @@ void parameterizedTest7<A1, A2, A3, A4, A5, A6, A7>(
237256
/// The test body which is executed for each test value.
238257
/// See [TestParameters] for more info on different bodies.
239258
dynamic Function(A1, A2, A3, A4, A5, A6, A7) body, {
259+
/// Provide a custom description builder which will build the description for all the test values test executed.
260+
CustomDescriptionBuilder? customDescriptionBuilder,
240261
dynamic Function()? setUp,
241262

242263
/// Provide a tearDown function to the `group` test.
@@ -252,6 +273,7 @@ void parameterizedTest7<A1, A2, A3, A4, A5, A6, A7>(
252273
description,
253274
values,
254275
p7(body),
276+
customDescriptionBuilder: customDescriptionBuilder,
255277
setUp: setUp,
256278
tearDown: tearDown,
257279
testOn: testOn,
@@ -274,6 +296,8 @@ void parameterizedTest8<A1, A2, A3, A4, A5, A6, A7, A8>(
274296
/// The test body which is executed for each test value.
275297
/// See [TestParameters] for more info on different bodies.
276298
dynamic Function(A1, A2, A3, A4, A5, A6, A7, A8) body, {
299+
/// Provide a custom description builder which will build the description for all the test values test executed.
300+
CustomDescriptionBuilder? customDescriptionBuilder,
277301
dynamic Function()? setUp,
278302

279303
/// Provide a tearDown function to the `group` test.
@@ -289,6 +313,7 @@ void parameterizedTest8<A1, A2, A3, A4, A5, A6, A7, A8>(
289313
description,
290314
values,
291315
p8(body),
316+
customDescriptionBuilder: customDescriptionBuilder,
292317
setUp: setUp,
293318
tearDown: tearDown,
294319
testOn: testOn,
@@ -311,6 +336,8 @@ void parameterizedTest9<A1, A2, A3, A4, A5, A6, A7, A8, A9>(
311336
/// The test body which is executed for each test value.
312337
/// See [TestParameters] for more info on different bodies.
313338
dynamic Function(A1, A2, A3, A4, A5, A6, A7, A8, A9) body, {
339+
/// Provide a custom description builder which will build the description for all the test values test executed.
340+
CustomDescriptionBuilder? customDescriptionBuilder,
314341
dynamic Function()? setUp,
315342

316343
/// Provide a tearDown function to the `group` test.
@@ -326,6 +353,7 @@ void parameterizedTest9<A1, A2, A3, A4, A5, A6, A7, A8, A9>(
326353
description,
327354
values,
328355
p9(body),
356+
customDescriptionBuilder: customDescriptionBuilder,
329357
setUp: setUp,
330358
tearDown: tearDown,
331359
testOn: testOn,
@@ -348,6 +376,8 @@ void parameterizedTest10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(
348376
/// The test body which is executed for each test value.
349377
/// See [TestParameters] for more info on different bodies.
350378
dynamic Function(A1, A2, A3, A4, A5, A6, A7, A8, A9, A10) body, {
379+
/// Provide a custom description builder which will build the description for all the test values test executed.
380+
CustomDescriptionBuilder? customDescriptionBuilder,
351381
dynamic Function()? setUp,
352382

353383
/// Provide a tearDown function to the `group` test.
@@ -363,6 +393,7 @@ void parameterizedTest10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10>(
363393
description,
364394
values,
365395
p10(body),
396+
customDescriptionBuilder: customDescriptionBuilder,
366397
setUp: setUp,
367398
tearDown: tearDown,
368399
testOn: testOn,

lib/src/test_options/value_with_test_options.dart

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import 'group_test_options.dart';
22
import 'test_options.dart';
33

4+
typedef CustomDescriptionBuilder = String Function(
5+
Object groupDescription, int index, Iterable<dynamic> values);
6+
47
class ValueWithTestOptions extends Iterable<dynamic> {
5-
ValueWithTestOptions(this.value, this.testOptions);
8+
ValueWithTestOptions(
9+
this.value,
10+
this.testOptions, {
11+
this.groupDescription = '',
12+
this.index = 0,
13+
this.customDiscriptionBuilder,
14+
});
615

716
final Iterable<dynamic> value;
817
final TestOptions testOptions;
18+
final int index;
19+
final Object groupDescription;
20+
final CustomDescriptionBuilder? customDiscriptionBuilder;
921

1022
String get description =>
23+
customDiscriptionBuilder?.call(groupDescription, index, value) ??
1124
'[ ${value.map((e) => e is String ? '\'$e\'' : e.toString()).join(', ')} ]';
1225

1326
@override
@@ -21,4 +34,21 @@ class ValueWithTestOptions extends Iterable<dynamic> {
2134
tags: testOptions.tags,
2235
timeout: testOptions.timeout,
2336
);
37+
38+
ValueWithTestOptions copyWith({
39+
Iterable<dynamic>? value,
40+
TestOptions? testOptions,
41+
int? index,
42+
Object? groupDescription,
43+
CustomDescriptionBuilder? customDiscriptionBuilder,
44+
}) {
45+
return ValueWithTestOptions(
46+
value ?? this.value,
47+
testOptions ?? this.testOptions,
48+
groupDescription: groupDescription ?? this.groupDescription,
49+
index: index ?? this.index,
50+
customDiscriptionBuilder:
51+
customDiscriptionBuilder ?? this.customDiscriptionBuilder,
52+
);
53+
}
2454
}

lib/src/util/test_value_helpers.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,34 @@ void validityCheck(Iterable<dynamic> values, int length) {
3737
Iterable<ValueWithTestOptions> wrap(
3838
Iterable<dynamic> values,
3939
TestOptions defaultTestOptions,
40+
Object groupDescription,
41+
CustomDescriptionBuilder? customDiscriptionBuilder,
4042
) {
43+
int index = 0;
4144
return values.map((e) {
45+
index++;
4246
if (e is ValueWithTestOptions) {
43-
return e;
47+
return e.copyWith(
48+
index: index,
49+
groupDescription: groupDescription,
50+
customDiscriptionBuilder: customDiscriptionBuilder,
51+
);
4452
} else if (e is Iterable<dynamic>) {
45-
return ValueWithTestOptions(e, defaultTestOptions);
53+
return ValueWithTestOptions(
54+
e,
55+
defaultTestOptions,
56+
index: index,
57+
groupDescription: groupDescription,
58+
customDiscriptionBuilder: customDiscriptionBuilder,
59+
);
4660
} else {
47-
return ValueWithTestOptions([e], defaultTestOptions);
61+
return ValueWithTestOptions(
62+
[e],
63+
defaultTestOptions,
64+
index: index,
65+
groupDescription: groupDescription,
66+
customDiscriptionBuilder: customDiscriptionBuilder,
67+
);
4868
}
4969
});
5070
}

lib/src/value_source.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@ import 'util/test_value_helpers.dart';
88
abstract class ValueSource implements TestSource {
99
factory ValueSource(
1010
Iterable<dynamic> values,
11-
GroupTestOptions groupTestOptions, [
11+
GroupTestOptions groupTestOptions,
12+
CustomDescriptionBuilder? customDiscriptionBuilder, [
1213
TestOptions defaultTestOptions = const TestOptions(),
1314
]) =>
14-
_ValueSourceImpl(wrap(values, defaultTestOptions), groupTestOptions);
15+
_ValueSourceImpl(
16+
wrap(
17+
values,
18+
defaultTestOptions,
19+
groupTestOptions.description,
20+
customDiscriptionBuilder,
21+
),
22+
groupTestOptions);
1523
}
1624

1725
class _ValueSourceImpl implements ValueSource {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: parameterized_test
22
description: Simple package that helps with executing parameterized tests. Inspired by JUnit ParameterizedTest.
3-
version: 1.1.2
3+
version: 1.1.3
44
homepage: https://www.github.com/DutchCodingCompany/parameterized_test
55

66
environment:

0 commit comments

Comments
 (0)