/
feature-context.md
134 lines (95 loc) · 3.78 KB
/
feature-context.md
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Feature Context
Reqnroll provides access to the current test context using both `FeatureContext` and the more commonly used [ScenarioContext](scenario-context). `FeatureContext` persists for the duration of the execution of an entire feature, whereas `ScenarioContext` only persists for the duration of a scenario.
## Accessing the FeatureContext
### in Bindings
To access the `FeatureContext` you have to get it via [Context-Injection](context-injection).
Example:
```{code-block} csharp
:caption: Step Definition File
[Binding]
public class Binding
{
private FeatureContext _featureContext;
public Binding(FeatureContext featureContext)
{
_featureContext = featureContext;
}
}
```
Now you can access the FeatureContext in all your Bindings with the `_featureContext` field.
### in Hooks
#### Before/AfterTestRun
Accessing the `FeatureContext` is not possible, as no `Feature` is executed, when the hook is called.
#### Before/AfterFeature
You can get the `FeatureContext` via parameter of the static method.
Example:
```{code-block} csharp
:caption: Hook File
[Binding]
public class Hooks
{
[BeforeFeature]
public static void BeforeFeature(FeatureContext featureContext)
{
Console.WriteLine("Starting " + featureContext.FeatureInfo.Title);
}
[AfterFeature]
public static void AfterFeature(FeatureContext featureContext)
{
Console.WriteLine("Finished " + featureContext.FeatureInfo.Title);
}
}
```
#### Before/AfterScenario
Accessing the `FeatureContext` is done like in [normal bindings](#in-bindings)
#### Before/AfterStep
Accessing the `FeatureContext` is done like in [normal bindings](#in-bindings)
## Storing data in the FeatureContext
`FeatureContext` implements Dictionary<string, object>. So you can use the `FeatureContext` like a property bag.
## FeatureContext.FeatureInfo
`FeatureInfo` provides more information than `ScenarioInfo`, but it works the same:
In the feature file:
```{code-block} gherkin
:caption: Feature File
Scenario: Showing information of the feature
When I execute any scenario in the feature
Then the FeatureInfo contains the following information
| Field | Value |
| Tags | showUpInScenarioInfo, andThisToo |
| Title | FeatureContext features |
| TargetLanguage | CSharp |
| Language | en-US |
| Description | In order to |
```
...and in the step definition:
```{code-block} csharp
:caption: Step Definition File
private class FeatureInformation
{
public string Title { get; set; }
public GenerationTargetLanguage TargetLanguage { get; set; }
public string Description { get; set; }
public string Language { get; set; }
public string[] Tags { get; set; }
}
[When(@"I execute any scenario in the feature")]
public void ExecuteAnyScenario() { }
[Then(@"the FeatureInfo contains the following information")]
public void FeatureInfoContainsInterestingInformation(DataTable table)
{
// Create our small DTO for the info from the step
var fromStep = table.CreateInstance<FeatureInformation>();
fromStep.Tags = table.Rows[0]["Value"].Split(',');
var fi = _featureContext.FeatureInfo;
// Assertions
fi.Title.Should().Equal(fromStep.Title);
fi.GenerationTargetLanguage.Should().Equal(fromStep.TargetLanguage);
fi.Description.Should().StartWith(fromStep.Description);
fi.Language.IetfLanguageTag.Should().Equal(fromStep.Language);
for (var i = 0; i < fi.Tags.Length - 1; i++)
{
fi.Tags[i].Should().Equal(fromStep.Tags[i]);
}
}
```
`FeatureContext` exposes a Binding Culture property that simply points to the culture the feature is written in (en-US in our example).