Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        /// <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);
        }
Ejemplo n.º 4
0
        /// <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;
        }
Ejemplo n.º 5
0
        /// <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));
            }
        }
Ejemplo n.º 6
0
        /// <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;
        }