2

I have a hooks.cs Binding file which have BeforeTestRun, BeforeFeature and BeforeScenario. I need Title of current Feature and scenario for log and report purposes. Since I am running the test in parallel, ScenarioContext throws exception as:

The ScenarioContext.Current static accessor cannot be used in multi-threaded execution...

Is there any way that I could get the current feature title and scenario title in multi-threaded execution?

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Sathish
  • 350
  • 3
  • 24
  • 1
    This question is technically a duplicate, but the question and answer aren't as good: https://stackoverflow.com/q/48586809/3092298 – Greg Burghardt Jan 31 '20 at 16:30

1 Answers1

5

Yes, you can get the current FeatureContext and ScenarioContext by getting it via constructor injection.

public class MyBindingClass
{
  private ScenarioContext scenarioContext;

  public MyBindingClass(ScenarioContext scenarioContext)
  {
    this.scenarioContext = scenarioContext;
  }

  [When("I say hello to ScenarioContext")]
  public void WhenISayHello()
  {
    // access scenarioContext here
  }
}

See https://specflow.org/documentation/ScenarioContext/ - Injecting ScenarioContext at the bottom.

Andreas Willich
  • 5,665
  • 3
  • 15
  • 22