Example #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);
        }
Example #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);
        }
Example #3
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDown, List<TestActionItem> actions)
        {
            // Handle skipped tests
            if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
                return MakeSkipCommand(suite);

            // Build the OneTimeSetUpCommand itself
            TestCommand command = new OneTimeSetUpCommand(suite, setUpTearDown, actions);

            // Prefix with any IApplyToContext items from attributes
            IList<IApplyToContext> changes = null;

            if (suite.TypeInfo != null)
                changes = suite.TypeInfo.GetCustomAttributes<IApplyToContext>(true);
            else if (suite.Method != null)
                changes = suite.Method.GetCustomAttributes<IApplyToContext>(true);
            else
            {
                var testAssembly = suite as TestAssembly;
                if (testAssembly != null)
#if PORTABLE
                    changes = new List<IApplyToContext>(testAssembly.Assembly.GetAttributes<IApplyToContext>());
#else
                    changes = (IApplyToContext[])testAssembly.Assembly.GetCustomAttributes(typeof(IApplyToContext), true);
#endif
            }

            if (changes != null && changes.Count > 0)
                command = new ApplyChangesToContextCommand(command, changes);

            return command;
        }
Example #4
0
        public void ShouldPassAfter2RepeatsAndTimeoutIsResetEachTime()
        {
            // Rather than testing with sleeps, this tests that the execution will occur in the correct
            // order by checking which commands are run when. As the repeat command comes first, the
            // timeout will be reset each time it runs
            var            test    = TestBuilder.MakeTestFromMethod(typeof(HelperMethodForTimeoutsClass), nameof(HelperMethodForTimeoutsClass.ShouldPassAfter2RepeatsAndTimeoutIsResetEachTime));
            SimpleWorkItem work    = TestBuilder.CreateWorkItem(test) as SimpleWorkItem;
            var            method  = typeof(SimpleWorkItem).GetMethod("MakeTestCommand", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            TestCommand    command = (TestCommand)method.Invoke(work, null);

            Assert.That(command, Is.TypeOf(typeof(RepeatAttribute.RepeatedTestCommand)));
            RepeatAttribute.RepeatedTestCommand repeatedCommand = (RepeatAttribute.RepeatedTestCommand)command;

            command = GetInnerCommand(repeatedCommand);

            Assert.That(command, Is.TypeOf(typeof(TimeoutCommand)));
            TimeoutCommand timeoutCommand = (TimeoutCommand)command;

            command = GetInnerCommand(timeoutCommand);

            Assert.That(command, Is.TypeOf(typeof(ApplyChangesToContextCommand)));
            ApplyChangesToContextCommand applyChangesToContextCommand = (ApplyChangesToContextCommand)command;

            command = GetInnerCommand(applyChangesToContextCommand);

            Assert.That(command, Is.TypeOf(typeof(TestMethodCommand)));
        }
Example #5
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List<SetUpTearDownItem> setUpTearDown, List<TestActionItem> actions)
        {
            // Handle skipped tests
            if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
                return MakeSkipCommand(suite);

            // Build the OneTimeSetUpCommand itself
            TestCommand command = new OneTimeSetUpCommand(suite, setUpTearDown, actions);

            // Prefix with any IApplyToContext items from attributes
            if (suite.TypeInfo != null)
            {
                IApplyToContext[] changes = suite.TypeInfo.GetCustomAttributes<IApplyToContext>(true);
                if (changes.Length > 0)
                    command = new ApplyChangesToContextCommand(command, changes);
            }
            if (suite.Method!=null)
            {
                IApplyToContext[] changes = suite.Method.GetCustomAttributes<IApplyToContext>(true);
                if (changes.Length > 0)
                    command = new ApplyChangesToContextCommand(command, changes);
            }

            return command;
        }
