private Test BuildSingleFixture(Type type, Attribute attr)
        {
            object[] arguments = null;

            if (attr != null)
            {
                arguments = (object[])Reflect.GetPropertyValue(attr, "Arguments");
#if NET_2_0
                if (type.ContainsGenericParameters)
                {
                    Type[] typeArgs = (Type[])Reflect.GetPropertyValue(attr, "TypeArgs");
                    if (typeArgs.Length > 0 ||
                        TypeHelper.CanDeduceTypeArgsFromArgs(type, arguments, ref typeArgs))
                    {
                        type = TypeHelper.MakeGenericType(type, typeArgs);
                    }
                }
#endif
            }

            this.fixture = new NUnitTestFixture(type, arguments);
            CheckTestFixtureIsValid(fixture);

            NUnitFramework.ApplyCommonAttributes(type, fixture);

            AddTestCases(type);

            if (this.fixture.RunState != RunState.NotRunnable && this.fixture.TestCount == 0)
            {
                this.fixture.RunState     = RunState.NotRunnable;
                this.fixture.IgnoreReason = fixture.TestName.Name + " does not have any tests";
            }

            return(this.fixture);
        }
Ejemplo n.º 2
0
        ////private MethodInfo _methodinfo;

        /// <summary>
        /// Constructor to map method with this class.
        /// </summary>
        /// <param name="method">Method in test fixture without parameters.</param>
        public WalmartTestMethod(MethodInfo method)
            : base(method)
        {
            NUnitFramework.ApplyCommonAttributes(method, this);
            NUnitFramework.ApplyExpectedExceptionAttribute(method, this);

            var categories = new List <string>();
            var className  = method.DeclaringType;

            categories.ForEach(c => Categories.Add(c));

            var notReadyAttr = method.GetCustomAttributes(typeof(NotReadyAttribute), true);

            if (notReadyAttr.Any())
            {
                Categories.Add("NotReady");
            }

            ////Add EUAT as Category
            var regressionAttr = method.GetCustomAttributes(typeof(RegressionAttribute), true);

            if (regressionAttr.Any())
            {
                Categories.Add("Regression");
            }

            ////Add ToFix as Category
            var tofixAttr = method.GetCustomAttributes(typeof(ToFixAttribute), true);

            if (tofixAttr.Any())
            {
                Categories.Add("ToFix");
            }
        }
        private bool IsValidFixtureType(Type type, ref string reason)
        {
            if (type.IsAbstract)
            {
                reason = string.Format("{0} is an abstract class", type.FullName);
                return(false);
            }

            if (Reflect.GetConstructor(type) == null)
            {
                reason = string.Format("{0} does not have a valid constructor", type.FullName);
                return(false);
            }

            if (!NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.SetUpAttribute, ref reason) ||
                !NUnitFramework.CheckSetUpTearDownMethods(type, NUnitFramework.TearDownAttribute, ref reason))
            {
                return(false);
            }

            if (Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureSetUpAttribute, true))
            {
                reason = "TestFixtureSetUp method not allowed on a SetUpFixture";
                return(false);
            }

            if (Reflect.HasMethodWithAttribute(type, NUnitFramework.FixtureTearDownAttribute, true))
            {
                reason = "TestFixtureTearDown method not allowed on a SetUpFixture";
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes an instance of <see cref="InconclusiveTestCase"/>.
        /// </summary>
        /// <param name="testCase">Original test case.</param>
        public InconclusiveTestCase(NUnitTestMethod testCase)
            : base(testCase.Method)
        {
            NUnitFramework.ApplyCommonAttributes(testCase.Method, this);
            NUnitFramework.ApplyExpectedExceptionAttribute(testCase.Method, this);

            // Copy all the attributes of the original test
            this.BuilderException   = testCase.BuilderException;
            this.Categories         = testCase.Categories;
            this.Description        = testCase.Description;
            this.ExceptionProcessor = testCase.ExceptionProcessor;
            this.Fixture            = testCase.Fixture;
            this.IgnoreReason       = testCase.IgnoreReason;
            this.Parent             = testCase.Parent;
            this.Properties         = testCase.Properties;
            this.RunState           = testCase.RunState;
            this.TestName.Name      = testCase.TestName.Name;
            this.TestName.FullName  = testCase.TestName.FullName;
            this.TestName.RunnerID  = testCase.TestName.RunnerID;
            this.TestName.TestID    = testCase.TestName.TestID;

            this.setUpMethods    = GetFieldValue <MethodInfo[]>(typeof(TestMethod), testCase, "setUpMethods");
            this.tearDownMethods = GetFieldValue <MethodInfo[]>(typeof(TestMethod), testCase, "tearDownMethods");
            this.actions         = GetFieldValue <TestAction[]>(typeof(TestMethod), testCase, "actions");
            this.suiteActions    = GetFieldValue <TestAction[]>(typeof(TestMethod), testCase, "suiteActions");

            this.SetFieldValue("arguments", GetFieldValue <object[]>(typeof(TestMethod), testCase, "arguments"));
            this.SetFieldValue("expectedResult", GetFieldValue <object>(typeof(TestMethod), testCase, "expectedResult"));
            this.SetFieldValue("hasExpectedResult", GetFieldValue <bool>(typeof(TestMethod), testCase, "hasExpectedResult"));
        }
        protected void InspectTests(ITest test, List <string> methodNames, HashSet <string> categoryNames)
        {
            foreach (ITest t in test.Tests)
            {
                if (t is NUnitTestMethod)
                {
                    methodNames.Add(t.TestName.FullName);
                }

                if (t.Categories != null)
                {
                    foreach (string category in t.Categories)
                    {
                        if (NUnitFramework.IsValidCategoryName(category) && !categoryNames.Contains(category))
                        {
                            categoryNames.Add(category);
                        }
                    }
                }

                if (t.Tests != null)
                {
                    InspectTests(t, methodNames, categoryNames);
                }
            }
        }
        /// <summary>
        /// Check that the fixture type is valid. This method ensures that
        /// the type is not abstract and that there is no more than one of
        /// each setup or teardown method and that their signatures are correct.
        /// </summary>
        /// <param name="fixtureType">The type of the fixture to check</param>
        /// <param name="reason">A message indicating why the fixture is invalid</param>
        /// <returns>True if the fixture is valid, false if not</returns>
        private bool IsValidFixtureType(Type fixtureType, ref string reason)
        {
            //if (fixtureType.IsAbstract && !fixtureType.IsSealed)
            //{
            //    reason = string.Format("{0} is an abstract class", fixtureType.FullName);
            //    return false;
            //}

#if CLR_2_0 || CLR_4_0
            if (fixtureType.ContainsGenericParameters)
            {
                reason = "Fixture type contains generic parameters. You must either provide "
                         + "Type arguments or specify constructor arguments that allow NUnit "
                         + "to deduce the Type arguments.";
                return(false);
            }
#endif

            return(NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.SetUpAttribute, ref reason) &&
                   NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.TearDownAttribute, ref reason) &&
                   NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.TestFixtureSetUpAttribute, ref reason) &&
                   NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.OneTimeSetUpAttribute, ref reason) &&
                   NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.TestFixtureTearDownAttribute, ref reason) &&
                   NUnitFramework.CheckSetUpTearDownMethods(fixtureType, NUnitFramework.OneTimeTearDownAttribute, ref reason));
        }
        public static TestSuite CreateTestSuite(MethodInfo method)
        {
            var suite = new DecompilationTestSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, suite);
            PopulateTestSuite(method, suite);
            return(suite);
        }
