<PackageReference Include="NUnit" Version="3.0.0-alpha-4" />

CommandBuilder

public static class CommandBuilder
A utility class to create TestCommands
using NUnit.Framework.Interfaces; using NUnit.Framework.Internal.Commands; using System; using System.Collections.Generic; using System.Reflection; namespace NUnit.Framework.Internal.Execution { public static class CommandBuilder { public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDown, List<TestActionItem> actions) { if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit) return new SkipCommand(suite); TestCommand testCommand = new OneTimeSetUpCommand(suite, setUpTearDown, actions); if ((object)suite.FixtureType != null) { IApplyToContext[] array = (IApplyToContext[])suite.FixtureType.GetCustomAttributes(typeof(IApplyToContext), true); if (array.Length > 0) testCommand = new ApplyChangesToContextCommand(testCommand, array); } return testCommand; } public static TestCommand MakeOneTimeTearDownCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDownItems, List<TestActionItem> actions) { TestCommand testCommand = new OneTimeTearDownCommand(suite, setUpTearDownItems, actions); if (suite.TestType == "Theory") testCommand = new TheoryResultCommand(testCommand); return testCommand; } public static TestCommand MakeTestCommand(TestMethod test) { if (test.RunState != RunState.Runnable && test.RunState != RunState.Explicit) return new SkipCommand(test); TestCommand innerCommand = new TestMethodCommand(test); innerCommand = new TestActionCommand(innerCommand); innerCommand = new SetUpTearDownCommand(innerCommand); object[] customAttributes = test.Method.GetCustomAttributes(typeof(ICommandDecorator), true); for (int i = 0; i < customAttributes.Length; i++) { ICommandDecorator commandDecorator = (ICommandDecorator)customAttributes[i]; innerCommand = commandDecorator.Decorate(innerCommand); } IApplyToContext[] array = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (array.Length > 0) innerCommand = new ApplyChangesToContextCommand(innerCommand, array); return innerCommand; } public static List<SetUpTearDownItem> BuildSetUpTearDownList(Type fixtureType, Type setUpType, Type tearDownType) { MethodInfo[] methodsWithAttribute = Reflect.GetMethodsWithAttribute(fixtureType, setUpType, true); MethodInfo[] methodsWithAttribute2 = Reflect.GetMethodsWithAttribute(fixtureType, tearDownType, true); List<SetUpTearDownItem> list = new List<SetUpTearDownItem>(); while ((object)fixtureType != null && (object)fixtureType != typeof(object)) { SetUpTearDownItem setUpTearDownItem = BuildNode(fixtureType, methodsWithAttribute, methodsWithAttribute2); if (setUpTearDownItem.HasMethods) list.Add(setUpTearDownItem); fixtureType = fixtureType.BaseType; } return list; } private static SetUpTearDownItem BuildNode(Type fixtureType, IList<MethodInfo> setUpMethods, IList<MethodInfo> tearDownMethods) { List<MethodInfo> setUpMethods2 = SelectMethodsByDeclaringType(fixtureType, setUpMethods); List<MethodInfo> tearDownMethods2 = SelectMethodsByDeclaringType(fixtureType, tearDownMethods); return new SetUpTearDownItem(setUpMethods2, tearDownMethods2); } private static List<MethodInfo> SelectMethodsByDeclaringType(Type type, IList<MethodInfo> methods) { List<MethodInfo> list = new List<MethodInfo>(); foreach (MethodInfo method in methods) { if ((object)method.DeclaringType == type) list.Add(method); } return list; } } }