static Type GetTestFrameworkType(_IAssemblyInfo testAssembly) { try { var testFrameworkAttr = testAssembly.GetCustomAttributes(typeof(ITestFrameworkAttribute)).FirstOrDefault(); if (testFrameworkAttr != null) { var discovererAttr = testFrameworkAttr.GetCustomAttributes(typeof(TestFrameworkDiscovererAttribute)).FirstOrDefault(); if (discovererAttr != null) { var discoverer = GetTestFrameworkTypeDiscoverer(discovererAttr); if (discoverer != null) { var discovererType = discoverer.GetTestFrameworkType(testFrameworkAttr); if (discovererType != null) { return(discovererType); } } TestContext.Current?.SendDiagnosticMessage("Unable to create custom test framework discoverer type from '{0}'", testFrameworkAttr.GetType().FullName); } else { TestContext.Current?.SendDiagnosticMessage("Assembly-level test framework attribute was not decorated with [TestFrameworkDiscoverer]"); } } } catch (Exception ex) { TestContext.Current?.SendDiagnosticMessage("Exception thrown during test framework discoverer construction: {0}", ex.Unwrap()); } return(typeof(XunitTestFramework)); }
/// <summary> /// Gets all the custom attributes for the given assembly. /// </summary> /// <param name="assemblyInfo">The assembly</param> /// <param name="attributeType">The type of the attribute</param> /// <returns>The matching attributes that decorate the assembly</returns> public static IEnumerable <_IAttributeInfo> GetCustomAttributes(this _IAssemblyInfo assemblyInfo, Type attributeType) { Guard.ArgumentNotNull(nameof(assemblyInfo), assemblyInfo); Guard.ArgumentNotNull(nameof(attributeType), attributeType); Guard.NotNull("Attribute type cannot be a generic type parameter", attributeType.AssemblyQualifiedName); return(assemblyInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName)); }
/// <summary> /// Gets all the custom attributes for the given assembly. /// </summary> /// <param name="assemblyInfo">The assembly</param> /// <param name="attributeType">The type of the attribute</param> /// <returns>The matching attributes that decorate the assembly</returns> public static IReadOnlyCollection <_IAttributeInfo> GetCustomAttributes(this _IAssemblyInfo assemblyInfo, Type attributeType) { Guard.ArgumentNotNull(assemblyInfo); Guard.ArgumentNotNull(attributeType); Guard.NotNull("Attribute type cannot be a generic type parameter", attributeType.AssemblyQualifiedName); return(assemblyInfo.GetCustomAttributes(attributeType.AssemblyQualifiedName)); }
/// <summary> /// Gets the target framework name for the given assembly. /// </summary> /// <param name="assembly">The assembly.</param> /// <returns>The target framework (typically in a format like ".NETFramework,Version=v4.7.2" /// or ".NETCoreApp,Version=v2.1"). If the target framework type is unknown (missing file, /// missing attribute, etc.) then returns "UnknownTargetFramework".</returns> public static string GetTargetFramework(this _IAssemblyInfo assembly) { Guard.ArgumentNotNull(nameof(assembly), assembly); string?result = null; var attrib = assembly.GetCustomAttributes(typeof(TargetFrameworkAttribute)).FirstOrDefault(); if (attrib != null) { result = attrib.GetConstructorArguments().Cast <string>().First(); } return(result ?? AssemblyExtensions.UnknownTargetFramework); }
/// <summary> /// Initializes a new instance of the <see cref="XunitTestFrameworkDiscoverer"/> class. /// </summary> /// <param name="assemblyInfo">The test assembly.</param> /// <param name="configFileName">The test configuration file.</param> /// <param name="collectionFactory">The test collection factory used to look up test collections.</param> public XunitTestFrameworkDiscoverer( _IAssemblyInfo assemblyInfo, string?configFileName, IXunitTestCollectionFactory?collectionFactory = null) : base(assemblyInfo) { var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault(); var disableParallelization = collectionBehaviorAttribute != null && collectionBehaviorAttribute.GetNamedArgument <bool>("DisableTestParallelization"); testAssembly = new TestAssembly(assemblyInfo, configFileName); TestCollectionFactory = collectionFactory ?? ExtensibilityPointFactory.GetXunitTestCollectionFactory(collectionBehaviorAttribute, testAssembly) ?? new CollectionPerClassTestCollectionFactory(testAssembly); TestFrameworkDisplayName = $"{DisplayName} [{TestCollectionFactory.DisplayName}, {(disableParallelization ? "non-parallel" : "parallel")}]"; }
/// <summary> /// Initializes a new instance of the <see cref="XunitTestFrameworkDiscoverer"/> class. /// </summary> /// <param name="assemblyInfo">The test assembly.</param> /// <param name="configFileName">The test configuration file.</param> /// <param name="sourceProvider">The source information provider.</param> /// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param> /// <param name="collectionFactory">The test collection factory used to look up test collections.</param> public XunitTestFrameworkDiscoverer( _IAssemblyInfo assemblyInfo, string?configFileName, _ISourceInformationProvider sourceProvider, _IMessageSink diagnosticMessageSink, IXunitTestCollectionFactory?collectionFactory = null) : base(assemblyInfo, configFileName, sourceProvider, diagnosticMessageSink) { var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault(); var disableParallelization = collectionBehaviorAttribute != null && collectionBehaviorAttribute.GetNamedArgument <bool>("DisableTestParallelization"); var testAssembly = new TestAssembly(assemblyInfo, configFileName); TestAssemblyUniqueID = testAssembly.UniqueID; TestCollectionFactory = collectionFactory ?? ExtensibilityPointFactory.GetXunitTestCollectionFactory(diagnosticMessageSink, collectionBehaviorAttribute, testAssembly) ?? new CollectionPerClassTestCollectionFactory(testAssembly, diagnosticMessageSink); TestFrameworkDisplayName = $"{DisplayName} [{TestCollectionFactory.DisplayName}, {(disableParallelization ? "non-parallel" : "parallel")}]"; }