Ejemplo n.º 8
0
        public Test BuildFrom(Type type)
        {
            var testFixture = new ConcordionTestFixture(type);

            testFixture.Add(new ConcordionTest(type));
            NUnitFramework.ApplyCommonAttributes(type, testFixture);
            return(testFixture);
        }
Ejemplo n.º 9
0
        public Test BuildFrom(Type type)
        {
            var testSuite = new TestSuite(type.FullName);

            testSuite.Add(new ConcordionTest(type));
            NUnitFramework.ApplyCommonAttributes(type, testSuite);
            return(testSuite);
        }
Ejemplo n.º 10
0
        private static void HandleException(Exception e, TestResult testResult)
        {
            if (e is NUnitException)
            {
                e = e.InnerException;
            }

            testResult.SetResult(NUnitFramework.GetResultState(e), e);
        }
Ejemplo n.º 11
0
        public Test BuildFrom(Type type)
        {
            NativeTestFactory nativeTestFactory = new NativeTestFactory();

            SpecificationBuilder.BuildTestFixture(type, nativeTestFactory, new GlobalSetupOwner());

            var result = nativeTestFactory.RootTest.GetNative() as Test;

            NUnitFramework.ApplyCommonAttributes(type.GetCustomAttributes(false).Cast <Attribute>().ToArray(), result);

            return(result);
        }
