Esempio n. 1
0
 private void BuildFixturesFromType(Test parent, ITypeInfo type)
 {
     try
     {
         foreach (TestFixturePatternAttribute2 attrib in AttributeUtils.GetAttributes <TestFixturePatternAttribute2>(type, true))
         {
             BuildTestFixtureFromPatternAttribute(parent, type, attrib);
         }
     }
     catch (Exception ex)
     {
         testModel.AddAnnotation(new Annotation(AnnotationType.Error, type,
                                                "An exception was thrown while exploring an MbUnit v2 test type.", ex));
     }
 }
Esempio n. 2
0
        private Test TryGetTypeTest(ITypeInfo type, Test assemblyTest)
        {
            Test typeTest;

            if (!typeTests.TryGetValue(type, out typeTest))
            {
                try
                {
                    XunitTypeInfoAdapter xunitTypeInfo = new XunitTypeInfoAdapter(type);
                    ITestClassCommand    command       = TestClassCommandFactory.Make(xunitTypeInfo);
                    if (command != null)
                    {
                        typeTest = CreateTypeTest(xunitTypeInfo, command);
                    }
                }
                catch (Exception ex)
                {
                    TestModel.AddAnnotation(new Annotation(AnnotationType.Error, type, "An exception was thrown while exploring an xUnit.net test type.", ex));
                }

                if (typeTest != null)
                {
                    assemblyTest.AddChild(typeTest);
                    typeTests.Add(type, typeTest);
                }
            }

            return(typeTest);
        }
