title: How do I create an Extent Report in C# when running BDD tests in Parallel with Specflow
body: <p>When I run C# Specflow tests singularly my Extent Report is generated correctly.</p>
  
  <p>However, when I run the tests in Parallel the Extent Report is wrong. Steps get written to the wrong Scenarios and Scenarios from the same Feature are not grouped.</p>
  
  <p>Has anyone managed to generate an ExtentReport correctly when running tests in parallel using C# and Specflow?</p>
  
  <p>Below is the code I am currently using.</p>
  
  <p>Thanks for any help.</p>
  
  <pre><code>using AutomationFramework.Base;
  using TechTalk.SpecFlow;
  using AutomationFramework.Helpers;
  using AutomationFramework.Config;
  using AventStack.ExtentReports;
  using AventStack.ExtentReports.Reporter;
  using AventStack.ExtentReports.Gherkin.Model;
  
  namespace EmployeeTest.Hooks
  {
  
  [Binding]
  public class HookInitialize : TestInitializeHook
  {
  
      private readonly ParallelConfig _parallelConfig;
      private readonly FeatureContext _featureContext;
      private readonly ScenarioContext _scenarioContext;
  
      public HookInitialize(ParallelConfig parallelConfig, FeatureContext featureContext, ScenarioContext scenarioContext) : base(parallelConfig)
      {
          _parallelConfig = parallelConfig;
          _featureContext = featureContext;
          _scenarioContext = scenarioContext;
      }
  
      private static ExtentTest featureName;
      private static ExtentTest scenario;
      private static ExtentReports extent;
  
  
      [BeforeTestRun]
      public static void TestInitalize()
      {
          //Initialize the Report
          var htmlReporter = new ExtentHtmlReporter(@"C:\Logs\ExtentReport.html");
          htmlReporter.Config.Theme = AventStack.ExtentReports.Reporter.Configuration.Theme.Dark;
          htmlReporter.Config.ReportName = "Automation Test Report";
          extent = new ExtentReports();
          extent.AttachReporter(htmlReporter);
  
      }
  
  
      [AfterStep]
      public void InsertReportingSteps()
      {
          var stepType = _scenarioContext.StepContext.StepInfo.StepDefinitionType.ToString();
  
  
          if (_scenarioContext.TestError == null)
          {
              if (stepType == "Given")
                  scenario.CreateNode&lt;Given&gt;(_scenarioContext.StepContext.StepInfo.Text);
              else if (stepType == "When")
                  scenario.CreateNode&lt;When&gt;(_scenarioContext.StepContext.StepInfo.Text);
              else if (stepType == "Then")
                  scenario.CreateNode&lt;Then&gt;(_scenarioContext.StepContext.StepInfo.Text);
              else if (stepType == "And")
                  scenario.CreateNode&lt;And&gt;(_scenarioContext.StepContext.StepInfo.Text);
          }
          else if (_scenarioContext.TestError != null)
          {
              if (stepType == "Given")
              {
                  scenario.CreateNode&lt;Given&gt;(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.InnerException);
                  ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
              }
              else if (stepType == "When")
              {
                  scenario.CreateNode&lt;When&gt;(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.InnerException);
                  ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
              }
              else if (stepType == "Then")
              {
                  scenario.CreateNode&lt;Then&gt;(_scenarioContext.StepContext.StepInfo.Text).Fail(_scenarioContext.TestError.Message);
                  ScreenShotHelpers.CaptureScreen(_parallelConfig.Driver, _featureContext.FeatureInfo.Title);
              }
          }
  
  
  
      }
  
      [BeforeScenario]
      public void Initialize()
      {
          InitializeSettings();
          Settings.ApplicationCon = Settings.ApplicationCon.DBConnect(Settings.AppConnectionString);
  
          //Create feature name
          featureName = extent.CreateTest&lt;Feature&gt;(_featureContext.FeatureInfo.Title);
  
          //Get scenario name
          scenario = featureName.CreateNode&lt;Scenario&gt;(_scenarioContext.ScenarioInfo.Title);
  
      }
  
  
      [AfterScenario]
      public void TestStop()
      {
          _parallelConfig.Driver.Quit();
  
      }
  
      [AfterTestRun]
      public static void TearDownReport()
      {
          //Write the report to the report directory
          extent.Flush();
  
  
      }
  
  }
  </code></pre>
  
  <p>}</p>