<PackageReference Include="NUnit" Version="4.3.2" />

DisposeFixtureCommand

OneTimeTearDownCommand performs any teardown actions specified for a suite and calls Dispose on the user test object, if any.
using NUnit.Framework.Interfaces; using System; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal.Commands { [NullableContext(1)] [Nullable(0)] public class DisposeFixtureCommand : AfterTestCommand { public DisposeFixtureCommand(TestCommand innerCommand) : base(innerCommand) { Guard.OperationValid(HasDisposableFixture(base.Test), "DisposeFixtureCommand does not apply neither to " + base.Test.GetType().Name + ", nor to " + (base.Test.Parent?.GetType().Name ?? "it's parent (null)")); AfterTest = delegate(TestExecutionContext context) { try { DisposeHelper.EnsureDisposed(context.TestObject); } catch (Exception ex) { context.CurrentResult.RecordTearDownException(ex); } }; } private static bool HasDisposableFixture(ITest test) { ITest test2 = test; do { if (test2 is IDisposableFixture) return true; test2 = test2.Parent; } while (test2 != null); return false; } } }