示例#1
0
        /// <summary>
        /// Runs the test, saving a TestResult in the supplied TestExecutionContext.
        /// </summary>
        /// <param name="context">The context in which the test should run.</param>
        /// <returns>A TestResult</returns>
        public override TestResult Execute(TestExecutionContext context)
        {
            if (Test.Fixture == null)
            {
                Test.Fixture = context.TestObject;
            }

            // 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.
            foreach (ITestAction action in context.UpstreamActions)
            {
                System.Diagnostics.Debug.Assert(
                    action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test,
                    "Invalid target on upstream action: " + action.Targets.ToString());

                _actions.Add(new TestActionItem(action));
            }

            foreach (ITestAction action in ActionsHelper.GetActionsFromAttributeProvider(((TestMethod)Test).Method.MethodInfo))
            {
                if (action.Targets == ActionTargets.Default || (action.Targets & ActionTargets.Test) == ActionTargets.Test)
                {
                    _actions.Add(new TestActionItem(action));
                }
            }

            try
            {
                for (int i = 0; i < _actions.Count; i++)
                {
                    _actions[i].BeforeTest(Test);
                }

                context.CurrentResult = innerCommand.Execute(context);
            }
            catch (Exception ex)
            {
#if !PORTABLE
                if (ex is ThreadAbortException)
                {
                    Thread.ResetAbort();
                }
#endif
                context.CurrentResult.RecordException(ex);
            }
            finally
            {
                if (context.ExecutionStatus != TestExecutionStatus.AbortRequested)
                {
                    for (int i = _actions.Count - 1; i >= 0; i--)
                    {
                        _actions[i].AfterTest(Test);
                    }
                }
            }

            return(context.CurrentResult);
        }
示例#2
0
        /// <summary>
        /// Initialize the TestExecutionContext. This must be done
        /// before executing the WorkItem.
        /// </summary>
        /// <remarks>
        /// Originally, the context was provided in the constructor
        /// but delaying initialization of the context until the item
        /// is about to be dispatched allows changes in the parent
        /// context during OneTimeSetUp to be reflected in the child.
        /// </remarks>
        /// <param name="context">The TestExecutionContext to use</param>
        public void InitializeContext(TestExecutionContext context)
        {
            Guard.OperationValid(Context == null, "The context has already been initialized");

            Context = context;

            if (Test is TestAssembly)
            {
                Actions.AddRange(ActionsHelper.GetActionsFromAttributeProvider(((TestAssembly)Test).Assembly));
            }
            else if (Test is ParameterizedMethodSuite)
            {
                Actions.AddRange(ActionsHelper.GetActionsFromAttributeProvider(Test.Method.MethodInfo));
            }
            else if (Test.TypeInfo != null)
            {
                Actions.AddRange(ActionsHelper.GetActionsFromTypesAttributes(Test.TypeInfo.Type));
            }
        }