Ejemplo n.º 12
0
        public RowTestSuite CreateRowTestSuite(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            RowTestSuite testSuite = new RowTestSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, testSuite);

            return(testSuite);
        }
Ejemplo n.º 13
0
 public void AddCategories(ITest test)
 {
     if (test.Categories != null)
     {
         foreach (string name in test.Categories)
         {
             if (NUnitFramework.IsValidCategoryName(name))
             {
                 Add(name);
             }
         }
     }
 }
Ejemplo n.º 14
0
        public static CecilTestSuite CreateTestSuite(MethodInfo method)
        {
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            var suite = new CecilTestSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, suite);
            PopulateTestSuite(method, suite);

            return(suite);
        }
Ejemplo n.º 15
0
        private Test BuildSingleFixture(Type type, Attribute attr)
        {
            object[] arguments  = null;
            IList    categories = null;

            if (attr != null)
            {
                arguments = (object[])Reflect.GetPropertyValue(attr, "Arguments");

                categories = Reflect.GetPropertyValue(attr, "Categories") as IList;
#if CLR_2_0 || CLR_4_0
                if (type.ContainsGenericParameters)
                {
                    Type[] typeArgs = (Type[])Reflect.GetPropertyValue(attr, "TypeArgs");
                    if (typeArgs.Length > 0 ||
                        TypeHelper.CanDeduceTypeArgsFromArgs(type, arguments, ref typeArgs))
                    {
                        type = TypeHelper.MakeGenericType(type, typeArgs);
                    }
                }
#endif
            }

            this.fixture = new NUnitTestFixture(type, arguments);
            CheckTestFixtureIsValid(fixture);

            NUnitFramework.ApplyCommonAttributes(type, fixture);

            if (categories != null)
            {
                foreach (string category in categories)
                {
                    fixture.Categories.Add(category);
                }
            }

            if (fixture.RunState == RunState.Runnable && attr != null)
            {
                object objIgnore = Reflect.GetPropertyValue(attr, "Ignore");
                if (objIgnore != null && (bool)objIgnore == true)
                {
                    fixture.RunState     = RunState.Ignored;
                    fixture.IgnoreReason = (string)Reflect.GetPropertyValue(attr, "IgnoreReason");
                }
            }

            AddTestCases(type);

            return(this.fixture);
        }
        /// <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="parms">The ParameterSet to be used, or null</param>
        /// <returns></returns>
        public static NUnitTestMethod BuildSingleTestMethod(MethodInfo method, ParameterSet parms)
        {
            NUnitTestMethod testMethod = new NUnitTestMethod(method);

            if (CheckTestMethodSignature(testMethod, parms))
            {
                NUnitFramework.ApplyCommonAttributes(method, testMethod);
                NUnitFramework.ApplyExpectedExceptionAttribute(method, testMethod);
            }

            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.TestName.Name     = parms.TestName;
                    testMethod.TestName.FullName = method.ReflectedType.FullName + "." + parms.TestName;
                }
                else if (parms.Arguments != null)
                {
                    string name = MethodHelper.GetDisplayName(method, parms.Arguments);
                    testMethod.TestName.Name     = name;
                    testMethod.TestName.FullName = method.ReflectedType.FullName + "." + name;
                }

                if (parms.ExpectedExceptionName != null)
                {
                    testMethod.exceptionProcessor = new ExpectedExceptionProcessor(testMethod, parms);
                }

                foreach (string key in parms.Properties.Keys)
                {
                    testMethod.Properties.Add(key, parms.Properties[key]);
                }

                // Description is stored in parms.Properties
                if (parms.Description != null)
                {
                    testMethod.Description = parms.Description;
                }
            }

            return(testMethod);
        }
