Esempio n. 1
0
        public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
        {
            Guard.ArgumentNotNull("sources", sources);
            Guard.ArgumentNotNull("logger", logger);
            Guard.ArgumentNotNull("discoverySink", discoverySink);

            try
            {
                RemotingUtility.CleanUpRegisteredChannels();
                List<SourceSink> sourceSinks = new List<SourceSink>();

                using (AssemblyHelper.SubscribeResolve())
                    try
                    {
                        foreach (string source in sources)
                        {
                            try
                            {
                                if (cancelled)
                                    break;

                                if (IsXunitTestAssembly(source))
                                {
                                    var framework = new Xunit2(SourceInformationProvider, source, configFileName: null, shadowCopy: true);
                                    var sink = new VsDiscoveryVisitor(source, framework, logger, discoverySink, () => cancelled);
                                    sourceSinks.Add(new SourceSink { Framework = framework, Source = source, Sink = sink });
                                    framework.Find(includeSourceInformation: true, messageSink: sink);
                                }
                            }
                            catch (Exception e)
                            {
                                logger.SendMessage(TestMessageLevel.Error, String.Format("xUnit.net: Exception discovering tests from {0}: {1}", source, e));
                            }

                        }

                        foreach (var sourceSink in sourceSinks)
                            sourceSink.Sink.Finished.WaitOne();
                    }
                    finally
                    {
                        foreach (var sourceSink in sourceSinks)
                        {
                            sourceSink.Sink.Dispose();
                            sourceSink.Framework.Dispose();
                        }
                    }
            }
            catch
            {
            }
        }
Esempio n. 2
0
    public List <IMessageSinkMessage> Run(Type[] types, Func <IMessageSinkMessage, bool> cancellationThunk = null)
    {
        Xunit2 = new Xunit2(new NullSourceInformationProvider(), new Uri(types[0].Assembly.CodeBase).LocalPath, configFileName: null, shadowCopy: true);

        bool cancelled = false;
        Func <IMessageSinkMessage, bool> wrapper = msg =>
        {
            var result = true;

            if (cancellationThunk != null)
            {
                result = cancellationThunk(msg);
            }

            if (!result)
            {
                cancelled = true;
            }

            return(result);
        };

        var discoverySink = new SpyMessageSink <IDiscoveryCompleteMessage>(wrapper);

        foreach (Type type in types)
        {
            Xunit2.Find(type.FullName, includeSourceInformation: false, messageSink: discoverySink);
            discoverySink.Finished.WaitOne();
            discoverySink.Finished.Reset();
        }

        if (cancelled)
        {
            return(new List <IMessageSinkMessage>());
        }

        var testCases = discoverySink.Messages.OfType <ITestCaseDiscoveryMessage>().Select(msg => msg.TestCase).ToArray();

        var runSink = new SpyMessageSink <ITestAssemblyFinished>(cancellationThunk);

        Xunit2.Run(testCases, runSink);
        runSink.Finished.WaitOne();

        return(runSink.Messages.ToList());
    }
Esempio n. 3
0
    public List <IMessageSinkMessage> Run(Type[] types)
    {
        Xunit2 = new Xunit2(AppDomainSupport.Required, new NullSourceInformationProvider(), types[0].Assembly.GetLocalCodeBase(), configFileName: null, shadowCopy: true);

        var discoverySink = new SpyMessageSink <IDiscoveryCompleteMessage>();

        foreach (var type in types)
        {
            Xunit2.Find(type.FullName, includeSourceInformation: false, messageSink: discoverySink, discoveryOptions: TestFrameworkOptions.ForDiscovery());
            discoverySink.Finished.WaitOne();
            discoverySink.Finished.Reset();
        }

        var testCases = discoverySink.Messages.OfType <ITestCaseDiscoveryMessage>().Select(msg => msg.TestCase).ToArray();

        var runSink = new SpyMessageSink <ITestAssemblyFinished>();

        Xunit2.RunTests(testCases, runSink, TestFrameworkOptions.ForExecution());
        runSink.Finished.WaitOne();

        return(runSink.Messages.ToList());
    }