Esempio n. 3
0
        private Test TryGetTypeTest(ITypeInfo type, Test assemblyTest)
        {
            Test typeTest;

            if (!typeTests.TryGetValue(type, out typeTest))
            {
                try
                {
                    foreach (var attribute in type.GetAttributes(null, false))
                    {
                        if (attribute is ConcordionTestAttribute)
                        {
                            typeTest = CreateTypeTest(new ConcordionTypeInfoAdapter(type));
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    TestModel.AddAnnotation(new Annotation(AnnotationType.Error, type, "An exception was thrown while exploring an concordion test type.", ex));
                }

                if (typeTest != null)
                {
                    assemblyTest.AddChild(typeTest);
                    typeTests.Add(type, typeTest);
                }
            }
            return(typeTest);
        }
Esempio n. 4
0
        private Test TryGetTypeTest(ITypeInfo type, Test assemblyTest)
        {
            Test typeTest;

            if (!typeTests.TryGetValue(type, out typeTest))
            {
                try
                {
                    if (IsFixture(type))
                    {
                        typeTest = CreateTypeTest(type);
                    }
                }
                catch (Exception ex)
                {
                    TestModel.AddAnnotation(new Annotation(AnnotationType.Error, type, "An exception was thrown while exploring an MSTest test type.", ex));
                }

                if (typeTest != null)
                {
                    assemblyTest.AddChild(typeTest);
                    typeTests.Add(type, typeTest);
                }
            }

            return(typeTest);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a message sink that populates the test model as test and annotation
        /// discovered messages are published.
        /// </summary>
        /// <param name="testModel">The test model to populate.</param>
        /// <returns>The message sink.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="testModel"/> is null.</exception>
        public static IMessageSink CreateMessageSinkToPopulateTestModel(TestModel testModel)
        {
            if (testModel == null)
            {
                throw new ArgumentNullException("testModel");
            }

            return(new MessageConsumer()
                   .Handle <TestDiscoveredMessage>(message =>
            {
                Test test = message.Test.ToTest();

                if (message.ParentTestId != null)
                {
                    Test parentTest = testModel.FindTest(message.ParentTestId);
                    if (parentTest == null)
                    {
                        throw new InvalidOperationException("The parent test is missing.");
                    }

                    parentTest.AddChild(test);
                }
                else
                {
                    testModel.RootTest = test;
                }
            })
                   .Handle <AnnotationDiscoveredMessage>(message =>
            {
                testModel.AddAnnotation(message.Annotation.ToAnnotation());
            }));
        }
Esempio n. 6
0
        protected override void ExploreImpl(IReflectionPolicy reflectionPolicy, ICodeElementInfo codeElement)
        {
            IAssemblyInfo assembly = ReflectionUtils.GetAssembly(codeElement);

            if (assembly == null)
            {
                return;
            }

            try
            {
                MbUnit2TestExplorerEngine engine;
                if (!assemblyTestExplorerEngines.TryGetValue(assembly, out engine))
                {
                    Assembly loadedAssembly = assembly.Resolve(false);

                    if (Reflector.IsUnresolved(loadedAssembly))
                    {
                        engine = new MbUnit2ReflectiveTestExplorerEngine(TestModel, assembly);
                    }
                    else
                    {
                        engine = new MbUnit2NativeTestExplorerEngine(TestModel, loadedAssembly);
                    }

                    assemblyTestExplorerEngines.Add(assembly, engine);

                    bool skipChildren = !(codeElement is IAssemblyInfo);
                    engine.ExploreAssembly(skipChildren, unresolvedDependencies);

                    for (int i = 0; i < unresolvedDependencies.Count; i++)
                    {
                        foreach (var entry in assemblyTestExplorerEngines)
                        {
                            if (entry.Key.FullName == unresolvedDependencies[i].Value)
                            {
                                unresolvedDependencies[i].Key.AddDependency(entry.Value.GetAssemblyTest());
                                unresolvedDependencies.RemoveAt(i--);
                                break;
                            }
                        }
                    }
                }

                ITypeInfo type = ReflectionUtils.GetType(codeElement);
                if (type != null)
                {
                    engine.ExploreType(type);
                }
            }
            catch (Exception ex)
            {
                TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly,
                                                       "An exception was thrown while exploring an MbUnit v2 test assembly.", ex));
            }
        }
Esempio n. 7
0
        public override void Finish()
        {
            try
            {
                foreach (NUnitTestExplorerEngine engine in assemblyTestExplorerEngines.Values)
                {
                    engine.Finish();
                }
            }
            catch (Exception ex)
            {
                TestModel.AddAnnotation(new Annotation(AnnotationType.Error, null,
                                                       "An exception was thrown while finishing the population of the NUnit test model.", ex));
            }

            base.Finish();
        }
Esempio n. 8
0
        private TestModel PublishTestModelFromCodeElements(IEnumerable <ICodeElementInfo> codeElements, IMessageSink messageSink, IProgressMonitor progressMonitor)
        {
            TestModel testModel = new TestModel();
            var       tests     = new Dictionary <ICodeElementInfo, Test>();

            foreach (ICodeElementInfo codeElement in codeElements)
            {
                if (progressMonitor.IsCanceled)
                {
                    return(null);
                }

                testModel.AddAnnotation(new Annotation(AnnotationType.Warning, codeElement, FallbackExplanation));

                IAssemblyInfo assembly = ReflectionUtils.GetAssembly(codeElement);
                if (assembly != null)
                {
                    Test assemblyTest;
                    if (!tests.TryGetValue(assembly, out assemblyTest))
                    {
                        assemblyTest = CreateTest(Path.GetFileName(assembly.Path), assembly);
                        testModel.RootTest.AddChild(assemblyTest);
                        tests.Add(assembly, assemblyTest);

                        ITypeInfo type = ReflectionUtils.GetType(codeElement);
                        if (type != null)
                        {
                            Test typeTest;
                            if (!tests.TryGetValue(type, out typeTest))
                            {
                                typeTest = CreateTest(type.Name, type);
                                assemblyTest.AddChild(typeTest);
                                tests.Add(type, typeTest);
                            }
                        }
                    }
                }

                progressMonitor.Worked(1);
            }

            TestModelSerializer.PublishTestModel(testModel, messageSink);
            return(testModel);
        }
Esempio n. 9
0
        protected override void ExploreImpl(IReflectionPolicy reflectionPolicy, ICodeElementInfo codeElement)
        {
            IAssemblyInfo assembly = ReflectionUtils.GetAssembly(codeElement);

            if (assembly == null)
            {
                return;
            }

            try
            {
                NUnitTestExplorerEngine engine;
                if (!assemblyTestExplorerEngines.TryGetValue(assembly, out engine))
                {
                    Assembly loadedAssembly = assembly.Resolve(false);

                    if (Reflector.IsUnresolved(loadedAssembly))
                    {
                        engine = new NUnitReflectiveTestExplorerEngine(TestModel, assembly);
                    }
                    else
                    {
                        engine = new NUnitNativeTestExplorerEngine(TestModel, loadedAssembly);
                    }

                    assemblyTestExplorerEngines.Add(assembly, engine);

                    bool skipChildren = !(codeElement is IAssemblyInfo);
                    engine.ExploreAssembly(skipChildren);
                }

                ITypeInfo type = ReflectionUtils.GetType(codeElement);
                if (type != null)
                {
                    engine.ExploreType(type);
                }
            }
            catch (Exception ex)
            {
                TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly,
                                                       "An exception was thrown while exploring an NUnit test assembly.", ex));
            }
        }
