Example #1
0
        /// <summary>
        /// Recursively search the type hierarchy, beginning with the given type, looking for
        /// Setup and Cleanup methods for the current TestCase.
        /// </summary>
        /// <param name="test">TestCase to find setup and cleanup methods for.</param>
        /// <param name="targetType">Object type to begin search from.</param>
        private void FindSupportMethods(TestCase test, Type targetType)
        {
            if (targetType == null)
            {
                throw new ArgumentException("TargetType cannot be null, error in recursion.");
            }

            MethodInfo[] methods = targetType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (MethodInfo method in methods)
            {
                object[] attributes = method.GetCustomAttributes(typeof(TestCase_SupportMethod), true);
                if (attributes.Length > 0)
                {
                    foreach (object attribute in attributes)
                    {
                        TestCase_SupportMethod candidateAttribute = (TestCase_SupportMethod)attribute;
                        if (candidateAttribute.CaseRegex.Match(test.Id).Success)
                        {
                            // Match to SetupMethod
                            if (candidateAttribute is TestCase_Setup)
                            {
                                if (ShouldOverrideSupportMethod(test.Id, test.SetupMethod, candidateAttribute))
                                {
                                    test.SetupMethod = method;
                                }
                            }
                            // Match to CleanupMethod
                            else
                            {
                                if (ShouldOverrideSupportMethod(test.Id, test.CleanupMethod, candidateAttribute))
                                {
                                    test.CleanupMethod = method;
                                }
                            }
                        }
                    }
                }
            }

            // Stop searching once we have found both a setup and cleanup method or we reach the top of
            // our object hierarchy.
            if (!targetType.Equals(typeof(TestSuite)) && (test.SetupMethod == null || test.CleanupMethod == null))
            {
                FindSupportMethods(test, targetType.BaseType);
            }
        }
Example #2
0
 /// <summary>
 /// Determine rules for which SupportMethod should be chosen if there are multiple ones that match
 /// the current Testcase Id.
 /// Rules applied in order of priority:
 ///    1. If only method.
 ///    2. If exact match to TestId.
 ///    3. If matches but not only wildcard.
 ///    4. If wildcard only.
 /// </summary>
 /// <param name="testId">Id of test.</param>
 /// <param name="current">Method we are determing if we should override, may be null.</param>
 /// <param name="candidate">Attribute of candidate method which will be used to make the decision.</param>
 /// <returns>True if the MethodInfo matching the candidate attribute should override the current support method.</returns>
 private bool ShouldOverrideSupportMethod(string testId, MethodInfo current, TestCase_SupportMethod candidate)
 {
     return(current == null ||
            candidate.CaseRegex.ToString().Equals(testId) ||
            !candidate.CaseRegex.ToString().Equals(TestCase_SupportMethod.DEFAULT));
 }