Skip to content

Instantly share code, notes, and snippets.

@chrisoldwood
Last active December 5, 2022 10:39
Show Gist options
  • Save chrisoldwood/fce752bab1f7060dc7b2 to your computer and use it in GitHub Desktop.
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.
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);
}
}
}
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";
}
}
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";
}
}
Copy link

ghost commented May 4, 2022

seems like this will not work any more with specflow 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment