/
mstest.md
153 lines (105 loc) · 3.33 KB
/
mstest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# MSTest
Reqnroll supports MsTest V2 (NuGet Version 2.2.8 or higher).
Documentation for MSTest can be found [here](https://docs.microsoft.com/en-us/visualstudio/test/unit-test-your-code?view=vs-2019).
## Needed NuGet Packages
For Reqnroll: [Reqnroll.MSTest](https://www.nuget.org/packages/Reqnroll.MSTest/)
For MSTest: [MSTest.TestFramework](https://www.nuget.org/packages/MSTest.TestFramework/)
For Test Discovery & Execution:
- [MSTest.TestAdapter](https://www.nuget.org/packages/MSTest.TestAdapter/)
- [Microsoft.NET.Test.Sdk](https://www.nuget.org/packages/Microsoft.NET.Test.Sdk)
## Accessing TestContext
You can access the MsTest TestContext instance in your step definition or hook classes by constructor injection:
``` csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;
[Binding]
public class MyStepDefs
{
private readonly TestContext _testContext;
public MyStepDefs(TestContext testContext) // use it as ctor parameter
{
_testContext = testContext;
}
[Given("a step")]
public void GivenAStep()
{
//you can access the TestContext injected in the ctor
_testContext.WriteLine(_testContext.TestRunDirectory);
}
[BeforeScenario()]
public void BeforeScenario()
{
//you can access the TestContext injected in the ctor
_testContext.WriteLine(_testContext.TestRunDirectory);
}
}
```
In the static BeforeTestRun/AfterTestRun hooks you can use parameter injection:
``` csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;
[Binding]
public class Hooks
{
[BeforeTestRun]
public static void BeforeTestRun(TestContext testContext)
{
//you can access the TestContext injected as parameter
testContext.WriteLine(testContext.TestRunDirectory);
}
[AfterTestRun]
public static void AfterTestRun(TestContext testContext)
{
//you can access the TestContext injected as parameter
testContext.WriteLine(testContext.DeploymentDirectory);
}
}
```
## Tags for TestClass Attributes
The MsTest Generator can generate test class attributes from tags specified on a **feature**.
### Owner
Tag:
``` gherkin
@Owner:John
```
Output:
``` csharp
[Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute("John")]
```
### Priority
Tag:
``` gherkin
@Priority:1
```
Output:
``` csharp
[Microsoft.VisualStudio.TestTools.UnitTesting.PriorityAttribute(1)]
```
Remarks:
The attribute is generated only when the value is a valid integer (valid means supported by [` int.TryParse `](https://learn.microsoft.com/it-it/dotnet/api/system.int32.tryparse?#system-int32-tryparse(system-string-system-int32@)))
### WorkItem
Tag:
``` gherkin
@WorkItem:123
```
Output:
``` csharp
[Microsoft.VisualStudio.TestTools.UnitTesting.WorkItemAttribute(123)]
```
### DeploymentItem
#### Example 1 : Copy a file to the same directory as the deployed test assemblies
Tag:
``` gherkin
@MsTest:DeploymentItem:test.txt
```
Output:
``` csharp
[Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute("test.txt")]
```
#### Example 2 : Copy a file to a sub-directory relative to the deployment directory
Tag:
``` gherkin
@MsTest:DeploymentItem:Resources\DeploymentItemTestFile.txt:Data
```
Output:
``` csharp
[Microsoft.VisualStudio.TestTools.UnitTesting.DeploymentItemAttribute("Resources\\DeploymentItemTestFile.txt", "Data")]
```