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

SetUpTearDownCommand

SetUpTearDownCommand runs any SetUp methods for a suite, runs the test and then runs any TearDown methods.
using NUnit.Framework.Internal.Execution; using System; using System.Collections.Generic; using System.Threading; namespace NUnit.Framework.Internal.Commands { public class SetUpTearDownCommand : DelegatingTestCommand { private IList<SetUpTearDownItem> _setUpTearDownItems; public SetUpTearDownCommand(TestCommand innerCommand) : base(innerCommand) { Guard.ArgumentValid(innerCommand.Test is TestMethod, "SetUpTearDownCommand may only apply to a TestMethod", "innerCommand"); Guard.OperationValid(base.Test.TypeInfo != null, "TestMethod must have a non-null TypeInfo"); _setUpTearDownItems = CommandBuilder.BuildSetUpTearDownList(base.Test.TypeInfo.Type, typeof(SetUpAttribute), typeof(TearDownAttribute)); } public override TestResult Execute(TestExecutionContext context) { try { int num = _setUpTearDownItems.Count; while (num > 0) { _setUpTearDownItems[--num].RunSetUp(context); } context.CurrentResult = innerCommand.Execute(context); } catch (Exception ex) { if (ex is ThreadAbortException) Thread.ResetAbort(); context.CurrentResult.RecordException(ex); } finally { if (context.ExecutionStatus != TestExecutionStatus.AbortRequested) { for (int i = 0; i < _setUpTearDownItems.Count; i++) { _setUpTearDownItems[i].RunTearDown(context); } } } return context.CurrentResult; } } }