Open qrjo opened 2 years ago
Did you repo this also without breakpoints? From my experience with the test runners, they don't give you much time to do stuff in a test run tear down, so it could simply be that they end the execution before the code is executed.
Yeah, I've tried that too, but it's still not hitting the Dispose method of TestClass1.
As for not having enough time to do anything: AfterTestRun methods work as intended, so I don't think time should be an issue.
I have investigated this a bit and have a few suggestions.
The first option is not use the BeforeTestRun hook, but to use the BeforeFeature hook. As long as the lifecycle and concurrency requirements of your object(s) can live with being instantiated and disposed for each Feature, this might be a cleaner way.
If use of BeforeTestRun is necessary, the following will work: ` [BeforeTestRun] public static void BeforeTestRun(ObjectContainer container) { container.BaseContainer.RegisterTypeAs<TestClass1, TestClass1>();
var tc1 = container.Resolve<TestClass1>();
}
` This approach injects the ObjectContainer class rather than the interface and relies on the knowledge that the TestThread container's BaseContainer is the global container. The global container is reliably disposed of (while the TestThread container does not appear to be). This approach is less clean than the above because it does rely on the current structure of SF classes which might change in the future.
@SabotageAndi - The TestThreadContext is Disposable. But the Dispose method does not dispose of its Container. Is that a bug or by design?
`public class TestThreadContext : SpecFlowContext, ITestThreadContext
{
public event Action
public TestThreadContext(IObjectContainer testThreadContainer)
{
TestThreadContainer = testThreadContainer;
}
protected override void Dispose()
{
Disposing?.Invoke(this);
base.Dispose();
}
} ` HTH
SpecFlow Version
3.9.74
Which test runner are you using?
NUnit
Test Runner Version Number
3.13.3
.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
N/A
Issue Description
When registering a type in the IObjectContainer from a binding method with the
BeforeTestRun
attribute, the type is constructed at the start of the test run, but it is not disposed at the end of the test run. This is different from registering from e.g. aBeforeScenario
method, where the type is constructed before every scenario and is then disposed after every scenario.Steps to Reproduce
Use the following code and put breakpoints in the dispose methods. Make sure to either put "When test1" or "When test2" (or both) in a scenario.
Link to Repro Project
No response