Skip to content

Commit 62e9ff1

Browse files
Add ability to use custom labels in SpecFlow plugin (#284)
* add parsing custom label as key-value * Add new unit test to check custom label * fix label regex Co-authored-by: Aleksandr Kulakov <a.kulakov@dodopizza.com>
1 parent 2422016 commit 62e9ff1

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

Allure.Features/TestData/Labels.feature

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ Feature: Labels
1717
Scenario: [v1.2 accounting] [core.update] Update test
1818

1919
@passed @epic:v.2.0 @story:security @package:com.company.security @class:main @method:getACL
20-
Scenario: [v1.2 / v.2.0 security] [core.update] [com.company.security.main.getACL] Get ACL test
20+
Scenario: [v1.2 / v.2.0 security] [core.update] [com.company.security.main.getACL] Get ACL test
21+
22+
@passed @label:layer:e2e @label:as_id:9894 @label:custom_label:pepa
23+
Scenario: Layer e2e (label)

Allure.Features/allureConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"labels": {
3333
"owner": "^owner:?(.+)",
34-
"severity": "^(normal|blocker|critical|minor|trivial)"
34+
"severity": "^(normal|blocker|critical|minor|trivial)",
35+
"label": "^label:([\\w]+):(.+)"
3536
},
3637
"links": {
3738
"link": "^link:(.+)",

Allure.SpecFlowPlugin.Tests/IntegrationTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ public void ShouldParseTags()
169169
});
170170
}
171171

172+
[Test]
173+
public void ShouldParseCustomLabel()
174+
{
175+
var scenarios = allureTestResults
176+
.Where(x => x.labels.Any(l => l.value == "labels"));
177+
178+
var labels = scenarios.SelectMany(x => x.labels);
179+
180+
Assert.That(labels.Where(x => x.value == "pepa").Select(l => l.name),
181+
Has.Exactly(1).Items.And.All.EqualTo("custom_label"));
182+
}
183+
172184
[Test]
173185
public void ShouldAddParametersForScenarioExamples()
174186
{

Allure.SpecFlowPlugin/PluginConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class Labels
4646
{
4747
public string owner { get; set; }
4848
public string severity { get; set; }
49+
public string label { get; set; }
4950
}
5051

5152
public class Links

Allure.SpecFlowPlugin/PluginHelper.cs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ private static Tuple<List<Label>, List<Link>> GetTags(FeatureInfo featureInfo, S
236236
continue;
237237
}
238238

239+
// label
240+
if (GetLabelProps(PluginConfiguration.labels.label, tagValue, out var props))
241+
{
242+
result.Item1.Add(new Label { name = props.Key, value = props.Value});
243+
continue;
244+
}
245+
239246
// tag
240247
result.Item1.Add(Label.Tag(tagValue));
241248
}
@@ -261,37 +268,66 @@ private static (List<Parameter> parameters, string hash) GetParameters(ScenarioI
261268

262269
private static bool TryUpdateValueByMatch(string expression, ref string value)
263270
{
264-
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(expression))
271+
var matchedGroups = GetMatchedGroups(expression, value);
272+
273+
if (!matchedGroups.Any()) return false;
274+
275+
if (matchedGroups.Count == 1)
276+
value = matchedGroups[0];
277+
else
278+
value = matchedGroups[1];
279+
return true;
280+
}
281+
282+
private static bool GetLabelProps(string expression, string value, out KeyValuePair<string, string> props)
283+
{
284+
props = default;
285+
286+
var matchedGroups = GetMatchedGroups(expression, value);
287+
288+
if (matchedGroups.Count != 3)
265289
return false;
290+
291+
props = new KeyValuePair<string, string>(matchedGroups[1], matchedGroups[2]);
292+
return true;
293+
294+
}
295+
296+
private static List<string> GetMatchedGroups(string expression, string value)
297+
{
298+
var matchedGroups = new List<string>();
299+
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(expression))
300+
return matchedGroups;
266301

267302
Regex regex = null;
268303
try
269304
{
270305
regex = new Regex(expression,
271-
RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
306+
RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
272307
}
273308
catch (Exception)
274309
{
275-
return false;
310+
return matchedGroups;
276311
}
277312

278313
if (regex == null)
279-
return false;
314+
return matchedGroups;
280315

281316
if (regex.IsMatch(value))
282317
{
283318
var groups = regex.Match(value).Groups;
284-
if (groups?.Count == 1)
285-
value = groups[0].Value;
286-
else
287-
value = groups[1].Value;
288319

289-
return true;
320+
for (var i = 0; i < groups.Count; i++)
321+
{
322+
matchedGroups.Add(groups[i].Value);
323+
}
324+
325+
return matchedGroups;
290326
}
291327

292-
return false;
328+
return matchedGroups;
293329
}
294-
330+
295331
private static int GetDeterministicHashCode(this string str)
296332
{
297333
unchecked

Allure.SpecFlowPlugin/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ Scenario: ....
111111
```
112112
will set current scenario severity in Allure report as Blocker
113113

114+
#### Label
115+
You can add Label for your scenarios using tags. It can be configured in `allureConfig.json`
116+
``` json
117+
"labels": {
118+
"label": "^label:([\\w]+):(.+)"
119+
},
120+
```
121+
The following scenario
122+
``` cucumber
123+
@label:layer:e2e: @label:as_id:123
124+
Scenario: ....
125+
```
126+
will set current scenario Layer as e2e and Id as 123 in Allure report
127+
114128
#### Tables conversion
115129
Table arguments in SpecFlow steps can be converted either to step csv-attacments or step parameters in the Allure report. The conversion is configurable in `specflow:stepArguments` config section.
116130
With `specflow:stepArguments:convertToParameters` set to `true` the following table arguments will be represented as parameters:

0 commit comments

Comments
 (0)