Esempio n. 10
0
        private Test GetAssemblyTest(IAssemblyInfo assembly, Test parentTest, Version frameworkVersion)
        {
            Test assemblyTest;

            if (assemblyTests.TryGetValue(assembly, out assemblyTest))
            {
                return(assemblyTest);
            }

            try
            {
                Assembly loadedAssembly = assembly.Resolve(false);

                if (Reflector.IsUnresolved(loadedAssembly))
                {
                    assemblyTest = BuildAssemblyTest_Reflective(assembly);
                }
                else
                {
                    assemblyTest = BuildAssemblyTest_Native(assembly, loadedAssembly.Location);
                }

                string frameworkName = String.Format(Resources.CSUnitTestExplorer_FrameworkNameWithVersionFormat, frameworkVersion);
                assemblyTest.Metadata.SetValue(MetadataKeys.Framework, frameworkName);
                assemblyTest.Metadata.SetValue(MetadataKeys.File, assembly.Path);
                assemblyTest.Kind = AssemblyKind;
            }
            catch (Exception ex)
            {
                TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly,
                                                       "An exception was thrown while exploring a csUnit test assembly.", ex));
                return(null);
            }

            if (assemblyTest != null)
            {
                parentTest.AddChild(assemblyTest);

                assemblyTests.Add(assembly, assemblyTest);
            }
            return(assemblyTest);
        }
        private void BuildNUnitFixturesFromType(ITypeInfo type)
        {
            try
            {
                // Note: This code takes advantage of the fact that ITypeInfo.Resolve will
                //       return an UnresolvedType object which adapts ITypeInfo to Type in a
                //       way that enables the native NUnit builders to perform the needed
                //       reflection in most cases.
                Type resolvedType = type.Resolve(false);

                if (NUnit.Core.TestFixtureBuilder.CanBuildFrom(resolvedType))
                {
                    var nunitFixture = (NUnit.Core.TestSuite)NUnit.Core.TestFixtureBuilder.BuildFrom(resolvedType);
                    nunitFixtures.Add(nunitFixture);
                }
            }
            catch (Exception ex)
            {
                testModel.AddAnnotation(new Annotation(AnnotationType.Error, type,
                                                       "An exception was thrown while exploring an NUnit test type.  This probably indicates that the type uses certain NUnit features that are not supported by the reflection-only test explorer engine.", ex));
            }
        }
Esempio n. 12
0
        private MSTest CreateTypeTest(ITypeInfo typeInfo)
        {
            MSTest typeTest = new MSTest(typeInfo.Name, typeInfo);

            typeTest.Kind = TestKinds.Fixture;

            foreach (IMethodInfo method in typeInfo.GetMethods(BindingFlags.Public | BindingFlags.Instance))
            {
                IEnumerable <IAttributeInfo> methodAttributes = method.GetAttributeInfos(null, true);
                foreach (IAttributeInfo methodAttribute in methodAttributes)
                {
                    if (methodAttribute.Type.FullName.CompareTo(MSTestAttributes.TestMethodAttribute) == 0)
                    {
                        try
                        {
                            MSTest testMethod = CreateMethodTest(typeInfo, method);
                            typeTest.AddChild(testMethod);
                        }
                        catch (Exception ex)
                        {
                            TestModel.AddAnnotation(new Annotation(AnnotationType.Error, method, "An exception was thrown while exploring an MSTest test method.", ex));
                        }
                        break;
                    }
                }
            }

            PopulateTestClassMetadata(typeInfo, typeTest);

            // Add XML documentation.
            string xmlDocumentation = typeInfo.GetXmlDocumentation();

            if (xmlDocumentation != null)
            {
                typeTest.Metadata.SetValue(MetadataKeys.XmlDocumentation, xmlDocumentation);
            }

            return(typeTest);
        }
