SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.22k stars 751 forks source link

NullReference exception: BoDi.ObjectContainerException: Object container disposed (MSTest) #2748

Open 7timus opened 1 month ago

7timus commented 1 month ago

SpecFlow Version

3.8.7

Which test runner are you using?

MSTest

Test Runner Version Number

3.8.7

.NET Implementation

.NET Core 3.1

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

When running tests with MSTest from OutputAPI example project the ISpecFlowOutputHelper in BrowserDriver.cs dispose method throws null reference exception on disposal. This happens due to already disposed ObjectContainer that is accessed by MSTestContextProvider

Stack trace:

Message: 
TestCleanup method CalculatorSelenium.Specs.Features.CalculatorFeature.TestTearDown threw exception. BoDi.ObjectContainerException: Object container disposed.

  Stack Trace: 
ObjectContainer.AssertNotDisposed()
ObjectContainer.Resolve(Type typeToResolve, ResolutionList resolutionPath, String name)
ObjectContainer.Resolve(Type typeToResolve, String name)
ObjectContainer.Resolve[T](String name)
ObjectContainer.Resolve[T]()
MSTestTestContextProvider.GetTestContext()
MSTestTraceListener.WriteToolOutput(String message)
SpecFlowOutputHelper.WriteLine(String message)
BrowserDriver.Dispose() line 60
ObjectContainer.Dispose()
InternalContextManager`1.DisposeInstance()
InternalContextManager`1.Cleanup()
ContextManager.CleanupScenarioContext()
TestExecutionEngine.OnScenarioEnd()
TestRunner.OnScenarioEnd()
CalculatorFeature.TestTearDown()

Steps to Reproduce

Clone https://github.com/SpecFlowOSS/SpecFlow-Examples/tree/master/OutputAPI/CalculatorSelenium/CalculatorSelenium.Specs. Change NUnit to MSTest adapter and run any of the tests.

Link to Repro Project

https://github.com/SpecFlowOSS/SpecFlow-Examples/tree/master/OutputAPI/CalculatorSelenium/CalculatorSelenium.Specs

7timus commented 1 month ago

I also noticed the problem with MSTest while running tests in parallel. I duplicated (20 features) the same Calculator feature from the example project and using runsettings configured ClassLevel parallelization on 8 workers. This gives me some null ref exceptions from random tests at each run.

 AddTwoNumbers
   Source: Calculator - Copy (15).feature line 9
   Duration: 1.6 sec

  Message: 
Test method CalculatorSelenium.Specs.Features.Calculator221111Feature.AddTwoNumbers threw exception: 
BoDi.ObjectContainerException: Object container disposed

  Stack Trace: 
ObjectContainer.AssertNotDisposed()
ObjectContainer.Resolve(Type typeToResolve, ResolutionList resolutionPath, String name)
ObjectContainer.Resolve[T](String name)
MSTestTraceListener.WriteToolOutput(String message)
TestTracer.TraceWarning(String text)
InternalContextManager`1.Init(TContext newInstance, IObjectContainer newObjectContainer)
ContextManager.InitializeScenarioContext(ScenarioInfo scenarioInfo)
TestExecutionEngine.OnScenarioInitialize(ScenarioInfo scenarioInfo)
TestRunner.OnScenarioInitialize(ScenarioInfo scenarioInfo)
Calculator221111Feature.ScenarioInitialize(ScenarioInfo scenarioInfo)
Calculator221111Feature.AddTwoNumbers() line 9