diff --git a/Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs b/Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs index e90eaae0d..c145706cb 100644 --- a/Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs +++ b/Tests/Reqnroll.SystemTests/Generation/GenerationTestBase.cs @@ -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; @@ -345,18 +348,18 @@ 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(); @@ -364,5 +367,69 @@ public async Task WhenScenarioStepIsCalled() #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(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> { new() { "A" }, new() { "B" }, new() { "C" } }); + } + + #endregion + //TODO: test parallel execution (details TBD) - maybe this should be in a separate test class } diff --git a/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TRXParser.cs b/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TRXParser.cs index 02d2f2fe1..7e7fd7c13 100644 --- a/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TRXParser.cs +++ b/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TRXParser.cs @@ -154,7 +154,7 @@ private List GetTestResultsInternal(XElement testRunResultsElement, TestName = testNameAttribute.Value, Outcome = outcomeAttribute.Value, StdOut = stdOutElement?.Value, - Steps = steps, + Steps = steps.ToList(), ErrorMessage = errorMessage?.Value, InnerResults = GetTestResultsInternal(innerResultsElement, xmlns) }; @@ -162,11 +162,11 @@ private List GetTestResultsInternal(XElement testRunResultsElement, return testResults.ToList(); } - private static List ParseTestOutput(XElement stdOutElement) + private static IEnumerable ParseTestOutput(XElement stdOutElement) { if (stdOutElement == null) { - return null; + yield break; } var stdOutText = stdOutElement.Value; @@ -175,13 +175,32 @@ private static List 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) diff --git a/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TestExecutionResult.cs b/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TestExecutionResult.cs index e8e8522f0..1c9680b29 100644 --- a/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TestExecutionResult.cs +++ b/Tests/TestProjectGenerator/Reqnroll.TestProjectGenerator/TestExecutionResult.cs @@ -46,5 +46,6 @@ public class TestStepResult public string Step { get; set; } public string Error { get; set; } public string Result { get; set; } + public List Output { get; } = []; } } \ No newline at end of file