-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathGenerationTestBase.cs
562 lines (471 loc) · 21.2 KB
/
GenerationTestBase.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using System.Text.Json;
using System.Collections.Generic;
using Reqnroll.TestProjectGenerator;
namespace Reqnroll.SystemTests.Generation;
[TestCategory("Generation")]
public abstract class GenerationTestBase : SystemTestBase
{
[TestCategory("focus")]
[TestMethod]
public void GeneratorAllIn_sample_can_be_handled()
{
PrepareGeneratorAllInSamples();
ExecuteTests();
ShouldAllScenariosPass();
ShouldFinishWithoutTestExecutionWarnings();
}
[TestMethod]
public void GeneratorAllIn_sample_can_be_handled_with_VisualBasic()
{
_testRunConfiguration.ProgrammingLanguage = ProgrammingLanguage.VB;
PrepareGeneratorAllInSamples();
ExecuteTests();
ShouldAllScenariosPass();
}
[TestMethod]
public void Handles_simple_scenarios_without_namespace_collisions()
{
_projectsDriver.CreateProject("CollidingNamespace.Reqnroll", "C#");
AddSimpleScenarioAndOutline();
AddScenario(
"""
Scenario: Scenario with DataTable
When something happens with
| who | when |
| me | today |
| someone else | tomorrow |
""");
AddPassingStepBinding();
ExecuteTests();
ShouldAllScenariosPass();
}
#region Test different outcomes: success, failure, pending, undefined, ignored (scenario & scenario outline)
protected virtual string GetExpectedPendingOutcome() => "NotExecuted";
protected virtual string GetExpectedUndefinedOutcome() => "NotExecuted";
protected virtual string GetExpectedIgnoredOutcome() => "NotExecuted";
[TestMethod]
public void Handles_different_scenario_and_scenario_outline_outcomes()
{
AddFeatureFile(
"""
Feature: Sample Feature
Scenario: Passing scenario
When the step passes
Scenario: Failing scenario
When the step fails
Scenario: Pending scenario
When the step is pending
Scenario: Undefined scenario
When the step is undefined
@ignore
Scenario: Ignored scenario
When the step fails
Scenario Outline: SO
When the step <result>
Examples:
| result |
| passes |
| fails |
| is pending |
| is undefined |
@ignore
Examples:
| result |
| ignored |
@ignore
Scenario Outline: ExampleIgnored
When the step <result>
Examples:
| result |
| fails |
@ignore
Rule: Scenario in this Rule should be Ignored
Scenario: Ruleignored scenario
When the step passes
""");
_projectsDriver.AddPassingStepBinding(stepRegex: "the step passes");
_projectsDriver.AddFailingStepBinding(stepRegex: "the step fails");
_projectsDriver.AddStepBinding("StepDefinition", regex: "the step is pending", "throw new PendingStepException();", "Throw New PendingStepException()");
ExecuteTests();
// handles PASSED
var expectedPassedOutcome = "Passed";
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Passing"))
.Which.Outcome.Should().Be(expectedPassedOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("SO") && tr.TestName.Contains("passes"))
.Which.Outcome.Should().Be(expectedPassedOutcome);
// handles FAILED
var expectedFailedOutcome = "Failed";
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Failing"))
.Which.Outcome.Should().Be(expectedFailedOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("SO") && tr.TestName.Contains("fails"))
.Which.Outcome.Should().Be(expectedFailedOutcome);
// handles PENDING
var expectedPendingOutcome = GetExpectedPendingOutcome();
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Pending"))
.Which.Outcome.Should().Be(expectedPendingOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("SO") && tr.TestName.Contains("pending"))
.Which.Outcome.Should().Be(expectedPendingOutcome);
// handles UNDEFINED
var expectedUndefinedOutcome = GetExpectedUndefinedOutcome();
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Undefined"))
.Which.Outcome.Should().Be(expectedUndefinedOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("SO") && tr.TestName.Contains("undefined"))
.Which.Outcome.Should().Be(expectedUndefinedOutcome);
// handles IGNORED
var expectedIgnoredOutcome = GetExpectedIgnoredOutcome();
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Ignored"))
.Which.Outcome.Should().Be(expectedIgnoredOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("ExampleIgnored"))
.Which.Outcome.Should().Be(expectedIgnoredOutcome);
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().ContainSingle(tr => tr.TestName.StartsWith("Ruleignored"))
.Which.Outcome.Should().Be(expectedIgnoredOutcome);
AssertIgnoredScenarioOutlineExampleHandled();
}
protected virtual void AssertIgnoredScenarioOutlineExampleHandled()
{
// the scenario outline examples ignored by a tag on the examples block are not generated
_vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults
.Should().NotContain(tr => tr.TestName.StartsWith("SO") && tr.TestName.Contains("ignored"))
.And.Subject.Where(tr => tr.TestName.StartsWith("SO")).Should().HaveCount(4);
}
#endregion
#region Test async steps (async steps are executed in order)
[TestMethod]
public void Async_steps_are_executed_in_order()
{
AddScenario(
"""
Scenario: Async Scenario Steps
When Async step 1 is called
When Async step 2 is called
When Async step 3 is called
""");
AddBindingClass(
"""
namespace AsyncSequence.StepDefinitions
{
[Binding]
public class AsyncSequenceStepDefinitions
{
[When("Async step 1 is called")]
public async Task WhenStep1IsTaken()
{
await Task.Run(() => global::Log.LogStep() );
}
[When("Async step 2 is called")]
public async Task WhenStep2IsTaken()
{
await Task.Run(() => global::Log.LogStep() );
}
[When("Async step 3 is called")]
public async Task WhenStep3IsTaken()
{
await Task.Run(() => global::Log.LogStep() );
}
}
}
""");
ExecuteTests();
_bindingDriver.AssertExecutedStepsEqual("WhenStep1IsTaken", "WhenStep2IsTaken", "WhenStep3IsTaken");
ShouldAllScenariosPass();
}
#endregion
#region Test hooks: before/after run, feature & scenario hook (require special handling by test frameworks)
[TestMethod]
public void TestRun_Feature_and_Scenario_hooks_are_executed_in_right_order()
{
var testsInFeatureFile = 3;
AddSimpleScenario();
AddSimpleScenarioOutline(testsInFeatureFile - 1);
AddPassingStepBinding();
AddHookBinding("BeforeTestRun");
AddHookBinding("AfterTestRun");
AddHookBinding("BeforeFeature");
AddHookBinding("AfterFeature");
AddHookBinding("BeforeScenario");
AddHookBinding("AfterScenario");
ExecuteTests();
_bindingDriver.AssertExecutedHooksEqual(
"BeforeTestRun",
"BeforeFeature",
"BeforeScenario",
"AfterScenario",
"BeforeScenario",
"AfterScenario",
"BeforeScenario",
"AfterScenario",
"AfterFeature",
"AfterTestRun");
ShouldAllScenariosPass();
}
#endregion
#region Test scenario outlines (nr of examples, params are available in ScenarioContext, allowRowTests=false, examples tags)
[TestMethod]
public void Scenario_outline_examples_gather_tags_and_parameters()
{
AddFeatureFile(
"""
@feature_tag
Feature: Sample Feature
@rule_tag
Rule: Sample Rule
@so1_tag
Scenario Outline: SO1
When <what1> happens to <person>
Examples:
| what1 | person |
| something | me |
| nothing | you |
@so2_tag
Scenario Outline: SO2
When <what2> happens to <person>
Examples:
| what2 | person |
| something | me |
@e3_tag
Examples: E3
| what2 | person |
| nothing | you |
""");
_projectsDriver.AddStepBinding("StepDefinition", regex: ".*",
"""
global::Log.LogCustom("tags", string.Join(",", _scenarioContext.ScenarioInfo.CombinedTags));
global::Log.LogCustom("parameters", string.Join(",", _scenarioContext.ScenarioInfo.Arguments.OfType<System.Collections.DictionaryEntry>().Select(kv => $"{kv.Key}={kv.Value}")));
""");
ExecuteTests();
ShouldAllScenariosPass(4);
_bindingDriver.GetActualLogLines("tags").Should().BeEquivalentTo(
"-> tags: so1_tag,feature_tag,rule_tag:StepBinding",
"-> tags: so1_tag,feature_tag,rule_tag:StepBinding",
"-> tags: so2_tag,feature_tag,rule_tag:StepBinding",
"-> tags: so2_tag,e3_tag,feature_tag,rule_tag:StepBinding");
_bindingDriver.GetActualLogLines("parameters").Should().BeEquivalentTo(
"-> parameters: what1=something,person=me:StepBinding",
"-> parameters: what1=nothing,person=you:StepBinding",
"-> parameters: what2=something,person=me:StepBinding",
"-> parameters: what2=nothing,person=you:StepBinding");
}
#endregion
#region Test background support (backgrounds steps are executed at the appropriate times)
[TestMethod]
public void Handles_scenario_with_backgrounds()
{
AddFeatureFile(
"""
Feature: Sample Feature
Background:
Given background step 1 is called
Scenario: Background Scenario Steps
When scenario step is called
Rule: Rule with background
Background:
Given background step 2 is called
Scenario: Rule Background Scenario Steps
When scenario step is called
""");
AddBindingClass(
"""
namespace Background.StepDefinitions
{
[Binding]
public class BackgroundStepDefinitions
{
[Given("background step 1 is called")]
public async Task GivenBackgroundStep1IsCalled()
{
await Task.Run(() => global::Log.LogStep() );
}
[Given("background step 2 is called")]
public async Task GivenBackgroundStep2IsCalled()
{
await Task.Run(() => global::Log.LogStep() );
}
[When("scenario step is called")]
public async Task WhenScenarioStepIsCalled()
{
await Task.Run(() => global::Log.LogStep() );
}
}
}
""");
ExecuteTests();
var results = _vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults;
results.Should().ContainSingle(tr => tr.TestName == "Background Scenario Steps" || tr.TestName == "BackgroundScenarioSteps")
.Which.Steps.Select(result => result.Step).Should().BeEquivalentTo(
[
"Given background step 1 is called",
"When scenario step is called"
]);
results.Should().ContainSingle(tr => tr.TestName == "Rule Background Scenario Steps" || tr.TestName == "RuleBackgroundScenarioSteps")
.Which.Steps.Select(result => result.Step).Should().BeEquivalentTo(
[
"Given background step 1 is called",
"Given background step 2 is called",
"When scenario step is called"
]);
ShouldAllScenariosPass();
}
#endregion
#region Test tables arguments are processed
[TestMethod]
public void Table_arguments_are_passed_to_steps()
{
AddScenario(
"""
Scenario: Using tables with steps
When this table is processed
| Example |
| A |
| B |
| C |
""");
AddBindingClass(
"""
namespace TableArguments.StepDefinitions
{
[Binding]
public class TableArgumentSteps
{
[When("this table is processed")]
public async Task WhenThisTableIsProcessed(DataTable table)
{
var tableData = new
{
headings = table.Header.ToList(),
rows = table.Rows.Select(row => row.Select(kvp => kvp.Value)).ToList()
};
Log.LogCustom("argument", $"table = {System.Text.Json.JsonSerializer.Serialize(tableData)}");
}
}
}
""");
ExecuteTests();
var arguments = _bindingDriver.GetActualLogLines("argument").ToList();
arguments.Should().NotBeEmpty();
arguments[0].Should().StartWith("-> argument: table = ");
var tableSource = arguments[0];
var tableJson = tableSource[tableSource.IndexOf('{')..(tableSource.LastIndexOf('}')+1)];
var tableData = JsonSerializer.Deserialize<JsonElement>(tableJson);
var actualHeadings = tableData
.GetProperty("headings")
.EnumerateArray()
.Select(item => item.ToString());
var actualRows = tableData
.GetProperty("rows")
.EnumerateArray()
.Select(item => item.EnumerateArray().Select(data => data.ToString()).ToList());
actualHeadings.Should().BeEquivalentTo(["Example"]);
actualRows.Should().BeEquivalentTo(new List<List<string>> { new() { "A" }, new() { "B" }, new() { "C" } });
}
#endregion
#region It is able to run tests parallel
// We need to verify
// Tests are run in parallel
// Each scenario execution has a feature context and scenario context that is matching to the feature/scenario
// For each scenario the before feature has been executed for that feature context
[TestMethod]
public void Tests_can_be_executed_parallel()
{
_projectsDriver.EnableTestParallelExecution();
var scenarioTemplate =
"""
Scenario: {0}
When executing '{0}' in '{1}'
""";
var scenarioOutlineTemplate =
"""
Scenario Outline: {0}
When executing '{0}' in '{1}'
Examples:
| Count |
| 1 |
| 2 |
| 3 |
""";
const int scenariosPerFile = 3;
const int scenarioOutlinesPerFile = 3;
string[] features = ["A", "B"];
int scenarioIdCounter = 0;
foreach (string feature in features)
{
AddFeatureFile($"Feature: {feature}" + Environment.NewLine);
for (int i = 0; i < scenariosPerFile; i++)
AddScenario(string.Format(scenarioTemplate, $"S{++scenarioIdCounter}", feature));
for (int i = 0; i < scenarioOutlinesPerFile; i++)
AddScenario(string.Format(scenarioOutlineTemplate, $"S{++scenarioIdCounter}", feature));
}
AddBindingClass(
"""
namespace ParallelExecution.StepDefinitions
{
[Binding]
public class ParallelExecutionSteps
{
public static int startIndex = 0;
public static int featureAStartIndex = 0;
public static int featureBStartIndex = 0;
private readonly FeatureContext _featureContext;
private readonly ScenarioContext _scenarioContext;
public ParallelExecutionSteps(FeatureContext featureContext, ScenarioContext scenarioContext)
{
_featureContext = featureContext;
_scenarioContext = scenarioContext;
}
[BeforeFeature]
public static void BeforeFeature(FeatureContext featureContext)
{
featureContext.Set(true, "before_feature");
}
[When("executing {string} in {string}")]
public void WhenExecuting(string scenarioName, string featureName)
{
var currentStartIndex = System.Threading.Interlocked.Increment(ref startIndex);
var currentFeatureStartIndex = featureName == "A"
? System.Threading.Interlocked.Increment(ref featureAStartIndex)
: System.Threading.Interlocked.Increment(ref featureBStartIndex);
global::Log.LogStep();
if (_scenarioContext.ScenarioInfo.Title != scenarioName)
throw new System.Exception($"Invalid scenario context: {_scenarioContext.ScenarioInfo.Title} should be {scenarioName}");
if (_featureContext.FeatureInfo.Title != featureName)
throw new System.Exception($"Invalid scenario context: {_featureContext.FeatureInfo.Title} should be {featureName}");
if (!_featureContext.TryGetValue<bool>("before_feature", out var value) || !value)
throw new System.Exception($"BeforeFeature hook was not executed!");
System.Threading.Thread.Sleep(10);
var afterStartIndex = startIndex;
if (afterStartIndex != currentStartIndex)
Log.LogCustom("parallel", "true");
var afterFeatureStartIndex = featureName == "A" ? featureAStartIndex : featureBStartIndex;
if (afterFeatureStartIndex != currentFeatureStartIndex)
Log.LogCustom("scenario-parallel", "true");
}
}
}
""");
ExecuteTests();
ShouldAllScenariosPass();
var parallelLogs = _bindingDriver.GetActualLogLines("parallel").ToList();
parallelLogs.Should().NotBeEmpty("the scenarios should have run parallel");
AssertScenarioLevelParallelExecution();
}
protected virtual void AssertScenarioLevelParallelExecution()
{
var scenarioParallelLogs = _bindingDriver.GetActualLogLines("scenario-parallel").ToList();
scenarioParallelLogs.Should().NotBeEmpty("the scenarios should have run parallel using scenario-level parallelization");
}
#endregion
}