Last active
December 5, 2022 10:39
-
-
Save chrisoldwood/fce752bab1f7060dc7b2 to your computer and use it in GitHub Desktop.
Custom SpecFlow listener and "logging" class to reduce noise when running SpecFlow tests from the command line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using TechTalk.SpecFlow; | |
namespace MyCompany.SpecFlowTests | |
{ | |
[Binding] | |
public static class Hooks | |
{ | |
[BeforeFeature] | |
public static void BeforeFeature() | |
{ | |
Console.WriteLine(); | |
Console.WriteLine("Feature: " + FeatureContext.Current.FeatureInfo.Title); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using TechTalk.SpecFlow; | |
namespace MyCompany.SpecFlowTests | |
{ | |
using LogBuffer = List<string>; | |
[Binding] | |
internal static class ScenarioLog | |
{ | |
public static void Write(string format, params object[] args) | |
{ | |
if (ScenarioContext.Current != null) | |
{ | |
var message = String.Format(format, args); | |
if (!ScenarioContext.Current.ContainsKey(ScenarioLogKey)) | |
ScenarioContext.Current[ScenarioLogKey] = new LogBuffer(); | |
var log = (LogBuffer)ScenarioContext.Current[ScenarioLogKey]; | |
log.Add(message); | |
} | |
} | |
[AfterScenario] | |
public static void HandleScenarioFailure() | |
{ | |
if (ScenarioContext.Current.TestError != null) | |
DumpLog(); | |
} | |
private static void DumpLog() | |
{ | |
if (ScenarioContext.Current.ContainsKey(ScenarioLogKey)) | |
{ | |
Console.WriteLine("\nScenario: {0}", ScenarioContext.Current.ScenarioInfo.Title); | |
var log = (LogBuffer)ScenarioContext.Current[ScenarioLogKey]; | |
foreach (var message in log) | |
Console.WriteLine(message); | |
} | |
} | |
private const string ScenarioLogKey = "ScenarioLog"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using TechTalk.SpecFlow; | |
using TechTalk.SpecFlow.Tracing; | |
// <specFlow> | |
// <trace traceSuccessfulSteps="false" listener="MyCompany.SpecFlowTests.SpecFlowTestListener, MyCompany.SpecFlowTests" /> | |
// </specFlow> | |
namespace MyCompany.SpecFlowTests | |
{ | |
public class SpecFlowTestListener : ITraceListener | |
{ | |
public AcceptanceTestListener() | |
{ | |
var disableTrace = Environment.GetEnvironmentVariable(DisableTraceVariable); | |
if (String.IsNullOrWhiteSpace(disableTrace)) | |
_listener = new DefaultListener(); | |
} | |
public void WriteTestOutput(string message) | |
{ | |
if (_listener != null) | |
_listener.WriteTestOutput(message); | |
} | |
public void WriteToolOutput(string message) | |
{ | |
if (_listener != null) | |
_listener.WriteToolOutput(message); | |
} | |
private readonly ITraceListener _listener; | |
private const string DisableTraceVariable = "DISABLE_SPECFLOW_TRACE_OUTPUT"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems like this will not work any more with specflow 3