Ejemplo n.º 17
0
        private TestSuite BuildTestAssembly(string assemblyName, IList fixtures, bool autoSuites)
        {
            TestSuite testAssembly = new TestAssembly(assemblyName);

            if (autoSuites)
            {
                NamespaceTreeBuilder treeBuilder =
                    new NamespaceTreeBuilder(testAssembly);
                treeBuilder.Add(fixtures);
                testAssembly = treeBuilder.RootSuite;
            }
            else
            {
                foreach (TestSuite fixture in fixtures)
                {
                    if (fixture != null)
                    {
                        if (fixture is SetUpFixture)
                        {
                            fixture.RunState     = RunState.NotRunnable;
                            fixture.IgnoreReason = "SetUpFixture cannot be used when loading tests as a flat list of fixtures";
                        }

                        testAssembly.Add(fixture);
                    }
                }
            }

            if (fixtures.Count == 0)
            {
                testAssembly.RunState     = RunState.NotRunnable;
                testAssembly.IgnoreReason = "Has no TestFixtures";
            }

            NUnitFramework.ApplyCommonAttributes(assembly, testAssembly);

            testAssembly.Properties["_PID"]       = System.Diagnostics.Process.GetCurrentProcess().Id;
            testAssembly.Properties["_APPDOMAIN"] = AppDomain.CurrentDomain.FriendlyName;


            // TODO: Make this an option? Add Option to sort assemblies as well?
            testAssembly.Sort();

            return(testAssembly);
        }
