private void RunTestScenarios(string scenarioName, RunContext runContext) { string tempFile = Path.GetTempFileName(); try { string[] pathObjects = runContext.TestFeatures.Keys.ToArray(); if (pathObjects.Length == 0) { return; } _powerShell.AddCommand("Invoke-Gherkin") .AddParameter("Path", pathObjects) //.AddParameter("DetailedCodeCoverage") .AddParameter("CodeCoverageOutputFile", tempFile) .AddParameter("PassThru"); if (!string.IsNullOrEmpty(scenarioName)) { _powerShell.AddParameter("ScenarioName", scenarioName); } string[] codecoverage = GetCodeCoverageFilenames(runContext, name => name.Substring(0, name.Length - Constant.TestsPs1.Length)); if (codecoverage != null && codecoverage.Length > 0) { _powerShell.AddParameter("CodeCoverage", codecoverage); } Collection <PSObject> pesterResults = _powerShell.Invoke(); _powerShell.Commands.Clear(); // The test results are not necessary stored in the first PSObject. Array results = GetTestResults(pesterResults); if (results.Length == 0) { foreach (TestFeature file in runContext.TestFeatures.Values) { file.SetOutcome(TestOutcome.Failed); } } else { int i = 0; TestScenario lastScenario = null; foreach (PSObject result in results) { string filename = result.Properties["Filename"].Value as string; string scenario = result.Properties["Describe"].Value as string; if (filename != null && runContext.TestFeatures.TryGetValue(filename, out TestFeature feature)) { TestScenario testScenario = feature.Scenarios.FirstOrDefault(s => s.Name == scenario); if (testScenario != null) { if (lastScenario != testScenario) { i = 0; lastScenario = testScenario; } testScenario.ProcessTestResults(result, i); } } i++; } Report report = Report.Load(tempFile); if (report != null) { runContext.AddCodeCoverageReport(report); } } } finally { File.Delete(tempFile); } }