示例#1
0
        /// <summary>
        /// Handles the construction and disposement of a fixture per test case
        /// </summary>
        /// <param name="innerCommand">The inner command to which the command applies</param>
        public FixturePerTestCaseCommand(TestCommand innerCommand)
            : base(innerCommand)
        {
            TestFixture testFixture = null;

            ITest currentTest = Test;

            while (currentTest != null && testFixture == null)
            {
                testFixture = currentTest as TestFixture;
                currentTest = currentTest.Parent;
            }

            Guard.ArgumentValid(testFixture != null, "FixturePerTestCaseCommand must reference a TestFixture", nameof(innerCommand));

            ITypeInfo typeInfo = testFixture.TypeInfo;

            BeforeTest = (context) =>
            {
                if (typeInfo != null && !typeInfo.IsStaticClass)
                {
                    context.TestObject = typeInfo.Construct(testFixture.Arguments);
                    Test.Fixture       = context.TestObject;
                }
            };
        }
        /// <summary>
        /// Overridden to run the one-time setup for a suite.
        /// </summary>
        /// <param name="context">The TestExecutionContext to be used.</param>
        /// <returns>A TestResult</returns>
        public override TestResult Execute(TestExecutionContext context)
        {
            if (_typeInfo != null)
            {
                // Use pre-constructed fixture if available, otherwise construct it
                if (!_typeInfo.IsStaticClass)
                {
                    context.TestObject = _suite.Fixture ?? _typeInfo.Construct(_arguments);
                    if (_suite.Fixture == null)
                    {
                        _suite.Fixture = context.TestObject;
                    }
                    Test.Fixture = _suite.Fixture;
                }

                for (int i = _setUpTearDown.Count; i > 0;)
                {
                    _setUpTearDown[--i].RunSetUp(context);
                }
            }


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

            return(context.CurrentResult);
        }
        /// <summary>
        /// Constructs a OneTimeSetUpCommand for a suite
        /// </summary>
        /// <param name="innerCommand">The inner command to which the command applies</param>
        public ConstructFixtureCommand(TestCommand innerCommand)
            : base(innerCommand)
        {
            Guard.ArgumentValid(Test is TestSuite, "ConstructFixtureCommand must reference a TestSuite", "innerCommand");

            BeforeTest = (context) =>
            {
                ITypeInfo typeInfo = Test.TypeInfo;

                if (typeInfo != null)
                {
                    // Use pre-constructed fixture if available, otherwise construct it
                    if (!typeInfo.IsStaticClass)
                    {
                        context.TestObject = Test.Fixture ?? typeInfo.Construct(((TestSuite)Test).Arguments);
                        if (Test.Fixture == null)
                        {
                            Test.Fixture = context.TestObject;
                        }
                    }
                }
            };
        }
 public object Construct(object[] args)
 {
     return(_baseInfo.Construct(args));
 }