Esempio n. 13
0
        private void GetInputOutputDirectories(IAssemblyInfo assembly)
        {
            var config = new SpecificationConfig().Load(assembly.Resolve(false));

            var baseInputDirectoryInfo = new DirectoryInfo(config.BaseInputDirectory);

            if (baseInputDirectoryInfo.Exists)
            {
            }
            else
            {
                TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly, String.Format("The Base Input Directory {0} does not exist, reverting to default", config.BaseInputDirectory)));
            }

            var baseOutputDirectoryInfo = new DirectoryInfo(config.BaseOutputDirectory);

            this._baseOutputDirectory = baseOutputDirectoryInfo;

            if (!_baseOutputDirectory.Exists)
            {
                Directory.CreateDirectory(_baseOutputDirectory.FullName);
            }
        }
Esempio n. 14
0
        private Test BuildAssemblyTest_Native(IAssemblyInfo assembly, string location)
        {
            if (String.IsNullOrEmpty(location))
            {
                throw new ArgumentNullException("location");
            }

            // If csUnit.Core is not in the GAC then it must be in the assembly path due to a
            // bug in csUnit setting up the AppDomain.  It sets the AppDomain's base directory
            // to the assembly directory and sets its PrivateBinPath to the csUnit directory
            // which is incorrect (PrivateBinPath only specifies directories relative to the app base)
            // so it does actually not ensure that csUnit.Core can be loaded as desired.
            Assembly csUnitCoreAssembly = typeof(csUnit.Core.Loader).Assembly;

            if (!csUnitCoreAssembly.GlobalAssemblyCache)
            {
                string csUnitAppBase = Path.GetDirectoryName(location);
                string csUnitCoreAssemblyPathExpected = Path.Combine(csUnitAppBase, "csUnit.Core.dll");
                if (!File.Exists(csUnitCoreAssemblyPathExpected))
                {
                    return(CreateAssemblyTest(assembly, location, delegate(Test assemblyTest)
                    {
                        TestModel.AddAnnotation(new Annotation(AnnotationType.Error, null,
                                                               string.Format("Cannot load csUnit tests from '{0}'.  "
                                                                             + "'csUnit.Core.dll' and related DLLs must either be copied to the same directory as the test assembly or must be installed in the GAC.",
                                                                             location)));
                    }));
                }
            }

            // Load the assembly using the native CSUnit loader.
            using (Loader loader = new Loader(location))
            {
                // Construct the test tree
                return(CreateAssemblyTest(assembly, location, delegate(Test assemblyTest)
                {
                    TestFixtureInfoCollection collection = loader.TestFixtureInfos;
                    if (collection == null)
                    {
                        return;
                    }

                    foreach (ITestFixtureInfo fixtureInfo in collection)
                    {
                        try
                        {
                            ITypeInfo fixtureType = assembly.GetType(fixtureInfo.FullName);

                            assemblyTest.AddChild(CreateFixtureFromType(fixtureType, delegate(Test fixtureTest)
                            {
                                if (fixtureInfo.TestMethods == null)
                                {
                                    return;
                                }

                                foreach (ITestMethodInfo testMethodInfo in fixtureInfo.TestMethods)
                                {
                                    try
                                    {
                                        IMethodInfo methodType = fixtureType.GetMethod(testMethodInfo.Name,
                                                                                       BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);

                                        fixtureTest.AddChild(CreateTestFromMethod(methodType, null));
                                    }
                                    catch (Exception ex)
                                    {
                                        TestModel.AddAnnotation(new Annotation(AnnotationType.Error, fixtureType,
                                                                               "An exception was thrown while exploring a csUnit test case.", ex));
                                    }
                                }
                            }));
                        }
                        catch (Exception ex)
                        {
                            TestModel.AddAnnotation(new Annotation(AnnotationType.Error, assembly,
                                                                   "An exception was thrown while exploring a csUnit test fixture.", ex));
                        }
                    }
                }));
            }
        }