Skip to content

Dispose is not called on test fixtures with LifeCycle.InstancePerTestCase without TearDown method #3953

Closed

Description

@Crown0815

Issue

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)]
public class Test_fixture : IDisposable
{
    private static bool _disposeWasCalled;

    [Test, Order(0)]
    public void test_running_first() => Assert.Pass("Some test running before");

    [Test, Order(1)]
    public void test_that_dispose_was_called() => Assert.That(_disposeWasCalled);

    [TearDown]
    public static void TearDown(){}

    public void Dispose() => _disposeWasCalled = true;
}

image

However, if I remove the TearDown method, the Dispose method is no longer called and _wasDisposed remains false.

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class Test_fixture : IDisposable
{
    private static bool _disposeWasCalled;

    [Test, Order(0)]
    public void test_running_first() => Assert.Pass("Some test running before");

    [Test, Order(1)]
    public void test_that_dispose_was_called() => Assert.That(_disposeWasCalled);

    public void Dispose() => _disposeWasCalled = true;
}

image

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.

Activity

  1. 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
  2. Crown0815 commented on Oct 5, 2021

    @Crown0815
    Author

    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;
    }
    

    image

  3. stevenaw commented on Oct 5, 2021

    @stevenaw
    Member

    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?

  4. Crown0815 commented on Oct 9, 2021

    @Crown0815
    Author

    @stevenaw, I tested this behavior with v3.13.2.

  5. stevenaw commented on Oct 9, 2021

    @stevenaw
    Member

    Thanks for confirming @Crown0815

    I've labeled this for follow-up

  6. mikkelbu commented on Oct 10, 2021

    @mikkelbu
    Member

    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 FixtureLifeCycle

  7. added this to the 3.13.3 milestone on Oct 10, 2021
  8. rprouse commented on Oct 12, 2021

    @rprouse
    Member

    Dispose was working, but this area of the code is fragile. We'll need to look into it.

  9. manfred-brands commented on Oct 14, 2021

    @manfred-brands
    Member

    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.

  10. manfred-brands commented on Oct 14, 2021

    @manfred-brands
    Member

    I'll create a PR.

  11. modified the milestones: 3.13.3, 4.0 on Oct 14, 2021
  12. mikkelbu commented on Oct 29, 2021

    @mikkelbu
    Member

    @rprouse Shouldn't this be in the 3.13.3 milestone? (I guess that 3.13.3 will be released before 4)

  13. rprouse commented on Oct 29, 2021

    @rprouse
    Member

    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

  14. mikkelbu commented on Oct 29, 2021

    @mikkelbu
    Member

    You are right. #3962 is marked with the 3 milestone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions