Example #1
0
        public static TestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms)
        {
            TestMethod testMethod = new TestMethod(method, parentSuite);
            string     fullName   = method.ReflectedType.FullName;

            if (parentSuite != null)
            {
                fullName = parentSuite.FullName;
            }
            if (CheckTestMethodSignature(testMethod, parms))
            {
                if (parms == null)
                {
                    testMethod.ApplyCommonAttributes(method);
                }
                object[] customAttributes = method.GetCustomAttributes(typeof(ICommandDecorator), inherit: true);
                for (int i = 0; i < customAttributes.Length; i++)
                {
                    ICommandDecorator item = (ICommandDecorator)customAttributes[i];
                    testMethod.CustomDecorators.Add(item);
                }
                ExpectedExceptionAttribute[] array = (ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), inherit: false);
                if (array.Length > 0)
                {
                    ExpectedExceptionAttribute expectedExceptionAttribute = array[0];
                    string handler = expectedExceptionAttribute.Handler;
                    if (handler != null && GetExceptionHandler(testMethod.FixtureType, handler) == null)
                    {
                        MarkAsNotRunnable(testMethod, $"The specified exception handler {handler} was not found");
                    }
                    testMethod.CustomDecorators.Add(new ExpectedExceptionDecorator(expectedExceptionAttribute.ExceptionData));
                }
            }
            if (parms != null)
            {
                method = testMethod.Method;
                if (parms.TestName != null)
                {
                    testMethod.Name     = parms.TestName;
                    testMethod.FullName = fullName + "." + parms.TestName;
                }
                else if (parms.OriginalArguments != null)
                {
                    string text = (testMethod.Name = MethodHelper.GetDisplayName(method, parms.OriginalArguments));
                    testMethod.FullName = fullName + "." + text;
                }
                parms.ApplyToTest(testMethod);
            }
            return(testMethod);
        }
        /// <summary>
        /// Builds a single NUnitTestMethod, either as a child of the fixture
        /// or as one of a set of test cases under a ParameterizedTestMethodSuite.
        /// </summary>
        /// <param name="method">The MethodInfo from which to construct the TestMethod</param>
        /// <param name="parentSuite">The suite or fixture to which the new test will be added</param>
        /// <param name="parms">The ParameterSet to be used, or null</param>
        /// <returns></returns>
        public static TestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms)
        {
            TestMethod testMethod = new TestMethod(method, parentSuite);

            string prefix = method.ReflectedType.FullName;

            // Needed to give proper fullname to test in a parameterized fixture.
            // Without this, the arguments to the fixture are not included.
            if (parentSuite != null)
            {
                prefix = parentSuite.FullName;
                //testMethod.FullName = prefix + "." + testMethod.Name;
            }

            if (CheckTestMethodSignature(testMethod, parms))
            {
                if (parms == null)
                {
                    testMethod.ApplyCommonAttributes(method);
                }

                foreach (ICommandDecorator decorator in method.GetCustomAttributes(typeof(ICommandDecorator), true))
                {
                    testMethod.CustomDecorators.Add(decorator);
                }

                ExpectedExceptionAttribute[] attributes =
                    (ExpectedExceptionAttribute[])method.GetCustomAttributes(typeof(ExpectedExceptionAttribute), false);

                if (attributes.Length > 0)
                {
                    ExpectedExceptionAttribute attr = attributes[0];
                    string handlerName = attr.Handler;
                    if (handlerName != null && GetExceptionHandler(testMethod.FixtureType, handlerName) == null)
                    {
                        MarkAsNotRunnable(
                            testMethod,
                            string.Format("The specified exception handler {0} was not found", handlerName));
                    }

                    testMethod.CustomDecorators.Add(new ExpectedExceptionDecorator(attr.ExceptionData));
                }
            }

            if (parms != null)
            {
                // NOTE: After the call to CheckTestMethodSignature, the Method
                // property of testMethod may no longer be the same as the
                // original MethodInfo, so we reassign it here.
                method = testMethod.Method;

                if (parms.TestName != null)
                {
                    testMethod.Name     = parms.TestName;
                    testMethod.FullName = prefix + "." + parms.TestName;
                }
                else if (parms.OriginalArguments != null)
                {
                    string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments);
                    testMethod.Name     = name;
                    testMethod.FullName = prefix + "." + name;
                }

                parms.ApplyToTest(testMethod);
            }

            return(testMethod);
        }