You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dispose is not called on test fixtures with LifeCycle.InstancePerTestCase without TearDown method #3953
If I run the following test fixture, the test_that_dispose_was_called passes because the Dispose method is called as expected.
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]publicclassTest_fixture:IDisposable{privatestaticbool_disposeWasCalled;[Test,Order(0)]publicvoidtest_running_first()=>Assert.Pass("Some test running before");[Test,Order(1)]publicvoidtest_that_dispose_was_called()=>Assert.That(_disposeWasCalled);[TearDown]publicstaticvoidTearDown(){}publicvoidDispose()=>_disposeWasCalled=true;}
However, if I remove the TearDown method, the Dispose method is no longer called and _wasDisposed remains false.
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]publicclassTest_fixture:IDisposable{privatestaticbool_disposeWasCalled;[Test,Order(0)]publicvoidtest_running_first()=>Assert.Pass("Some test running before");[Test,Order(1)]publicvoidtest_that_dispose_was_called()=>Assert.That(_disposeWasCalled);publicvoidDispose()=>_disposeWasCalled=true;}
Expected
I would have expected Dispose to be called after every test if the fixture has the attribute FixtureLifeCycle(LifeCycle.InstancePerTestCase) set on it, independent of the presence of a TearDown method. This is expectation is based on the documentation of the FixtureLifeCycleAttribute, which states that IDisposable fixtures are disposed after each test.
changed the title [-]Failing tests cause Dispose not to be called with LifeCycle.InstancePerTestCase[/-][+]Dispose is not called on test fixtures with LifeCycle.InstancePerTestCase without TearDown method[/+]on Oct 5, 2021
It appears to be linked to exceptions being thrown from the test_running_first. If test_running_first throws no exception (Assert.Pass(...) also throws an exception) the Dispose method is called as expected.
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class Test_fixture : IDisposable
{
private static bool _disposeWasCalled;
[Test, Order(0)]
public void test_running_first() {}
[Test, Order(1)]
public void test_that_dispose_was_called() => Assert.That(_disposeWasCalled);
public void Dispose() => _disposeWasCalled = true;
}
Thanks for reporting this @Crown0815 !
I think there's been a few bugs fixed in this area in recent releases. Can you please confirm which version of the NUnit framework you are seeing this on?
When there is a TearDown (or SetUp for that matter), the BeforeAndAfterTestCommand runs the actual test inside a try/catch.
When no such method exists, only an AfterTestCommand is run. This runs the actual test without a try/catch so that if the actual test routine throws an exception, the AfterTest command is not executed.
This might affect other use cases as there are more classes deriving from AfterTestCommand.
Activity
[-]Failing tests cause Dispose not to be called with LifeCycle.InstancePerTestCase[/-][+]Dispose is not called on test fixtures with LifeCycle.InstancePerTestCase without TearDown method[/+]Crown0815 commentedon Oct 5, 2021
It appears to be linked to exceptions being thrown from the
test_running_first. Iftest_running_firstthrows no exception (Assert.Pass(...)also throws an exception) theDisposemethod is called as expected.stevenaw commentedon Oct 5, 2021
Thanks for reporting this @Crown0815 !
I think there's been a few bugs fixed in this area in recent releases. Can you please confirm which version of the NUnit framework you are seeing this on?
Crown0815 commentedon Oct 9, 2021
@stevenaw, I tested this behavior with v3.13.2.
stevenaw commentedon Oct 9, 2021
Thanks for confirming @Crown0815
I've labeled this for follow-up
mikkelbu commentedon Oct 10, 2021
I can confirm it on the 3.13-dev branch, but I'm unsure what the problem is, as I've not been involved much with
FixtureLifeCyclerprouse commentedon Oct 12, 2021
Dispose was working, but this area of the code is fragile. We'll need to look into it.
manfred-brands commentedon Oct 14, 2021
When there is a
TearDown(orSetUpfor that matter), theBeforeAndAfterTestCommandruns the actual test inside atry/catch.When no such method exists, only an
AfterTestCommandis run. This runs the actual test without atry/catchso that if the actual test routine throws an exception, theAfterTestcommand is not executed.This might affect other use cases as there are more classes deriving from
AfterTestCommand.manfred-brands commentedon Oct 14, 2021
I'll create a PR.
mikkelbu commentedon Oct 29, 2021
@rprouse Shouldn't this be in the 3.13.3 milestone? (I guess that 3.13.3 will be released before 4)
rprouse commentedon Oct 29, 2021
If I remember right, the pr for this went to master. There was another pr that cherry picked it, so I marked that with the 3 milestone
mikkelbu commentedon Oct 29, 2021
You are right. #3962 is marked with the 3 milestone