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

SetUpTearDownItem

public class SetUpTearDownItem
SetUpTearDownItem holds the setup and teardown methods for a single level of the inheritance hierarchy.
using System; using System.Collections.Generic; using System.Reflection; namespace NUnit.Framework.Internal.Commands { public class SetUpTearDownItem { private readonly IList<MethodInfo> _setUpMethods; private readonly IList<MethodInfo> _tearDownMethods; private bool _setUpWasRun; public bool HasMethods { get { if (_setUpMethods.Count <= 0) return _tearDownMethods.Count > 0; return true; } } public SetUpTearDownItem(IList<MethodInfo> setUpMethods, IList<MethodInfo> tearDownMethods) { _setUpMethods = setUpMethods; _tearDownMethods = tearDownMethods; } public void RunSetUp(TestExecutionContext context) { _setUpWasRun = true; foreach (MethodInfo setUpMethod in _setUpMethods) { RunSetUpOrTearDownMethod(context, setUpMethod); } } public void RunTearDown(TestExecutionContext context) { if (_setUpWasRun) try { int count = context.CurrentResult.AssertionResults.Count; int num = _tearDownMethods.Count; while (--num >= 0) { RunSetUpOrTearDownMethod(context, _tearDownMethods[num]); } if (context.CurrentResult.AssertionResults.Count > count) context.CurrentResult.RecordTestCompletion(); } catch (Exception ex) { context.CurrentResult.RecordTearDownException(ex); } } private static void RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method) { Guard.ArgumentNotAsyncVoid(method, "method"); if (AsyncToSyncAdapter.IsAsyncOperation(method)) AsyncToSyncAdapter.Await(() => InvokeMethod(method, context)); else InvokeMethod(method, context); } private static object InvokeMethod(MethodInfo method, TestExecutionContext context) { return Reflect.InvokeMethod(method, method.IsStatic ? null : context.TestObject); } } }