Exemplo n.º 1
0
        /// <summary>
        /// Returns an implementation of <see cref="IFrontController"/> which can be used
        /// for both discovery and execution.
        /// </summary>
        /// <param name="projectAssembly">The test project assembly.</param>
        /// <param name="sourceInformationProvider">The source information provider. If <c>null</c>, uses the default (<see cref="T:Xunit.VisualStudioSourceInformationProvider"/>).</param>
        /// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param>
        public static IFrontController ForDiscoveryAndExecution(
            XunitProjectAssembly projectAssembly,
            _ISourceInformationProvider?sourceInformationProvider = null,
            _IMessageSink?diagnosticMessageSink = null)
        {
            Guard.ArgumentNotNull(projectAssembly);

            var innerController  = default(IFrontController);
            var assemblyFileName = projectAssembly.AssemblyFileName;
            var assemblyFolder   = Path.GetDirectoryName(assemblyFileName);

            if (diagnosticMessageSink == null)
            {
                diagnosticMessageSink = _NullMessageSink.Instance;
            }

#if NETFRAMEWORK
            if (sourceInformationProvider == null)
            {
                if (assemblyFileName == null)
                {
                    sourceInformationProvider = _NullSourceInformationProvider.Instance;
                }
                else
                {
                    sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticMessageSink);
                }
            }

            if (assemblyFolder != null)
            {
                if (Directory.EnumerateFiles(assemblyFolder, "xunit.execution.*.dll").Any())
                {
                    innerController = Xunit2.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
                }
                else
                {
                    var xunitPath = Path.Combine(assemblyFolder, "xunit.dll");
                    if (File.Exists(xunitPath))
                    {
                        innerController = Xunit1.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
                    }
                }
            }
#else
            if (sourceInformationProvider == null)
            {
                sourceInformationProvider = _NullSourceInformationProvider.Instance;
            }

            innerController = Xunit2.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
#endif

            if (innerController == null)
            {
                throw new InvalidOperationException($"Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in {assemblyFolder ?? "<unknown assembly folder>"}");
            }

            return(new XunitFrontController(innerController));
        }
Exemplo n.º 2
0
        // Factory methods

        /// <summary>
        /// Returns an implementation of <see cref="IFrontControllerDiscoverer"/> which can be
        /// used to discovery tests, including source-based discovery (note that xUnit.net v1
        /// does not support source-based discovery).
        /// </summary>
        /// <param name="assemblyInfo">The assembly to use for discovery</param>
        /// <param name="projectAssembly">The test project assembly.</param>
        /// <param name="referenceList">The full path names of all referenced assemblies. This is used to
        /// search for references to specific xUnit.net reference assemblies to determine which version
        /// of xUnit.net the tests were written against.</param>
        /// <param name="sourceInformationProvider">The optional source information provider.</param>
        /// <param name="diagnosticMessageSink">The message sink which receives <see cref="_DiagnosticMessage"/> messages.</param>
        /// <returns></returns>
        public static IFrontControllerDiscoverer ForDiscovery(
            _IAssemblyInfo assemblyInfo,
            XunitProjectAssembly projectAssembly,
            IReadOnlyCollection <string> referenceList,
            _ISourceInformationProvider?sourceInformationProvider = null,
            _IMessageSink?diagnosticMessageSink = null)
        {
            Guard.ArgumentNotNull(assemblyInfo);
            Guard.ArgumentNotNull(projectAssembly);
            Guard.ArgumentNotNull(referenceList);

            var innerDiscoverer  = default(IFrontControllerDiscoverer);
            var assemblyFileName = projectAssembly.AssemblyFileName;

            if (diagnosticMessageSink == null)
            {
                diagnosticMessageSink = _NullMessageSink.Instance;
            }

            if (sourceInformationProvider == null)
            {
                sourceInformationProvider = _NullSourceInformationProvider.Instance;
#if NETFRAMEWORK
                if (assemblyFileName != null)
                {
                    sourceInformationProvider = new VisualStudioSourceInformationProvider(assemblyFileName, diagnosticMessageSink);
                }
#endif
            }

            var v2PathPattern        = new Regex(@"^xunit\.execution\..*\.dll$");
            var v2ExecutionReference = referenceList.FirstOrDefault(reference => v2PathPattern.IsMatch(Path.GetFileNameWithoutExtension(reference)));
            if (v2ExecutionReference != null)
            {
                innerDiscoverer = Xunit2.ForDiscovery(assemblyInfo, projectAssembly, v2ExecutionReference, sourceInformationProvider, diagnosticMessageSink);
            }

#if NETFRAMEWORK
            if (referenceList.Any(reference => Path.GetFileNameWithoutExtension(reference) == "xunit.dll"))
            {
                innerDiscoverer = Xunit1.ForDiscoveryAndExecution(projectAssembly, sourceInformationProvider, diagnosticMessageSink);
            }
#endif

            if (innerDiscoverer == null)
            {
                throw new InvalidOperationException($"Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in assembly reference list");
            }

            return(new XunitFrontController(innerDiscoverer));
        }
Exemplo n.º 3
0
        public void AmbiguouslyNamedTestMethods_StillReturnAllMessages()
        {
            var code = @"
using Xunit;
using Xunit.Extensions;

public class AmbiguouslyNamedTestMethods
{
    [Theory]
    [InlineData(12)]
    public void TestMethod1(int value)
    {
    }

    [Theory]
    [InlineData(""foo"")]
    public void TestMethod1(string value)
    {
    }
}";

            using (var assembly = AcceptanceTestV1Assembly.Create(code))
            using (var xunit1 = new Xunit1(AppDomainSupport.Required, new NullSourceInformationProvider(), assembly.FileName))
            {
                var spy = new SpyMessageSink<ITestAssemblyFinished>();
                xunit1.Run(spy);
                spy.Finished.WaitOne();

                Assert.Collection(spy.Messages,
                    msg => Assert.IsAssignableFrom<ITestAssemblyStarting>(msg),
                    msg => Assert.IsAssignableFrom<ITestCollectionStarting>(msg),
                    msg => Assert.IsAssignableFrom<ITestClassStarting>(msg),
                    msg => Assert.IsAssignableFrom<ITestClassFinished>(msg),
                    msg => Assert.IsAssignableFrom<ITestCollectionFinished>(msg),
                    msg => Assert.IsAssignableFrom<IErrorMessage>(msg),
                    msg => Assert.IsAssignableFrom<ITestAssemblyFinished>(msg)
                );
            }
        }