Exemplo n.º 1
0
 private void RunSetUp()
 {
     if (setUpMethods != null)
         foreach( MethodInfo setUpMethod in setUpMethods )
             Reflect.InvokeMethod(setUpMethod, setUpMethod.IsStatic ? null : this.Fixture);
 }
 private static string GetUserMessage(object source)
 {
     return(Reflect.GetPropertyValue(source, "UserMessage") as string);
 }
Exemplo n.º 3
0
        private TestResult RunTestInContext()
        {
            TestExecutionContext.Save();

            TestExecutionContext.CurrentContext.CurrentTest = this;

            if (this.Parent != null)
            {
                this.Fixture = this.Parent.Fixture;
                TestSuite suite = this.Parent as TestSuite;
                if (suite != null)
                {
                    this.setUpMethods    = suite.SetUpMethods;
                    this.tearDownMethods = suite.TearDownMethods;
#if CLR_2_0 || CLR_4_0
                    this.suiteActions = suite.GetTestActions();
#endif
                }
            }

            try
            {
#if CLR_2_0 || CLR_4_0
                this.actions = ActionsHelper.GetActionsFromAttributeProvider(method);
#endif

                // Temporary... to allow for tests that directly execute a test case);
                if (Fixture == null && !method.IsStatic)
                {
                    Fixture = Reflect.Construct(this.FixtureType);
                }

                if (this.Properties["_SETCULTURE"] != null)
                {
                    TestExecutionContext.CurrentContext.CurrentCulture =
                        new System.Globalization.CultureInfo((string)Properties["_SETCULTURE"]);
                }
                else if (this.Properties["SetCulture"] != null) // In case we are running NUnitLite tests
                {
                    TestExecutionContext.CurrentContext.CurrentCulture =
                        new System.Globalization.CultureInfo((string)Properties["SetCulture"]);
                }

                if (this.Properties["_SETUICULTURE"] != null)
                {
                    TestExecutionContext.CurrentContext.CurrentUICulture =
                        new System.Globalization.CultureInfo((string)Properties["_SETUICULTURE"]);
                }
                if (this.Properties["SetUICulture"] != null) // In case we are running NUnitLite tests
                {
                    TestExecutionContext.CurrentContext.CurrentUICulture =
                        new System.Globalization.CultureInfo((string)Properties["SetUICulture"]);
                }

                return(RunRepeatedTest());
            }
            catch (Exception ex)
            {
                log.Debug("TestMethod: Caught " + ex.GetType().Name);

                if (ex is ThreadAbortException)
                {
                    Thread.ResetAbort();
                }

                TestResult testResult = new TestResult(this);
                RecordException(ex, testResult, FailureSite.Test);

                return(testResult);
            }
            finally
            {
                Fixture = null;

                TestExecutionContext.Restore();
            }
        }
 private static string GetHandler(object source)
 {
     return(Reflect.GetPropertyValue(source, "Handler") as string);
 }
 private static string GetExpectedMessage(object source)
 {
     return(Reflect.GetPropertyValue(source, PropertyNames.ExpectedMessage) as string);
 }
 public static bool IsSetUpMethod(MethodInfo method)
 {
     return(Reflect.HasAttribute(method, NUnitFramework.SetUpAttribute, false));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Construct from a MethodInfo
 /// </summary>
 /// <param name="method"></param>
 public ParameterizedMethodSuite(MethodInfo method)
     : base(method.ReflectedType.FullName, method.Name)
 {
     this.maintainTestOrder = true;
     this.isTheory          = Reflect.HasAttribute(method, NUnitFramework.TheoryAttribute, true);
 }
 public static string GetHandler(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "Handler") as string);
 }
 public static Type GetExceptionType(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "ExceptionType") as Type);
 }
 public static string GetIgnoreReason(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "Reason") as string);
 }
 /// <summary>
 /// Method to return the description from an attribute
 /// </summary>
 /// <param name="attribute">The attribute to check</param>
 /// <returns>The description, if any, or null</returns>
 public static string GetDescription(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "Description") as string);
 }
 public static MethodInfo GetDefaultExceptionHandler(Type fixtureType)
 {
     return(Reflect.HasInterface(fixtureType, ExpectExceptionInterface)
        ? GetExceptionHandler(fixtureType, "HandleException")
        : null);
 }
 public static MethodInfo GetFixtureTearDownMethod(Type fixtureType)
 {
     return(Reflect.GetMethodWithAttribute(fixtureType, FixtureTearDownAttribute,
                                           BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                                           true));
 }
