SpecFlow icon indicating copy to clipboard operation
SpecFlow copied to clipboard

Teardown [AfterFeature] fails in a multi-scenario feature file

Open dusloth opened this issue 1 year ago • 0 comments

SpecFlow Version

3.9.74

Which test runner are you using?

NUnit

Test Runner Version Number

4.0.1

.NET Implementation

.NET 6.0

Project Format of the SpecFlow project

Sdk-style project format

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

No response

Issue Description

TearDown method fails after final scenario is ran in a [BeforeFeature] and [AfterFeature] setup.

  • Step definitions have a single [BeforeScenario] init of Page Objects -- Steps class inherits hook class to access driver.Value in steps
  • Works fine using [BeforeScenario] and [AfterScenario] and does the exact same using IWebDriver instead of ThreadLocal

Error

TearDown failed for test fixture ConsoleApp2.Features.TestingThingFeature
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
StackTrace: --TearDown
   at ConsoleApp2.Misc.test.TearDown() in C:\Users\user\source\repos\ConsoleApp2\ConsoleApp2\Misc\test.cs:line 32
   at InvokeStub_Action`1.Invoke(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnFeatureEnd()
   at TechTalk.SpecFlow.TestRunner.OnFeatureEnd()
   at ConsoleApp2.Features.TestingThingFeature.FeatureTearDown()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Steps to Reproduce

Hook

[Binding]
 internal class test
 {

     public static ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
     //public static IWebDriver driver;

     [BeforeFeature]
     public static void SetUp()
     {
         new DriverManager().SetUpDriver(new ChromeConfig());
         driver.Value = new ChromeDriver();
     }

     [AfterFeature]
     public static void TearDown()
     {
         driver.Value.Close();
         driver.Value.Quit();
     }

 }

Feature File

Feature: TestingThing

@Thingy1
Scenario: Go to SS Fitness and click btn
	Given I navigate to SS Fitness
	And I wait for page load
	When I click the thing button
	Then I verify page changes properly

Scenario: Go to SS Fitness and click btnn1
	Given I navigate to SS Fitness
	And I wait for page load
	When I click the thing button
	Then I verify page changes properly

Link to Repro Project

No response

dusloth avatar Mar 15 '24 01:03 dusloth