Is there a way to obtain Feature/Scenario name at run-time

11 views
Skip to first unread message

Steve Wilkinson

unread,
Jan 4, 2011, 5:08:56 AM1/4/11
to SpecFlow
Apologies if this is something obvious I've missed, but I'm trying to
find a way to get the name of the current feature and scenario to add
to my trace output for test debugging. The only way I've found so far
is to walk the stack, but this is obviously guaranteed to break the
moment the internals of SpecFlow change. Here's my current approach:

[Binding]
public class DiagnosticSteps
{
[BeforeScenario]
public void TraceScenarioStarted()
{
ScenarioContext.Current["Name"] = GetCallingMethod(14);

Trace.WriteLine(string.Format("### Scenario {0} started ###",
ScenarioContext.Current["Name"]));
}

static string GetCallingMethod(int frame)
{
System.Diagnostics.StackTrace stackTrace = new
System.Diagnostics.StackTrace();

MethodBase method = stackTrace.GetFrame(frame).GetMethod();

return string.Format("{0}.{1}", method.DeclaringType.Name,
method.Name);
}
}

Is there a better approach, and if not, perhaps this would be a handy
feature for a future release...?

Many thanks in advance - Steve.
Reply all
Reply to author
Forward
0 new messages