Exemplo n.º 14
0
 public static bool IsValidType(Type type)
 {
     return(Reflect.GetSuiteProperty(type) != null);
 }
 public static bool IsTestCaseBuilder(Type type)
 {
     return(Reflect.HasAttribute(type, TestCaseBuilderAttributeName, false) &&
            Reflect.HasInterface(type, TestCaseBuilderInterfaceName));
 }
 public static string GetExceptionName(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "ExceptionName") as string);
 }
 public static bool IsTestDecorator(Type type)
 {
     return(Reflect.HasAttribute(type, TestDecoratorAttributeName, false) &&
            Reflect.HasInterface(type, TestDecoratorInterfaceName));
 }
        public static string GetMatchType(System.Attribute attribute)
        {
            object matchEnum = Reflect.GetPropertyValue(attribute, "MatchType");

            return(matchEnum != null?matchEnum.ToString() : null);
        }
 public static bool IsFixtureTearDownMethod(MethodInfo method)
 {
     return(Reflect.HasAttribute(method, NUnitFramework.FixtureTearDownAttribute, false));
 }
 public static string GetUserMessage(System.Attribute attribute)
 {
     return(Reflect.GetPropertyValue(attribute, "UserMessage") as string);
 }
 private static MethodInfo GetDefaultExceptionHandler(Type fixtureType)
 {
     return(Reflect.HasInterface(fixtureType, NUnitFramework.ExpectExceptionInterface)
         ? GetExceptionHandler(fixtureType, "HandleException")
         : null);
 }
 /// <summary>
 /// Modify a newly constructed test based on a type or method by
 /// applying any of NUnit's common attributes.
 /// </summary>
 /// <param name="member">The type or method from which the test was constructed</param>
 /// <param name="test">The test to which the attributes apply</param>
 public static void ApplyCommonAttributes(MemberInfo member, Test test)
 {
     ApplyCommonAttributes(Reflect.GetAttributes(member, false), test);
 }
 private static Type GetExceptionType(object source)
 {
     return(Reflect.GetPropertyValue(source, PropertyNames.ExpectedException) as Type);
 }
 /// <summary>
 /// Modify a newly constructed test based on an assembly by applying
 /// any of NUnit's common attributes.
 /// </summary>
 /// <param name="assembly">The assembly from which the test was constructed</param>
 /// <param name="test">The test to which the attributes apply</param>
 public static void ApplyCommonAttributes(Assembly assembly, Test test)
 {
     ApplyCommonAttributes(Reflect.GetAttributes(assembly, false), test);
 }
        private static string GetMatchType(object source)
        {
            object matchEnum = Reflect.GetPropertyValue(source, "MatchType");

            return(matchEnum != null?matchEnum.ToString() : null);
        }
        /// <summary>
        /// Modify a newly constructed test by applying any of NUnit's common
        /// attributes, based on an input array of attributes. This method checks
        /// for all attributes, relying on the fact that specific attributes can only
        /// occur on those constructs on which they are allowed.
        /// </summary>
        /// <param name="attributes">An array of attributes possibly including NUnit attributes
        /// <param name="test">The test to which the attributes apply</param>
        public static void ApplyCommonAttributes(Attribute[] attributes, Test test)
        {
            IList          categories = new ArrayList();
            ListDictionary properties = new ListDictionary();

            foreach (Attribute attribute in attributes)
            {
                Type   attributeType = attribute.GetType();
                string attributeName = attributeType.FullName;
                bool   isValid       = test.RunState != RunState.NotRunnable;

                switch (attributeName)
                {
                case TestFixtureAttribute:
                case TestAttribute:
                    if (test.Description == null)
                    {
                        test.Description = GetDescription(attribute);
                    }
                    break;

                case DescriptionAttribute:
                    test.Description = GetDescription(attribute);
                    break;

                case ExplicitAttribute:
                    if (isValid)
                    {
                        test.RunState     = RunState.Explicit;
                        test.IgnoreReason = GetIgnoreReason(attribute);
                    }
                    break;

                case IgnoreAttribute:
                    if (isValid)
                    {
                        test.RunState     = RunState.Ignored;
                        test.IgnoreReason = GetIgnoreReason(attribute);
                    }
                    break;

                case PlatformAttribute:
                    PlatformHelper pHelper = new PlatformHelper();
                    if (isValid && !pHelper.IsPlatformSupported(attribute))
                    {
                        test.RunState     = RunState.Skipped;
                        test.IgnoreReason = GetIgnoreReason(attribute);
                        if (test.IgnoreReason == null)
                        {
                            test.IgnoreReason = pHelper.Reason;
                        }
                    }
                    break;

                case CultureAttribute:
                    CultureDetector cultureDetector = new CultureDetector();
                    if (isValid && !cultureDetector.IsCultureSupported(attribute))
                    {
                        test.RunState     = RunState.Skipped;
                        test.IgnoreReason = cultureDetector.Reason;
                    }
                    break;

                default:
                    if (Reflect.InheritsFrom(attributeType, CategoryAttribute))
                    {
                        categories.Add(Reflect.GetPropertyValue(attribute, "Name"));
                    }
                    else if (Reflect.InheritsFrom(attributeType, PropertyAttribute))
                    {
                        string name = (string)Reflect.GetPropertyValue(attribute, "Name");
                        if (name != null && name != string.Empty)
                        {
                            object val = Reflect.GetPropertyValue(attribute, "Value");
                            properties[name] = val;
                        }
                    }
                    break;
                }
            }

            test.Categories = categories;
            test.Properties = properties;
        }