Ejemplo n.º 18
0
        TestFixture BuildFixture(Type type, ParameterizedTestFixtureAttribute attr, object parameter)
        {
            var categories = (IList <string>)Reflect.GetPropertyValue(attr, "Categories");

            Type fixtureType;

            if (type.IsGenericType)
            {
                fixtureType = type.MakeGenericType(attr.Type);
            }
            else
            {
                fixtureType = type;
            }

            var parameterizedFixture = typeof(ParameterizedTestFixture <>).MakeGenericType(attr.Type);
            var fixture = (NUnitTestFixture)Reflect.Construct(parameterizedFixture, new object[] { fixtureType, Configuration, parameter });

            NUnitFramework.ApplyCommonAttributes(fixtureType, fixture);

            if (categories != null)
            {
                foreach (string category in categories)
                {
                    fixture.Categories.Add(category);
                }
            }

            if (fixture.RunState == RunState.Runnable && attr != null)
            {
                var objIgnore = Reflect.GetPropertyValue(attr, "Ignore");
                if (objIgnore != null && (bool)objIgnore == true)
                {
                    fixture.RunState     = RunState.Ignored;
                    fixture.IgnoreReason = (string)Reflect.GetPropertyValue(attr, "IgnoreReason");
                }
            }

            AddTestCases(fixture);
            return(fixture);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Determine if the method is an NUnit test method.
        /// The method must normally be marked with the test
        /// attribute for this to be true. If the test config
        /// file sets AllowOldStyleTests to true, then any
        /// method beginning "test..." (case-insensitive)
        /// is treated as a test unless it is also marked
        /// as a setup or teardown method.
        /// </summary>
        /// <param name="method">A MethodInfo for the method being used as a test method</param>
        /// <returns>True if the builder can create a test case from this method</returns>
        public override bool CanBuildFrom(MethodInfo method)
        {
            if (Reflect.HasAttribute(method, NUnitFramework.TestAttribute, false))
            {
                return(true);
            }

            if (allowOldStyleTests)
            {
                Regex regex = new Regex("^(?i:test)");
                if (regex.Match(method.Name).Success &&
                    !NUnitFramework.IsSetUpMethod(method) &&
                    !NUnitFramework.IsTearDownMethod(method) &&
                    !NUnitFramework.IsFixtureSetUpMethod(method) &&
                    !NUnitFramework.IsFixtureTearDownMethod(method))
                {
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 20
0
        private TestSuite BuildTestAssembly(string assemblyName, IList fixtures, bool autoSuites)
        {
            TestSuite testAssembly = new TestSuite(assemblyName);

            if (autoSuites)
            {
                NamespaceTreeBuilder treeBuilder =
                    new NamespaceTreeBuilder(testAssembly);
                treeBuilder.Add(fixtures);
                testAssembly = treeBuilder.RootSuite;
            }
            else
            {
                foreach (TestSuite fixture in fixtures)
                {
                    if (fixture is SetUpFixture)
                    {
                        fixture.RunState     = RunState.NotRunnable;
                        fixture.IgnoreReason = "SetUpFixture cannot be used when loading tests as a flat list of fixtures";
                    }

                    testAssembly.Add(fixture);
                }
            }

            if (fixtures.Count == 0)
            {
                testAssembly.RunState     = RunState.NotRunnable;
                testAssembly.IgnoreReason = "Has no TestFixtures";
            }

            NUnitFramework.ApplyCommonAttributes(assembly, testAssembly);

            // TODO: Make this an option? Add Option to sort assemblies as well?
            testAssembly.Sort();

            return(testAssembly);
        }
        /// <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="parms">The ParameterSet to be used, or null</param>
        /// <returns></returns>
        public static NUnitTestMethod BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms)
        {
            NUnitTestMethod testMethod = new NUnitTestMethod(method);

            string prefix = method.ReflectedType.FullName;

            if (parentSuite != null)
            {
                prefix = parentSuite.TestName.FullName;
                testMethod.TestName.FullName = prefix + "." + testMethod.TestName.Name;
            }

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

            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.TestName.Name     = parms.TestName;
                    testMethod.TestName.FullName = prefix + "." + parms.TestName;
                }
                else if (parms.OriginalArguments != null)
                {
                    string name = MethodHelper.GetDisplayName(method, parms.OriginalArguments);
                    testMethod.TestName.Name     = name;
                    testMethod.TestName.FullName = prefix + "." + name;
                }

                if (parms.Ignored)
                {
                    testMethod.RunState     = RunState.Ignored;
                    testMethod.IgnoreReason = parms.IgnoreReason;
                }
                else if (parms.Explicit)
                {
                    testMethod.RunState = RunState.Explicit;
                }

                if (parms.ExpectedExceptionName != null)
                {
                    testMethod.exceptionProcessor = new ExpectedExceptionProcessor(testMethod, parms);
                }

                foreach (string key in parms.Properties.Keys)
                {
                    testMethod.Properties[key] = parms.Properties[key];
                }

                // Description is stored in parms.Properties
                if (parms.Description != null)
                {
                    testMethod.Description = parms.Description;
                }
            }

            //if (testMethod.BuilderException != null && testMethod.RunState != RunState.NotRunnable)
            //{
            //    testMethod.RunState = RunState.NotRunnable;
            //    testMethod.IgnoreReason = testMethod.BuilderException.Message;
            //}

            if (parentSuite != null)
            {
                if (parentSuite.RunState == RunState.NotRunnable && testMethod.RunState != RunState.NotRunnable)
                {
                    testMethod.RunState     = RunState.NotRunnable;
                    testMethod.IgnoreReason = parentSuite.IgnoreReason;
                }

                if (parentSuite.RunState == RunState.Ignored && testMethod.RunState != RunState.Ignored && testMethod.RunState != RunState.NotRunnable)
                {
                    testMethod.RunState     = RunState.Ignored;
                    testMethod.IgnoreReason = parentSuite.IgnoreReason;
                }
            }

            return(testMethod);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Get Testcase with Test case attributes
        /// </summary>
        /// <param name="method">The MethodInfo object.</param>
        public ArrayList GetTestCasesWithTestCaseAttribute(MethodInfo method)
        {
            ArrayList testCaseCollection = new ArrayList();

            testMethods = Reflect.IsAsyncMethod(method) ? new NUnitAsyncTestMethod(method) : new NUnitTestMethod(method);
            ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, methodSuite);
            IEnumerable ParameterList = ParamProvider.GetTestCasesFor(method);

            testCaseCollection.Add(testMethods.FixtureType.FullName + "." + method.Name);
            foreach (object source in ParameterList)
            {
                ParameterSet parms;
                if (source == null)
                {
                    parms           = new ParameterSet();
                    parms.Arguments = new object[] { null };
                }
                else
                {
                    parms = source as ParameterSet;
                }
                if (parms == null)
                {
                    if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null)
                    {
                        parms = ParameterSet.FromDataSource(source);
                    }
                    else
                    {
                        parms = new ParameterSet();
                        ParameterInfo[] parameters = method.GetParameters();
                        Type            sourceType = source.GetType();
                        if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                        {
                            parms.Arguments = new object[] { source }
                        }
                        ;
                        else if (source is object[])
                        {
                            parms.Arguments = (object[])source;
                        }
                        else if (source is Array)
                        {
                            Array array = (Array)source;
                            if (array.Rank == 1)
                            {
                                parms.Arguments = new object[array.Length];
                                for (int i = 0; i < array.Length; i++)
                                {
                                    parms.Arguments[i] = (object)array.GetValue(i);
                                }
                            }
                        }
                        else
                        {
                            parms.Arguments = new object[] { source }
                        };
                    }
                }
                TestMethod testMethod = NUnit.Core.Builders.NUnitTestCaseBuilder.BuildSingleTestMethod(method, null, parms);
                testCaseCollection.Add(testMethod.TestName.FullName);
            }
            return(testCaseCollection);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Set additional properties of the newly created test case based
 /// on its attributes. As implemented, the method sets the test's
 /// RunState,  Description, Categories and Properties.
 /// </summary>
 /// <param name="method">A MethodInfo for the method being used as a test method</param>
 /// <param name="testCase">The test case being constructed</param>
 protected override void SetTestProperties(MethodInfo method, TestCase testCase)
 {
     NUnitFramework.ApplyCommonAttributes(method, testCase);
     NUnitFramework.ApplyExpectedExceptionAttribute(method, (TestMethod)testCase);
 }
        public TestMethodExtension BuildSingleTestMethod(MethodInfo method, Test parentSuite, ParameterSet parms, ScreenCapture screenCapture, IDictionary properties)
        {
            //TODO : Here it doesn't support  Async Await Method, BTW, the Async Await just be supported from .net 4.5
            TestMethodExtension testMethod = new TestMethodExtension(method, screenCapture);

            string prefix = method.ReflectedType.FullName;

            if (parentSuite != null)
            {
                testMethod.Properties = properties;
                prefix = parentSuite.TestName.FullName;
                testMethod.TestName.FullName = prefix + "." + testMethod.TestName.Name;
            }

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

            if (parms != null)
            {
                method = testMethod.Method;

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

                if (parms.Ignored)
                {
                    testMethod.RunState     = RunState.Ignored;
                    testMethod.IgnoreReason = parms.IgnoreReason;
                }
                else if (parms.Explicit)
                {
                    testMethod.RunState = RunState.Explicit;
                }

                foreach (string key in parms.Properties.Keys)
                {
                    testMethod.Properties[key] = parms.Properties[key];
                }

                if (parms.Description != null)
                {
                    testMethod.Description = parms.Description;
                }
            }

            if (parentSuite != null)
            {
                if (parentSuite.RunState == RunState.NotRunnable && testMethod.RunState != RunState.NotRunnable)
                {
                    testMethod.RunState     = RunState.NotRunnable;
                    testMethod.IgnoreReason = parentSuite.IgnoreReason;
                }

                if (parentSuite.RunState == RunState.Ignored && testMethod.RunState != RunState.Ignored && testMethod.RunState != RunState.NotRunnable)
                {
                    testMethod.RunState     = RunState.Ignored;
                    testMethod.IgnoreReason = parentSuite.IgnoreReason;
                }
            }

            return(testMethod);
        }
 public RandomizerTestFixture(Type fixtureType)
     : base(fixtureType)
 {
     this.fixtureSetUp    = NUnitFramework.GetFixtureSetUpMethod(fixtureType);
     this.fixtureTearDown = NUnitFramework.GetFixtureTearDownMethod(fixtureType);
 }
        private Test BuildSingleFixture(Type type, Attribute attr)
        {
            object[] arguments  = null;
            IList    categories = null;

            if (attr != null)
            {
                arguments = GetArgsFromAttribute(attr);

                categories = Reflect.GetPropertyValue(attr, "Categories") as IList;
#if CLR_2_0 || CLR_4_0
                if (type.ContainsGenericParameters)
                {
                    Type[] typeArgs = GetTypeArgsFromAttribute(attr);
                    if (typeArgs == null)
                    {
                        int cnt = 0;
                        foreach (object o in arguments)
                        {
                            if (o is Type)
                            {
                                cnt++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        typeArgs = new Type[cnt];
                        for (int i = 0; i < cnt; i++)
                        {
                            typeArgs[i] = (Type)arguments[i];
                        }

                        if (cnt > 0)
                        {
                            object[] args = new object[arguments.Length - cnt];
                            for (int i = 0; i < args.Length; i++)
                            {
                                args[i] = arguments[cnt + i];
                            }

                            arguments = args;
                        }
                    }

                    if (typeArgs.Length > 0 ||
                        TypeHelper.CanDeduceTypeArgsFromArgs(type, arguments, ref typeArgs))
                    {
                        type = TypeHelper.MakeGenericType(type, typeArgs);
                    }
                }
#endif
            }

            this.fixture = new NUnitTestFixture(type, arguments);
            CheckTestFixtureIsValid(fixture);

            NUnitFramework.ApplyCommonAttributes(type, fixture);

            if (categories != null)
            {
                foreach (string category in categories)
                {
                    fixture.Categories.Add(category);
                }
            }

            if (fixture.RunState == RunState.Runnable && attr != null)
            {
                object objIgnore = Reflect.GetPropertyValue(attr, "Ignore");
                if (objIgnore != null && (bool)objIgnore == true)
                {
                    fixture.RunState     = RunState.Ignored;
                    fixture.IgnoreReason = (string)Reflect.GetPropertyValue(attr, "IgnoreReason");
                }
            }

            AddTestCases(type);

            return(this.fixture);
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Method that sets properties of the test suite based on the
        /// information in the provided Type.
        /// </summary>
        /// <param name="type">The type to examine</param>
        /// <param name="suite">The test suite being constructed</param>
        protected override void SetTestSuiteProperties(Type type, TestSuite suite)
        {
            base.SetTestSuiteProperties(type, suite);

            NUnitFramework.ApplyCommonAttributes(type, suite);
        }
 protected override void DoOneTimeSetUp(TestResult suiteResult)
 {
     base.DoOneTimeSetUp(suiteResult);
     suiteResult.AssertCount = NUnitFramework.GetAssertCount();
 }
 protected override void DoOneTimeTearDown(TestResult suiteResult)
 {
     base.DoOneTimeTearDown(suiteResult);
     suiteResult.AssertCount += NUnitFramework.GetAssertCount();
 }
        /// <summary>
        /// Builds a ParameterizedMetodSuite containing individual
        /// test cases for each set of parameters provided for
        /// this method.
        /// </summary>
        /// <param name="method">The MethodInfo for which a test is to be built</param>
        /// <returns>A ParameterizedMethodSuite populated with test cases</returns>
        public static Test BuildParameterizedMethodSuite(MethodInfo method, Test parentSuite)
        {
            ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method);

            NUnitFramework.ApplyCommonAttributes(method, methodSuite);

            if (parentSuite != null)
            {
                if (parentSuite.RunState == RunState.NotRunnable && methodSuite.RunState != RunState.NotRunnable)
                {
                    methodSuite.RunState     = RunState.NotRunnable;
                    methodSuite.IgnoreReason = parentSuite.IgnoreReason;
                }

                if (parentSuite.RunState == RunState.Ignored && methodSuite.RunState != RunState.Ignored && methodSuite.RunState != RunState.NotRunnable)
                {
                    methodSuite.RunState     = RunState.Ignored;
                    methodSuite.IgnoreReason = parentSuite.IgnoreReason;
                }
            }

            foreach (object source in CoreExtensions.Host.TestCaseProviders.GetTestCasesFor(method, parentSuite))
            {
                ParameterSet parms;

                if (source == null)
                {
                    parms           = new ParameterSet();
                    parms.Arguments = new object[] { null };
                }
                else
                {
                    parms = source as ParameterSet;
                }

                if (parms == null)
                {
                    if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null)
                    {
                        parms = ParameterSet.FromDataSource(source);
                    }
                    else
                    {
                        parms = new ParameterSet();

                        ParameterInfo[] parameters = method.GetParameters();
                        Type            sourceType = source.GetType();

                        if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType))
                        {
                            parms.Arguments = new object[] { source }
                        }
                        ;
                        else if (source is object[])
                        {
                            parms.Arguments = (object[])source;
                        }
                        else if (source is Array)
                        {
                            Array array = (Array)source;
                            if (array.Rank == 1)
                            {
                                parms.Arguments = new object[array.Length];
                                for (int i = 0; i < array.Length; i++)
                                {
                                    parms.Arguments[i] = (object)array.GetValue(i);
                                }
                            }
                        }
                        else
                        {
                            parms.Arguments = new object[] { source }
                        };
                    }
                }

                TestMethod test = BuildSingleTestMethod(method, parentSuite, parms);

                methodSuite.Add(test);
            }

            return(methodSuite);
        }