Example #6
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List <SetUpTearDownItem> setUpTearDown, List <TestActionItem> actions)
        {
            // Handle skipped tests
            if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
            {
                return(MakeSkipCommand(suite));
            }

            // Build the OneTimeSetUpCommand itself
            TestCommand command = new OneTimeSetUpCommand(suite, setUpTearDown, actions);

            // Prefix with any IApplyToContext items from attributes
            if (suite.TypeInfo != null)
            {
                IApplyToContext[] changes = suite.TypeInfo.GetCustomAttributes <IApplyToContext>(true);
                if (changes.Length > 0)
                {
                    command = new ApplyChangesToContextCommand(command, changes);
                }
            }
            if (suite.Method != null)
            {
                IApplyToContext[] changes = suite.Method.GetCustomAttributes <IApplyToContext>(true);
                if (changes.Length > 0)
                {
                    command = new ApplyChangesToContextCommand(command, changes);
                }
            }

            return(command);
        }
        private TestCommand MakeOneTimeSetUpCommand(List <SetUpTearDownItem> setUpTearDown, List <TestActionItem> actions)
        {
            TestCommand command = new EmptyTestCommand(Test);

            // Add Action Commands
            int index = actions.Count;

            while (--index >= 0)
            {
                command = new BeforeTestActionCommand(command, actions[index]);
            }

            if (Test.TypeInfo != null)
            {
                // Build the OneTimeSetUpCommands
                foreach (SetUpTearDownItem item in setUpTearDown)
                {
                    command = new OneTimeSetUpCommand(command, item);
                }

                // Construct the fixture if necessary
                if (!Test.TypeInfo.IsStaticClass)
                {
                    command = new ConstructFixtureCommand(command);
                }
            }

            // Prefix with any IApplyToContext items from attributes
            foreach (var attr in Test.GetCustomAttributes <IApplyToContext>(true))
            {
                command = new ApplyChangesToContextCommand(command, attr);
            }

            return(command);
        }
Example #8
0
        /// <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);
        }
Example #9
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public virtual TestCommand GetOneTimeSetUpCommand()
        {
            TestCommand command = new OneTimeSetUpCommand(this);

            if (this.FixtureType != null)
            {
                IApplyToContext[] changes = (IApplyToContext[])this.FixtureType.GetCustomAttributes(typeof(IApplyToContext), true);
                if (changes.Length > 0)
                {
                    command = new ApplyChangesToContextCommand(command, changes);
                }
            }

            return(command);
        }
Example #10
0
        /// <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);
        }
Example #11
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public static TestCommand MakeOneTimeSetUpCommand(TestSuite suite, List <SetUpTearDownItem> setUpTearDown, List <TestActionItem> actions)
        {
            // Handle skipped tests
            if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
            {
                return(MakeSkipCommand(suite));
            }

            // Build the OneTimeSetUpCommand itself
            TestCommand command = new OneTimeSetUpCommand(suite, setUpTearDown, actions);

            // Prefix with any IApplyToContext items from attributes
            IList <IApplyToContext> changes = null;

            if (suite.TypeInfo != null)
            {
                changes = (IApplyToContext[])suite.TypeInfo.GetCustomAttributes <IApplyToContext>(true);
            }
            else if (suite.Method != null)
            {
                changes = (IApplyToContext[])suite.Method.GetCustomAttributes <IApplyToContext>(true);
            }
            else
            {
                var testAssembly = suite as TestAssembly;
                if (testAssembly != null)
#if PORTABLE
                {   //changes = new List<IApplyToContext>(testAssembly.Assembly.GetAttributes<IApplyToContext>());
                    changes = new List <IApplyToContext>();
                }
#else
                { changes = (IApplyToContext[])testAssembly.Assembly.GetCustomAttributes(typeof(IApplyToContext), true); }
#endif
            }

            if (changes != null && changes.Count > 0)
            {
                command = new ApplyChangesToContextCommand(command, changes);
            }

            return(command);
        }
Example #12
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        private static TestCommand MakeSetUpCommand(TestSuite suite, SetUpTearDownList setUpTearDown)
        {
            if (suite.RunState != RunState.Runnable && suite.RunState != RunState.Explicit)
            {
                return(new SkipCommand(suite));
            }

            TestCommand command = new OneTimeSetUpCommand(suite, setUpTearDown);

            if (suite.FixtureType != null)
            {
                IApplyToContext[] changes = (IApplyToContext[])suite.FixtureType.GetCustomAttributes(typeof(IApplyToContext), true);
                if (changes.Length > 0)
                {
                    command = new ApplyChangesToContextCommand(command, changes);
                }
            }

            return(command);
        }
Example #13
0
        /// <summary>
        /// Gets the command to be executed before any of
        /// the child tests are run.
        /// </summary>
        /// <returns>A TestCommand</returns>
        public virtual TestCommand GetOneTimeSetUpCommand()
        {
            if (RunState != RunState.Runnable && RunState != RunState.Explicit)
            {
                return(new SkipCommand(this));
            }

            TestCommand command = new OneTimeSetUpCommand(this);

            if (this.FixtureType != null)
            {
                IApplyToContext[] changes = (IApplyToContext[])this.FixtureType.GetCustomAttributes(typeof(IApplyToContext), true);
                if (changes.Length > 0)
                {
                    command = new ApplyChangesToContextCommand(command, changes);
                }
            }

            return(command);
        }
Example #14
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);
        }
Example #15
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;
        }
Example #16
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));
            }
        }
Example #17
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;
        }