-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathLastTestEventReceiverTests.cs
More file actions
231 lines (194 loc) · 6.62 KB
/
LastTestEventReceiverTests.cs
File metadata and controls
231 lines (194 loc) · 6.62 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using TUnit.Core;
using TUnit.Core.Interfaces;
namespace TUnit.TestProject;
public class LastTestEventReceiverTests
{
public static readonly List<string> Events = [];
[Before(Test)]
public void ClearEvents()
{
Events.Clear();
}
[Test]
[LastTestEventReceiver]
public async Task Test1()
{
await Task.Delay(10);
}
[Test]
[LastTestEventReceiver]
public async Task Test2()
{
await Task.Delay(10);
}
[Test]
[LastTestEventReceiver]
public async Task Test3()
{
await Task.Delay(10);
}
[After(Test)]
public async Task VerifyLastTestEventFired(TestContext context)
{
// Give some time for async event receivers to complete
await Task.Delay(100);
var displayName = context. Metadata.DisplayName;
// After the last test (Test3), we should have the last test event recorded
if (displayName.Contains("Test3"))
{
await Assert.That(Events).Contains("LastTestInClass");
}
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class LastTestEventReceiverAttribute : Attribute,
ITestStartEventReceiver,
ITestEndEventReceiver,
ILastTestInClassEventReceiver
{
public int Order => 0;
public ValueTask OnTestStart(TestContext context)
{
LastTestEventReceiverTests.Events.Add($"TestStart: {context. Metadata.DisplayName}");
return default;
}
public ValueTask OnTestEnd(TestContext context)
{
LastTestEventReceiverTests.Events.Add($"TestEnd: {context. Metadata.DisplayName}");
return default;
}
public ValueTask OnLastTestInClass(ClassHookContext context, TestContext testContext)
{
LastTestEventReceiverTests.Events.Add("LastTestInClass");
return default;
}
}
// Separate test class to test assembly-level last test event
public class LastTestInAssemblyEventReceiverTests
{
public static readonly List<string> Events = [];
[Before(Test)]
public void ClearEvents()
{
Events.Clear();
}
[Test]
[LastTestInAssemblyEventReceiver]
public async Task AssemblyTest()
{
await Task.Delay(10);
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class LastTestInAssemblyEventReceiverAttribute : Attribute,
ILastTestInAssemblyEventReceiver
{
public int Order => 0;
public ValueTask OnLastTestInAssembly(AssemblyHookContext context, TestContext testContext)
{
LastTestInAssemblyEventReceiverTests.Events.Add("LastTestInAssembly");
return default;
}
}
// Test for skipped event receivers using [Skip] attribute.
// After(Test) hooks don't run for statically skipped tests, so we use a
// DependsOn verification test that runs after the skipped test completes.
public class SkippedEventReceiverTests
{
public static readonly List<string> Events = [];
public static string? CapturedSkipReason = null;
[Test, Skip("Testing skip event with custom reason")]
[SkipEventReceiverAttribute]
public async Task SkippedTestWithCustomReason()
{
await Task.Delay(10);
}
[Test]
[DependsOn(nameof(SkippedTestWithCustomReason), ProceedOnFailure = true)]
public async Task Verify_SkipEventReceiver_Fired_Exactly_Once()
{
// Events were populated by the SkipEventReceiverAttribute on the skipped test.
// No Before(Test) clearing here — we need to observe the cumulative state.
await Assert.That(Events).Contains("TestSkipped");
await Assert.That(Events).Contains("TestEnd");
await Assert.That(CapturedSkipReason).IsEqualTo("Testing skip event with custom reason");
// Guard against double invocation
var skipCount = Events.Count(e => e == "TestSkipped");
await Assert.That(skipCount).IsEqualTo(1);
var endCount = Events.Count(e => e == "TestEnd");
await Assert.That(endCount).IsEqualTo(1);
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class SkipEventReceiverAttribute : Attribute,
ITestSkippedEventReceiver,
ITestEndEventReceiver
{
public int Order => 0;
public ValueTask OnTestSkipped(TestContext context)
{
SkippedEventReceiverTests.Events.Add("TestSkipped");
SkippedEventReceiverTests.CapturedSkipReason = context.Execution.SkipReason;
return default;
}
public ValueTask OnTestEnd(TestContext context)
{
SkippedEventReceiverTests.Events.Add("TestEnd");
return default;
}
}
// Test for runtime skip using Skip.Test()
public class RuntimeSkipEventReceiverTests
{
public static readonly List<string> Events = [];
public static string? CapturedSkipReason = null;
[Before(Test)]
public void ClearEvents()
{
Events.Clear();
CapturedSkipReason = null;
}
[Test]
[RuntimeSkipEventReceiverAttribute]
public void RuntimeSkippedTestWithCustomReason()
{
Skip.Test("Custom runtime skip reason");
}
[After(Test)]
public async Task VerifyRuntimeSkipEventFired(TestContext context)
{
// Give some time for async event receivers to complete
await Task.Delay(100);
if (context. Metadata.DisplayName.Contains("RuntimeSkippedTestWithCustomReason"))
{
// Verify skip reason is preserved
await Assert.That(CapturedSkipReason).IsEqualTo("Custom runtime skip reason");
// Verify both skipped and end events are fired
await Assert.That(Events).Contains("TestSkipped");
await Assert.That(Events).Contains("TestEnd");
// Verify events are called exactly once (not twice)
var testSkippedCount = Events.Count(e => e == "TestSkipped");
await Assert.That(testSkippedCount).IsEqualTo(1);
var testEndCount = Events.Count(e => e == "TestEnd");
await Assert.That(testEndCount).IsEqualTo(1);
}
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class RuntimeSkipEventReceiverAttribute : Attribute,
ITestSkippedEventReceiver,
ITestEndEventReceiver
{
public int Order => 0;
public ValueTask OnTestSkipped(TestContext context)
{
RuntimeSkipEventReceiverTests.Events.Add("TestSkipped");
RuntimeSkipEventReceiverTests.CapturedSkipReason = context.Execution.SkipReason;
return default;
}
public ValueTask OnTestEnd(TestContext context)
{
RuntimeSkipEventReceiverTests.Events.Add("TestEnd");
return default;
}
}