Esempio n. 4
0
 public virtual IReadOnlyList <ITestCase> Discover()
 {
     return(Discover(sink => xunit.Find(false, sink, TestFrameworkOptions.ForDiscovery(configuration))));
 }
    public static void DeserializedFactsAndTheoriesFromTheSameClassStillShareFixtures()
    {
        var code = @"
using System;
using System.Threading;
using Xunit;

public class TestClassFixture : IDisposable
{
    public static long StaticConstructorCount = 0;
    public readonly long ConstructorInstance;

    public TestClassFixture() { ConstructorInstance = Interlocked.Increment(ref StaticConstructorCount); }

    public void Dispose() { Assert.Equal(1, StaticConstructorCount); }
}

public class TestClass : IClassFixture<TestClassFixture>
{
    readonly TestClassFixture fixture;

    public TestClass(TestClassFixture fixture) { this.fixture = fixture; }

    [Fact]
    public void FactMethod() { Assert.Equal(1, fixture.ConstructorInstance); }

    [Theory]
    [InlineData(42)]
    public void TheoryMethod(int x) { Assert.Equal(1, fixture.ConstructorInstance); }
}
";

        using (var assembly = CSharpAcceptanceTestV2Assembly.Create(code))
        {
            var discoverySink       = new SpyMessageSink <IDiscoveryCompleteMessage>();
            var serializedTestCases = default(List <string>);
            var descriptors         = default(List <TestCaseDescriptor>);

            using (var xunit2 = new Xunit2(AppDomainSupport.Required, new NullSourceInformationProvider(), assembly.FileName))
            {
                xunit2.Find("TestClass", false, discoverySink, TestFrameworkOptions.ForDiscovery());
                discoverySink.Finished.WaitOne();

                var testCases = discoverySink.Messages
                                .OfType <ITestCaseDiscoveryMessage>()
                                .Select(x => x.TestCase)
                                .ToList();

                serializedTestCases = testCases.Select(x => xunit2.Serialize(x)).ToList();
                descriptors         = xunit2.GetTestCaseDescriptors(testCases, true);
            }

            using (var xunit2 = new Xunit2(AppDomainSupport.Required, new NullSourceInformationProvider(), assembly.FileName))
            {
                var deserializations = default(List <ITestCase>);
                Action <List <KeyValuePair <string, ITestCase> > > callback = r => deserializations = r.Select(x => x.Value).ToList();

                new TestCaseBulkDeserializer(xunit2, xunit2, serializedTestCases, callback);

                var executionSink = new SpyMessageSink <ITestAssemblyFinished>();
                xunit2.RunTests(deserializations, executionSink, TestFrameworkOptions.ForExecution());
                executionSink.Finished.WaitOne();

                var passedTests = executionSink.Messages.OfType <ITestPassed>().ToList();
                var failedTests = executionSink.Messages.OfType <ITestFailed>().ToList();

                Assert.Equal(2, passedTests.Count);
                Assert.Empty(failedTests);
            }
        }
    }
Esempio n. 6
0
        void RunTestsInAssembly(IFrameworkHandle frameworkHandle, List<IDisposable> toDispose, string assemblyFileName, IEnumerable<TestCase> testCases = null)
        {
            if (cancelled)
                return;

            var xunit2 = new Xunit2(SourceInformationProvider, assemblyFileName, configFileName: null, shadowCopy: true);
            toDispose.Add(xunit2);

            if (testCases == null)
            {
                using (var visitor = new TestDiscoveryVisitor())
                {
                    xunit2.Find(includeSourceInformation: true, messageSink: visitor);
                    visitor.Finished.WaitOne();
                    testCases = visitor.TestCases.Select(tc => VsDiscoveryVisitor.CreateVsTestCase(frameworkHandle, assemblyFileName, xunit2, tc)).ToList();
                }
            }

            var xunitTestCases = testCases.ToDictionary(tc => xunit2.Deserialize(tc.GetPropertyValue<string>(SerializedTestCaseProperty, null)));

            using (var executionVisitor = new VsExecutionVisitor(assemblyFileName, frameworkHandle, xunitTestCases, () => cancelled))
            {
                xunit2.Run(xunitTestCases.Keys.ToList(), executionVisitor);
                executionVisitor.Finished.WaitOne();
            }
        }
Esempio n. 7
0
 public virtual IEnumerable <ITestCase> Discover()
 {
     return(Discover(sink => xunit.Find(false, sink, new XunitDiscoveryOptions(configuration))));
 }