Skip to content

Commit

Permalink
Add test for table arguments being passed to steps (#239)
Browse files Browse the repository at this point in the history
* Add test for table arguments being passed to steps

* Tighten up background handling assertions
  • Loading branch information
Code-Grump authored Aug 26, 2024
1 parent f1fc6c0 commit 92d3de3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
81 changes: 74 additions & 7 deletions Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FluentAssertions;
using Reqnroll.TestProjectGenerator;
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
using System.Collections.Generic;

namespace Reqnroll.SystemTests.Generation;

Expand Down Expand Up @@ -345,24 +348,88 @@ public async Task WhenScenarioStepIsCalled()
var results = _vsTestExecutionDriver.LastTestExecutionResult.LeafTestResults;

results.Should().ContainSingle(tr => tr.TestName == "Background Scenario Steps" || tr.TestName == "BackgroundScenarioSteps")
.Which.Steps.Should().BeEquivalentTo(
.Which.Steps.Select(result => result.Step).Should().BeEquivalentTo(
[
new TestStepResult { Step = "Given background step 1 is called" },
new TestStepResult { Step = "When scenario step is called" }
"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.Should().BeEquivalentTo(
.Which.Steps.Select(result => result.Step).Should().BeEquivalentTo(
[
new TestStepResult { Step = "Given background step 1 is called" },
new TestStepResult { Step = "Given background step 2 is called" },
new TestStepResult { Step = "When scenario step is called" }
"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

//TODO: test parallel execution (details TBD) - maybe this should be in a separate test class
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,19 @@ private List<TestResult> GetTestResultsInternal(XElement testRunResultsElement,
TestName = testNameAttribute.Value,
Outcome = outcomeAttribute.Value,
StdOut = stdOutElement?.Value,
Steps = steps,
Steps = steps.ToList(),
ErrorMessage = errorMessage?.Value,
InnerResults = GetTestResultsInternal(innerResultsElement, xmlns)
};

return testResults.ToList();
}

private static List<TestStepResult> ParseTestOutput(XElement stdOutElement)
private static IEnumerable<TestStepResult> ParseTestOutput(XElement stdOutElement)
{
if (stdOutElement == null)
{
return null;
yield break;
}

var stdOutText = stdOutElement.Value;
Expand All @@ -175,13 +175,32 @@ private static List<TestStepResult> ParseTestOutput(XElement stdOutElement)

var primaryOutput = logLines.TakeWhile(line => line != "TestContext Messages:");

var steps = primaryOutput
.Where(line => line.StartsWith("Given ") ||
line.StartsWith("When ") ||
line.StartsWith("Then ") ||
line.StartsWith("And "));
TestStepResult step = null;

return steps.Select(step => new TestStepResult { Step = step }).ToList();
foreach (var line in primaryOutput)
{
if (line.StartsWith("Given ") ||
line.StartsWith("When ") ||
line.StartsWith("Then ") ||
line.StartsWith("And "))
{
if (step != null)
{
yield return step;
}

step = new TestStepResult { Step = line };
}
else
{
step?.Output.Add(line);
}
}

if (step != null)
{
yield return step;
}
}

private int GetNUnitIgnoredCount(TestExecutionResult testExecutionResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public class TestStepResult
public string Step { get; set; }
public string Error { get; set; }
public string Result { get; set; }
public List<string> Output { get; } = [];
}
}

0 comments on commit 92d3de3

Please sign in to comment.