Exemplo n.º 27
0
        public static void CheckAttributes(ICustomAttributeProvider provider, string location)
        {
            var methodInfo    = provider as MethodInfo;
            var parameterInfo = provider as ParameterInfo;

            foreach (Attribute attribute in provider.GetCustomAttributes(true))
            {
                Type   attributeType     = attribute.GetType();
                string attributeName     = attributeType.Name;
                string attributeFullName = attributeType.FullName;

                switch (attributeFullName)
                {
                case "NUnit.Framework.ExpectedExceptionAttribute":
                    Error(location, "ExpectedExceptionAttribute is not supported in NUnit 3. Use Assert.Throws or Throws.InstanceOf.");
                    break;

                case "NUnit.Framework.IgnoreAttribute":
                    var reason = (string)Reflect.GetPropertyValue(attribute, "Reason");
                    if (string.IsNullOrEmpty(reason))
                    {
                        Error(location, "IgnoreAttribute must have a reason specified in NUnit 3.");
                    }
                    break;

                case "NUnit.Framework.RequiresSTAAttribute":
                case "NUnit.Framework.RequiresMTAAttribute":
                    Error(location, attributeName + " is not supported in NUnit 3. Use ApartmentAttribute.");
                    break;

                case "NUnit.Core.Extensibility.NUnitAddinAttribute":
                case "NUnit.Framework.RequiredAddinAttribute":
                    Error(location, attributeName + " is not available in NUnit 3, which no longer supports Addins. After conversion, you can create custom attributes or engine extensions instead.");
                    break;

                case "NUnit.Framework.SuiteAttribute":
                    Error(location, "SuiteAttribute is not supported in NUnit 3. You should restructure your tests to eliminate legacy Suites.");
                    break;

                case "NUnit.Framework.SetUpAttribute":
                case "NUnit.Framework.TearDownAttribute":
                    if (methodInfo != null && methodInfo.ReflectedType != null &&
                        Reflect.HasAttribute(methodInfo.ReflectedType, "NUnit.Framework.SetUpFixtureAttribute", true))
                    {
                        var replacement = "OneTime" + attributeName;
                        Error(location, attributeName + " is no longer allowed in a SetUpFixture in NUnit 3. Use " + replacement + ".");
                    }
                    break;

                case "NUnit.Framework.TestCaseAttribute":
                    string expectedExceptionName = (string)Reflect.GetPropertyValue(attribute, "ExpectedExceptionName");
                    if (!string.IsNullOrEmpty(expectedExceptionName))
                    {
                        Error(location, "TestCaseAttribute does not support ExpectedException in NUnit 3. Use Assert.Throws or Throws.InstanceOf.");
                    }
                    bool legacyResultUsed = (bool)Reflect.GetPropertyValue(attribute, "LegacyResultUsed", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (legacyResultUsed)
                    {
                        Error(location, "TestCaseAttribute no longer supports Result property in NUnit 3. Use ExpectedResult.");
                    }
                    bool ignoreUsed = (bool)Reflect.GetPropertyValue(attribute, "Ignore");
                    if (ignoreUsed)
                    {
                        Error(location, "TestCaseAttribute Ignore property changes from bool to string in NUnit 3. Fix after conversion.");
                    }
                    break;

                case "NUnit.Framework.TestCaseSourceAttribute":
                    string sourceName = (string)Reflect.GetPropertyValue(attribute, "SourceName");
                    if (!string.IsNullOrEmpty(sourceName))
                    {
                        Type sourceType = (Type)Reflect.GetPropertyValue(attribute, "SourceType");
                        if (sourceType == null && methodInfo != null)
                        {
                            sourceType = methodInfo.ReflectedType;
                        }
                        if (sourceType != null)
                        {
                            var members = sourceType.GetMember(sourceName, ALL_MEMBERS);
                            if (members.Length > 0 && !IsStaticMember(members[0]))
                            {
                                Error(sourceType + "." + members[0].Name, "TestCaseSourceAttribute must reference a static member.");
                            }
                        }
                    }
                    break;

                case "NUnit.Framework.TestFixtureAttribute":
                    bool ignore = (bool)Reflect.GetPropertyValue(attribute, "Ignore");
                    if (ignore)
                    {
                        Error(location, "TestFixtureAttribute Ignore property changes from bool to string in NUnit 3. Fix after conversion.");
                    }
                    break;

                case "NUnit.Framework.TestFixtureSetUpAttribute":
                    Error(location, "TestFixtureSetUpAttribute is not supported in NUnit 3. Use OneTimeSetUpAttribute.");
                    break;

                case "NUnit.Framework.TestFixtureTearDownAttribute":
                    Error(location, "TestFixtureTearDownAttribute is not supported in NUnit 3. Use OneTimeTearDownAttribute.");
                    break;

                case "NUnit.Framework.ValueSourceAttribute":
                    sourceName = (string)Reflect.GetPropertyValue(attribute, "SourceName");
                    if (!string.IsNullOrEmpty(sourceName))
                    {
                        Type sourceType = (Type)Reflect.GetPropertyValue(attribute, "SourceType");
                        if (sourceType == null && parameterInfo != null)
                        {
                            sourceType = parameterInfo.Member.ReflectedType;
                        }
                        if (sourceType != null)
                        {
                            var members = sourceType.GetMember(sourceName, ALL_MEMBERS);
                            if (members.Length > 0 && !IsStaticMember(members[0]))
                            {
                                Error(sourceType + "." + members[0].Name, "ValueSourceAttribute must reference a static member.");
                            }
                        }
                    }
                    break;

                case "System.STAThreadAttribute":
                case "System.MTAThreadAttribute":
                    Warning(location, attributeFullName + " has no effect in NUnit 3. Use ApartmentAttribute.");
                    break;
                }
            }
        }
 public static bool IsSuiteBuilder(Type type)
 {
     return(Reflect.HasAttribute(type, SuiteBuilderAttribute, false) &&
            Reflect.HasInterface(type, SuiteBuilderInterface));
 }
Exemplo n.º 29
0
        protected virtual object RunTestMethod()
        {
            object fixture = this.method.IsStatic ? null : this.Fixture;

            return(Reflect.InvokeMethod(this.method, fixture, this.arguments));
        }
Exemplo n.º 30
0
 public static bool IsValidType(Type type)
 {
     return(!type.IsAbstract && Reflect.HasTestFixtureAttribute(type));
 }