/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { // Command to execute test TestCommand command = new TestMethodCommand(test); // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in test.Method.GetCustomAttributes <IWrapTestMethod>(true)) { command = wrapper.Wrap(command); } // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in test.Method.GetCustomAttributes <IWrapSetUpTearDown>(true)) { command = decorator.Wrap(command); } // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = test.Method.GetCustomAttributes <IApplyToContext>(true); if (changes.Length > 0) { command = new ApplyChangesToContextCommand(command, changes); } return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { if (test.RunState != RunState.Runnable && test.RunState != RunState.Explicit) { return(new SkipCommand(test)); } // Command to execute test TestCommand command = new TestMethodCommand(test); // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add commands from Decorators supplied by attributes foreach (ICommandDecorator decorator in test.Method.GetCustomAttributes(typeof(ICommandDecorator), true)) { command = decorator.Decorate(command); } // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) { command = new ApplyChangesToContextCommand(command, changes); } return(command); }
protected override TestCommand MakeTestCommand(ITestFilter filter) { TestCommand command = new TestMethodCommand(this); command = ApplyDecoratorsToCommand(command); return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> protected override TestCommand MakeTestCommand() { TestCommand command = new TestMethodCommand(this); command = ApplyDecoratorsToCommand(command); IApplyToContext[] changes = (IApplyToContext[])this.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) { command = new ApplyChangesToContextCommand(command, changes); } return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public virtual TestCommand MakeTestCommand() { if (RunState != RunState.Runnable && RunState != RunState.Explicit) { return(new SkipCommand(this)); } TestCommand command = new TestMethodCommand(this); command = ApplyDecoratorsToCommand(command); IApplyToContext[] changes = (IApplyToContext[])this.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) { command = new ApplyChangesToContextCommand(command, changes); } return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { if (test.RunState != RunState.Runnable && test.RunState != RunState.Explicit) { return(new SkipCommand(test)); } // Command to execute test TestCommand command = new TestMethodCommand(test); // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in test.Method.GetCustomAttributes(typeof(IWrapTestMethod), true)) { command = wrapper.Wrap(command); } // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in test.Method.GetCustomAttributes(typeof(IWrapSetUpTearDown), true)) { command = decorator.Wrap(command); } // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) { command = new ApplyChangesToContextCommand(command, changes); } return(command); }
public void Initialize() { Assembly assembly = Assembly.GetExecutingAssembly(); foreach (Type type in assembly.GetTypes()) { TestFixtureAttribute fixtureAttribute = type.GetCustomAttribute <TestFixtureAttribute>(); if (fixtureAttribute == null) { continue; } object fixtureInstance = Activator.CreateInstance(type); TestClassCommand classCommand = new TestClassCommand(fixtureInstance); foreach (MethodInfo method in type.GetMethods()) { TestAttribute testAttribute = method.GetCustomAttribute <TestAttribute>(); if (testAttribute == null) { continue; } TestMethodCommand methodCommand = new TestMethodCommand(method) { Iterations = testAttribute.Iterations, PartitionCount = testAttribute.PartitionCount }; string methodName = method.Name.Replace("Tests", string.Empty).Replace("Test", string.Empty).ToLower(); classCommand.AddTestMethodCommand(methodName, methodCommand); } testClassCommands.Add(type.Name.ToLower(), classCommand); } }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public virtual TestCommand MakeTestCommand() { TestCommand command = new TestMethodCommand(this); command = ApplyDecoratorsToCommand(command); IApplyToContext[] changes = (IApplyToContext[])Method.GetCustomAttributes(typeof(IApplyToContext), true); foreach (var change in changes) { command = new ApplyToContextCommand(command, change); } if (!Method.IsStatic) { // Try to locate the parent fixture. In current implementations, the test method // is either one or two levels below the TestFixture - if this changes, // so should the following code. TestFixture fixture = Parent as TestFixture ?? Parent?.Parent as TestFixture; command = new IsolatedFixtureCommand(command, fixture); } return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { // Command to execute test TestCommand command = new TestMethodCommand(test); // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in test.Method.GetCustomAttributes<IWrapTestMethod>(true)) command = wrapper.Wrap(command); // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in test.Method.GetCustomAttributes<IWrapSetUpTearDown>(true)) command = decorator.Wrap(command); // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = test.Method.GetCustomAttributes<IApplyToContext>(true); if (changes.Length > 0) command = new ApplyChangesToContextCommand(command, changes); return command; }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns>A TestCommand</returns> private TestCommand MakeTestCommand() { if (Test.RunState == RunState.Runnable || Test.RunState == RunState.Explicit && Filter.IsExplicitMatch(Test)) { // Command to execute test TestCommand command = new TestMethodCommand(_testMethod); var method = _testMethod.Method; // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in method.GetCustomAttributes <IWrapTestMethod>(true)) { command = wrapper.Wrap(command); } // Create TestActionCommands using attributes of the method foreach (ITestAction action in Test.Actions) { if (action.Targets == ActionTargets.Default || action.Targets.HasFlag(ActionTargets.Test)) { command = new TestActionCommand(command, action); } } ; // Try to locate the parent fixture. In current implementations, the test method // is either one or two levels levels below the TestFixture - if this changes, // so should the following code. TestFixture parentFixture = Test.Parent as TestFixture ?? Test.Parent?.Parent as TestFixture; // In normal operation we should always get the methods from the parent fixture. // However, some of NUnit's own tests can create a TestMethod without a parent // fixture. Most likely, we should stop doing this, but it affects 100s of cases. var setUpMethods = parentFixture?.SetUpMethods ?? Reflect.GetMethodsWithAttribute(Test.TypeInfo.Type, typeof(SetUpAttribute), true); var tearDownMethods = parentFixture?.TearDownMethods ?? Reflect.GetMethodsWithAttribute(Test.TypeInfo.Type, typeof(TearDownAttribute), true); // Wrap in SetUpTearDownCommands var setUpTearDownList = BuildSetUpTearDownList(setUpMethods, tearDownMethods); foreach (var item in setUpTearDownList) { command = new SetUpTearDownCommand(command, item); } // In the current implementation, upstream actions only apply to tests. If that should change in the future, // then actions would have to be tested for here. For now we simply assert it in Debug. We allow // ActionTargets.Default, because it is passed down by ParameterizedMethodSuite. int index = Context.UpstreamActions.Count; while (--index >= 0) { ITestAction action = Context.UpstreamActions[index]; System.Diagnostics.Debug.Assert( action.Targets == ActionTargets.Default || action.Targets.HasFlag(ActionTargets.Test), "Invalid target on upstream action: " + action.Targets.ToString()); command = new TestActionCommand(command, action); } // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in method.GetCustomAttributes <IWrapSetUpTearDown>(true)) { command = decorator.Wrap(command); } // Add command to set up context using attributes that implement IApplyToContext foreach (var attr in method.GetCustomAttributes <IApplyToContext>(true)) { command = new ApplyChangesToContextCommand(command, attr); } // If a timeout is specified, create a TimeoutCommand #if !NETSTANDARD1_6 // Timeout set at a higher level int timeout = Context.TestCaseTimeout; // Timeout set on this test if (Test.Properties.ContainsKey(PropertyNames.Timeout)) { timeout = (int)Test.Properties.Get(PropertyNames.Timeout); } if (timeout > 0) { command = new TimeoutCommand(command, timeout); } #endif return(command); } else { return(new SkipCommand(_testMethod)); } }
public void AddTestMethodCommand(string name, TestMethodCommand command) { command.TestClassInstance = testClassInstance; testMethodCommands.Add(name, command); }
public static TestCommand BuildTestCommand(TestMethod test, ITestFilter filter) { if (test.RunState != RunState.Runnable && !(test.RunState == RunState.Explicit && filter.IsExplicitMatch(test))) { return(new SkipCommand(test)); } var testReturnsIEnumerator = test.Method.ReturnType.Type == typeof(IEnumerator); TestCommand command; if (!testReturnsIEnumerator) { command = new TestMethodCommand(test); } else { command = new EnumerableTestMethodCommand(test); } command = new UnityLogCheckDelegatingCommand(command); foreach (var wrapper in test.Method.GetCustomAttributes <IWrapTestMethod>(true)) { command = wrapper.Wrap(command); if (command == null) { var message = String.Format("IWrapTestMethod implementation '{0}' returned null as command.", wrapper.GetType().FullName); return(new FailCommand(test, ResultState.Failure, message)); } if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand)) { command = TryReplaceWithEnumerableCommand(command); if (command != null) { continue; } var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.", wrapper.GetType().FullName, GetTestBuilderName(test)); return(new FailCommand(test, ResultState.Failure, message)); } } command = new UnityEngine.TestTools.TestActionCommand(command); command = new UnityEngine.TestTools.SetUpTearDownCommand(command); if (!testReturnsIEnumerator) { command = new ImmediateEnumerableCommand(command); } foreach (var wrapper in test.Method.GetCustomAttributes <IWrapSetUpTearDown>(true)) { command = wrapper.Wrap(command); if (command == null) { var message = String.Format("IWrapSetUpTearDown implementation '{0}' returned null as command.", wrapper.GetType().FullName); return(new FailCommand(test, ResultState.Failure, message)); } if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand)) { command = TryReplaceWithEnumerableCommand(command); if (command != null) { continue; } var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.", wrapper.GetType().FullName, GetTestBuilderName(test)); return(new FailCommand(test, ResultState.Failure, message)); } } command = new EnumerableSetUpTearDownCommand(command); command = new OuterUnityTestActionCommand(command); IApplyToContext[] changes = test.Method.GetCustomAttributes <IApplyToContext>(true); if (changes.Length > 0) { command = new EnumerableApplyChangesToContextCommand(command, changes); } return(command); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { if (test.RunState != RunState.Runnable && test.RunState != RunState.Explicit) return new SkipCommand(test); // Command to execute test TestCommand command = new TestMethodCommand(test); // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in test.Method.GetCustomAttributes(typeof(IWrapTestMethod), true)) command = wrapper.Wrap(command); // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in test.Method.GetCustomAttributes(typeof(IWrapSetUpTearDown), true)) command = decorator.Wrap(command); // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) command = new ApplyChangesToContextCommand(command, changes); return command; }