Exemplo n.º 1
0
        public void SingleTestMethod()
        {
            string code = @"
                using Xunit;
        
                public class Foo
                {
                    [Fact]
                    public void Bar() { }
                }
            ";

            using (var assm = AcceptanceTestV2Assembly.Create(code))
            using (var controller = new TestableXunit2(assm.FileName, null, true))
            {
                var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();

                controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());

                sink.Finished.WaitOne();

                ITestCase testCase = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Single().TestCase;
                Assert.Equal("Foo.Bar", testCase.DisplayName);
            }
        }
Exemplo n.º 2
0
        public void FactAcceptanceTest()
        {
            string code = @"
                using System;
                using Xunit;

                namespace Namespace1
                {
                    public class Class1
                    {
                        [Fact]
                        [Trait(""Name!"", ""Value!"")]
                        public void Trait() { }

                        [Fact(Skip=""Skipping"")]
                        public void Skipped() { }

                        [Fact(DisplayName=""Custom Test Name"")]
                        public void CustomName() { }
                    }
                }

                namespace Namespace2
                {
                    public class OuterClass
                    {
                        public class Class2
                        {
                            [Fact]
                            public void TestMethod() { }
                        }
                    }
                }
            ";

            using (var assembly = new AcceptanceTestAssembly(code))
            using (var controller = new TestableXunit2(assembly.FileName, null, true))
            {
                var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();

                controller.Find(includeSourceInformation: false, messageSink: sink, options: new XunitDiscoveryOptions());

                sink.Finished.WaitOne();
                ITestCase[] testCases = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase).ToArray();

                Assert.Equal(4, testCases.Length);

                ITestCase traitTest = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Trait");
                string key = Assert.Single(traitTest.Traits.Keys);
                Assert.Equal("Name!", key);
                string value = Assert.Single(traitTest.Traits[key]);
                Assert.Equal("Value!", value);

                ITestCase skipped = Assert.Single(testCases, tc => tc.DisplayName == "Namespace1.Class1.Skipped");
                Assert.Equal("Skipping", skipped.SkipReason);

                Assert.Single(testCases, tc => tc.DisplayName == "Custom Test Name");
                Assert.Single(testCases, tc => tc.DisplayName == "Namespace2.OuterClass+Class2.TestMethod");
            }
        }
        public static IEnumerable<IMessageSinkMessage> Run(this Xunit2 runner, IEnumerable<ITestCase> testCases)
        {
            Guard.AgainstNullArgument("runner", runner);

            using (var sink = new SpyMessageSink<ITestAssemblyFinished>())
            {
                runner.RunTests(testCases, sink, TestFrameworkOptions.ForExecution());
                sink.Finished.WaitOne();
                return sink.Messages.Select(message => message);
            }
        }
        public static IEnumerable<ITestCase> Find(this Xunit2Discoverer discoverer, Type type)
        {
            Guard.AgainstNullArgument("discoverer", discoverer);
            Guard.AgainstNullArgument("type", type);

            using (var sink = new SpyMessageSink<IDiscoveryCompleteMessage>())
            {
                discoverer.Find(type.FullName, false, sink, TestFrameworkOptions.ForDiscovery());
                sink.Finished.WaitOne();
                return sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(message => message.TestCase).ToArray();
            }
        }
        public static IEnumerable<ITestCase> Find(this Xunit2Discoverer discoverer, string collectionName)
        {
            Guard.AgainstNullArgument("discoverer", discoverer);

            using (var sink = new SpyMessageSink<IDiscoveryCompleteMessage>())
            {
                discoverer.Find(false, sink, TestFrameworkOptions.ForDiscovery());
                sink.Finished.WaitOne();
                return sink.Messages.OfType<ITestCaseDiscoveryMessage>()
                    .Select(message => message.TestCase)
                    .Where(message => message.TestMethod.TestClass.TestCollection.DisplayName == collectionName)
                    .ToArray();
            }
        }
Exemplo n.º 6
0
        public void NoTestMethods()
        {
            using (var assm = AcceptanceTestV2Assembly.Create(code: ""))
            using (var controller = new TestableXunit2(assm.FileName, null, true))
            {
                var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();

                controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());

                sink.Finished.WaitOne();

                Assert.False(sink.Messages.Any(msg => msg is ITestCaseDiscoveryMessage));
            }
        }
Exemplo n.º 7
0
        public void ExceptionThrownDuringClassFinish_ResultsInErrorMessage()
        {
            var testCollection = new Xunit1TestCollection("AssemblyName.dll");
            var testCases      = new[] {
                new Xunit1TestCase("assembly", "failingtype", "passingmethod", "failingtype.passingmethod")
                {
                    TestCollection = testCollection
                }
            };
            var exception = new InvalidOperationException("Cannot use a test class as its own fixture data");
            var xunit1    = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");

            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
            .When(x => x.RunTests("failingtype", Arg.Any <List <string> >(),
                                  Arg.Any <ICallbackEventHandler>()))
            .Do(callInfo =>
            {
                // Ensure the exception has a callstack
                try { throw exception; }
                catch { }
                var callback = callInfo.Arg <ICallbackEventHandler>();
                callback.RaiseCallbackEvent("<start name='failingtype.passingmethod' type='failingtype' method='passingmethod'/>");
                callback.RaiseCallbackEvent("<test name='failingtype.passingmethod' type='failingtype' method='passingmethod' result='Pass' time='1.000'/>");
                callback.RaiseCallbackEvent(string.Format("<class name='failingtype' time='0.000' total='0' passed='1' failed='1' skipped='0'><failure exception-type='Xunit.Some.Exception'><message>Cannot use a test class as its own fixture data</message><stack-trace><![CDATA[{0}]]></stack-trace></failure></class>", exception.StackTrace));
            });
            var sink = new SpyMessageSink <ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var errorMessage = Assert.Single(sink.Messages.OfType <IErrorMessage>());

            Assert.Equal("Xunit.Some.Exception", errorMessage.ExceptionTypes.Single());
            Assert.Equal("Cannot use a test class as its own fixture data", errorMessage.Messages.Single());
            Assert.Equal(exception.StackTrace, errorMessage.StackTraces.Single());
        }
Exemplo n.º 8
0
        public void NestedExceptionsThrownDuringClassStart_ResultsInErrorMessage()
        {
            var testCollection = new Xunit1TestCollection("AssemblyName.dll");
            var testCases      = new[] {
                new Xunit1TestCase("assembly", "failingtype", "passingmethod", "failingtype.passingmethod")
                {
                    TestCollection = testCollection
                }
            };
            var exception = GetNestedExceptions();
            var xunit1    = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");

            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
            .When(x => x.RunTests("failingtype", Arg.Any <List <string> >(),
                                  Arg.Any <ICallbackEventHandler>()))
            .Do(callInfo =>
            {
                var callback = callInfo.Arg <ICallbackEventHandler>();
                callback.RaiseCallbackEvent("<start name='failingtype.passingmethod' type='failingtype' method='passingmethod'/>");
                callback.RaiseCallbackEvent("<test name='failingtype.passingmethod' type='failingtype' method='passingmethod' result='Pass' time='1.000'/>");
                callback.RaiseCallbackEvent(string.Format("<class name='failingtype' time='0.000' total='0' passed='1' failed='1' skipped='0'><failure exception-type='System.InvalidOperationException'><message>{0}</message><stack-trace><![CDATA[{1}]]></stack-trace></failure></class>", GetMessage(exception), GetStackTrace(exception)));
            });
            var sink = new SpyMessageSink <ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var errorMessage = Assert.Single(sink.Messages.OfType <IErrorMessage>());

            Assert.Equal(exception.GetType().FullName, errorMessage.ExceptionTypes[0]);
            Assert.Equal(exception.InnerException.GetType().FullName, errorMessage.ExceptionTypes[1]);
            Assert.Equal(exception.Message, errorMessage.Messages[0]);
            Assert.Equal(exception.InnerException.Message, errorMessage.Messages[1]);
            Assert.Equal(exception.StackTrace, errorMessage.StackTraces[0]);
            Assert.Equal(exception.InnerException.StackTrace, errorMessage.StackTraces[1]);
        }
Exemplo n.º 9
0
        public static async void Messages()
        {
            var summary = new RunSummary {
                Total = 4, Failed = 2, Skipped = 1, Time = 21.12m
            };
            var messages      = new List <IMessageSinkMessage>();
            var messageSink   = SpyMessageSink.Create(messages: messages);
            var runner        = TestableTestAssemblyRunner.Create(messageSink, summary);
            var thisAssembly  = Assembly.GetExecutingAssembly();
            var thisAppDomain = AppDomain.CurrentDomain;

            var result = await runner.RunAsync();

            Assert.Equal(4, result.Total);
            Assert.Equal(2, result.Failed);
            Assert.Equal(1, result.Skipped);
            Assert.NotEqual(21.12m, result.Time);  // Uses clock time, not result time
            Assert.Collection(messages,
                              msg =>
            {
                var starting = Assert.IsAssignableFrom <ITestAssemblyStarting>(msg);
                Assert.Equal(thisAssembly.GetLocalCodeBase(), starting.TestAssembly.Assembly.AssemblyPath);
                Assert.Equal(thisAppDomain.SetupInformation.ConfigurationFile, starting.TestAssembly.ConfigFileName);
                Assert.InRange(starting.StartTime, DateTime.Now.AddMinutes(-15), DateTime.Now);
                Assert.Equal("The test framework environment", starting.TestEnvironment);
                Assert.Equal("The test framework display name", starting.TestFrameworkDisplayName);
            },
                              msg =>
            {
                var finished = Assert.IsAssignableFrom <ITestAssemblyFinished>(msg);
                Assert.Equal(4, finished.TestsRun);
                Assert.Equal(2, finished.TestsFailed);
                Assert.Equal(1, finished.TestsSkipped);
                Assert.Equal(result.Time, finished.ExecutionTime);
            }
                              );
        }
Exemplo n.º 10
0
        public static async ValueTask FailureInBeforeTestAssemblyFinished_ReportsCleanupFailure_DoesNotIncludeExceptionsFromAfterTestAssemblyStarting()
        {
            var thisAssembly      = Assembly.GetExecutingAssembly();
            var messages          = new List <_MessageSinkMessage>();
            var messageSink       = SpyMessageSink.Create(messages: messages);
            var testCases         = new[] { TestCaseForTestCollection() };
            var runner            = TestableTestAssemblyRunner.Create(messageSink, testCases: testCases);
            var startingException = new DivideByZeroException();
            var finishedException = new InvalidOperationException();

            runner.AfterTestAssemblyStarting_Callback  = aggregator => aggregator.Add(startingException);
            runner.BeforeTestAssemblyFinished_Callback = aggregator => aggregator.Add(finishedException);

            await runner.RunAsync();

            var assemblyStarting = Assert.Single(messages.OfType <_TestAssemblyStarting>());
            var cleanupFailure   = Assert.Single(messages.OfType <_TestAssemblyCleanupFailure>());

#if NETFRAMEWORK
            Assert.Equal(thisAssembly.GetLocalCodeBase(), assemblyStarting.AssemblyPath);
            Assert.Equal(runner.TestAssembly.ConfigFileName, assemblyStarting.ConfigFilePath);
#endif
            Assert.Equal(typeof(InvalidOperationException).FullName, cleanupFailure.ExceptionTypes.Single());
        }
Exemplo n.º 11
0
        public static async void TestCaseOrdererWhichThrowsLogsMessageAndDoesNotReorderTests()
        {
            var spy = SpyMessageSink.Capture();

            TestContext.Current !.DiagnosticMessageSink = spy;
            var passing1 = Mocks.TestCase <ClassUnderTest>("Passing");
            var passing2 = Mocks.TestCase <ClassUnderTest>("Passing");
            var other1   = Mocks.TestCase <ClassUnderTest>("Other");
            var other2   = Mocks.TestCase <ClassUnderTest>("Other");
            var runner   = TestableTestClassRunner.Create(testCases: new[] { passing1, other1, passing2, other2 }, orderer: new ThrowingOrderer());

            await runner.RunAsync();

            Assert.Collection(
                runner.MethodsRun,
                tuple =>
            {
                Assert.Equal("Passing", tuple.Item1?.Name);
                Assert.Collection(tuple.Item2,
                                  testCase => Assert.Same(passing1, testCase),
                                  testCase => Assert.Same(passing2, testCase)
                                  );
            },
                tuple =>
            {
                Assert.Equal("Other", tuple.Item1?.Name);
                Assert.Collection(tuple.Item2,
                                  testCase => Assert.Same(other1, testCase),
                                  testCase => Assert.Same(other2, testCase)
                                  );
            }
                );
            var diagnosticMessage = Assert.Single(spy.Messages.Cast <_DiagnosticMessage>());

            Assert.StartsWith("Test case orderer 'TestClassRunnerTests+TestCaseOrderer+ThrowingOrderer' threw 'System.DivideByZeroException' during ordering: Attempted to divide by zero.", diagnosticMessage.Message);
        }
Exemplo n.º 12
0
    public Task <List <_MessageSinkMessage> > RunAsync(
        Type[] types,
        bool preEnumerateTheories                   = true,
        _IMessageSink?diagnosticMessageSink         = null,
        _IMessageSink?internalDiagnosticMessageSink = null,
        params _IReflectionAttributeInfo[] additionalAssemblyAttributes)
    {
        var tcs = new TaskCompletionSource <List <_MessageSinkMessage> >();

        ThreadPool.QueueUserWorkItem(async _ =>
        {
            try
            {
                TestContext.SetForInitialization(diagnosticMessageSink, internalDiagnosticMessageSink);

                await using var testFramework = new XunitTestFramework();

                var assemblyInfo = Reflector.Wrap(Assembly.GetEntryAssembly() !, additionalAssemblyAttributes);
                var discoverer   = testFramework.GetDiscoverer(assemblyInfo);
                var testCases    = new List <_ITestCase>();
                await discoverer.Find(testCase => { testCases.Add(testCase); return(new(true)); }, _TestFrameworkOptions.ForDiscovery(preEnumerateTheories: preEnumerateTheories), types);

                using var runSink = SpyMessageSink <_TestAssemblyFinished> .Create();
                var executor      = testFramework.GetExecutor(assemblyInfo);
                await executor.RunTestCases(testCases, runSink, _TestFrameworkOptions.ForExecution());

                tcs.TrySetResult(runSink.Messages.ToList());
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }
        });

        return(tcs.Task);
    }
Exemplo n.º 13
0
    public void CanDeserializeGeneralizedSerialization()
    {
        var discoverySink = new SpyMessageSink <IDiscoveryCompleteMessage>();

        discoverer.Find("TestCaseBulkDeserializerTests+TestClass", false, discoverySink, TestFrameworkOptions.ForDiscovery());
        discoverySink.Finished.WaitOne();
        var serializedTestCases = discoverySink.Messages
                                  .OfType <ITestCaseDiscoveryMessage>()
                                  .Where(m => m.TestCase.TestMethod.Method.Name == "TheoryMethod")
                                  .Select(m => discoverer.Serialize(m.TestCase))
                                  .ToList();

        var results = default(List <KeyValuePair <string, ITestCase> >);
        Action <List <KeyValuePair <string, ITestCase> > > callback = r => results = r;

        new TestCaseBulkDeserializer(discoverer, executor, serializedTestCases, callback);

        var kvp = Assert.Single(results);

        Assert.Equal(kvp.Value.UniqueID, kvp.Key);
        Assert.Equal("TestCaseBulkDeserializerTests+TestClass", kvp.Value.TestMethod.TestClass.Class.Name);
        Assert.Equal("TheoryMethod", kvp.Value.TestMethod.Method.Name);
        Assert.Equal("TestCaseBulkDeserializerTests+TestClass.TheoryMethod(x: 42)", kvp.Value.DisplayName);
    }
Exemplo n.º 14
0
        public void NestedExceptionResultFromTests_ResultsInErrorMessage()
        {
            var testCollection = new Xunit1TestCollection("AssemblyName.dll");
            var testCases      = new[] {
                new Xunit1TestCase("assembly", "type1", "failing", "type1.failing")
                {
                    TestCollection = testCollection
                }
            };
            var exception = GetNestedExceptions();
            var xunit1    = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");

            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
            .WhenForAnyArgs(x => x.RunTests(null, null, null))
            .Do(callInfo =>
            {
                var callback = callInfo.Arg <ICallbackEventHandler>();
                callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                callback.RaiseCallbackEvent(string.Format("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='{0}'><message>{1}</message><stack-trace><![CDATA[{2}]]></stack-trace></failure></test>", exception.GetType().FullName, GetMessage(exception), GetStackTrace(exception)));
                callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='1' failed='1' skipped='0'/>");
            });
            var sink = new SpyMessageSink <ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var testFailed = Assert.Single(sink.Messages.OfType <ITestFailed>());

            Assert.Equal(exception.GetType().FullName, testFailed.ExceptionTypes[0]);
            Assert.Equal(exception.InnerException.GetType().FullName, testFailed.ExceptionTypes[1]);
            Assert.Equal(exception.Message, testFailed.Messages[0]);
            Assert.Equal(exception.InnerException.Message, testFailed.Messages[1]);
            Assert.Equal(exception.StackTrace, testFailed.StackTraces[0]);
            Assert.Equal(exception.InnerException.StackTrace, testFailed.StackTraces[1]);
        }
Exemplo n.º 15
0
        public void SingleTestMethod()
        {
            string code = @"
using Xunit;

public class Foo
{
    [Fact]
    public void Bar() { }
}";

            using (var assm = CSharpAcceptanceTestV2Assembly.Create(code))
                using (var controller = new TestableXunit2(assm.FileName, null, true))
                {
                    var sink = new SpyMessageSink <IDiscoveryCompleteMessage>();

                    controller.Find(includeSourceInformation: false, messageSink: sink, discoveryOptions: TestFrameworkOptions.ForDiscovery());

                    sink.Finished.WaitOne();

                    ITestCase testCase = sink.Messages.OfType <ITestCaseDiscoveryMessage>().Single().TestCase;
                    Assert.Equal("Foo.Bar", testCase.DisplayName);
                }
        }
Exemplo n.º 16
0
    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);
            }
        }
    }
Exemplo n.º 17
0
 public TestFrameworkProxyTests()
 {
     diagnosticSpy = SpyMessageSink.Create(messages: diagnosticMessages);
 }
Exemplo n.º 18
0
 public static TestableFactDiscoverer Create() =>
 new TestableFactDiscoverer(SpyMessageSink.Create());
Exemplo n.º 19
0
        public void SupportsAsyncReturningMethods()
        {
            string code = @"
module FSharpTests

open Xunit

[<Fact>]
let AsyncFailing() =
    async {
        do! Async.Sleep(10)
        Assert.True(false)
    }
";

            using (var assembly = FSharpAcceptanceTestV2Assembly.Create(code))
            using (var controller = new TestableXunit2(assembly.FileName, null, true))
            {
                var sink = new SpyMessageSink<ITestAssemblyFinished>();

                controller.RunAll(sink, discoveryOptions: TestFrameworkOptions.ForDiscovery(), executionOptions: TestFrameworkOptions.ForExecution());
                sink.Finished.WaitOne();

                var failures = sink.Messages.OfType<ITestFailed>();
                var failure = Assert.Single(failures);
                Assert.Equal("FSharpTests.AsyncFailing", failure.TestCase.DisplayName);
            }
        }
Exemplo n.º 20
0
        public void NestedExceptionResultFromTests_ResultsInErrorMessage()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "type1", "failing", "type1.failing")
            };
            var exception = GetNestedExceptions();
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .WhenForAnyArgs(x => x.RunTests(null, null, null))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                      callback.RaiseCallbackEvent(string.Format("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='{0}'><message>{1}</message><stack-trace><![CDATA[{2}]]></stack-trace></failure></test>", exception.GetType().FullName, GetMessage(exception), GetStackTrace(exception)));
                      callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='1' failed='1' skipped='0'/>");
                  });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var testFailed = Assert.Single(sink.Messages.OfType<ITestFailed>());
            Assert.Equal(exception.GetType().FullName, testFailed.ExceptionTypes[0]);
            Assert.Equal(exception.InnerException.GetType().FullName, testFailed.ExceptionTypes[1]);
            Assert.Equal(exception.Message, testFailed.Messages[0]);
            Assert.Equal(exception.InnerException.Message, testFailed.Messages[1]);
            Assert.Equal(exception.StackTrace, testFailed.StackTraces[0]);
            Assert.Equal(exception.InnerException.StackTrace, testFailed.StackTraces[1]);
        }
Exemplo n.º 21
0
        public void NestedExceptionsThrownDuringClassStart_ResultsInErrorMessage()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "failingtype", "passingmethod", "failingtype.passingmethod")
            };
            var exception = GetNestedExceptions();
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .When(x => x.RunTests("failingtype", Arg.Any<List<string>>(),
                    Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='failingtype.passingmethod' type='failingtype' method='passingmethod'/>");
                      callback.RaiseCallbackEvent("<test name='failingtype.passingmethod' type='failingtype' method='passingmethod' result='Pass' time='1.000'/>");
                      callback.RaiseCallbackEvent(string.Format("<class name='failingtype' time='0.000' total='0' passed='1' failed='1' skipped='0'><failure exception-type='System.InvalidOperationException'><message>{0}</message><stack-trace><![CDATA[{1}]]></stack-trace></failure></class>", GetMessage(exception), GetStackTrace(exception)));
                  });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var errorMessage = Assert.Single(sink.Messages.OfType<IErrorMessage>());
            Assert.Equal(exception.GetType().FullName, errorMessage.ExceptionTypes[0]);
            Assert.Equal(exception.InnerException.GetType().FullName, errorMessage.ExceptionTypes[1]);
            Assert.Equal(exception.Message, errorMessage.Messages[0]);
            Assert.Equal(exception.InnerException.Message, errorMessage.Messages[1]);
            Assert.Equal(exception.StackTrace, errorMessage.StackTraces[0]);
            Assert.Equal(exception.InnerException.StackTrace, errorMessage.StackTraces[1]);
        }
Exemplo n.º 22
0
    public void BusShouldReportShutdownWhenMessageSinkReturnsFalse()
    {
        using var bus = new SynchronousMessageBus(SpyMessageSink.Create(returnResult: false));

        Assert.False(bus.QueueMessage(new _MessageSinkMessage()));
    }
Exemplo n.º 23
0
        public void RunWithTestCases()
        {
            var testCollection = new Xunit1TestCollection("AssemblyName.dll");
            var testCases = new[] {
                new Xunit1TestCase("assembly", "type1", "passing", "type1.passing") { TestCollection = testCollection },
                new Xunit1TestCase("assembly", "type1", "failing", "type1.failing") { TestCollection = testCollection },
                new Xunit1TestCase("assembly", "type2", "skipping", "type2.skipping") { TestCollection = testCollection }
            };
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .When(x => x.RunTests("type1", Arg.Any<List<string>>(), Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='type1.passing' type='type1' method='passing'/>");
                      callback.RaiseCallbackEvent("<test name='type1.passing' type='type1' method='passing' result='Pass' time='1.000'/>");
                      callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                      callback.RaiseCallbackEvent("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='Xunit.MockFailureException'><message>Failure message</message><stack-trace>Stack trace</stack-trace></failure></test>");
                      callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='2' failed='1' skipped='0'/>");
                  });
            xunit1.Executor
                  .When(x => x.RunTests("type2", Arg.Any<List<string>>(), Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='type2.skipping' type='type2' method='skipping'/>");
                      callback.RaiseCallbackEvent("<test name='type2.skipping' type='type2' method='skipping' result='Skip'><reason><message>Skip message</message></reason></test>");
                      callback.RaiseCallbackEvent("<class name='type2' time='0.000' total='1' failed='0' skipped='1'/>");
                  });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            Assert.Collection(sink.Messages,
                message =>
                {
                    var assemblyStarting = Assert.IsAssignableFrom<ITestAssemblyStarting>(message);
                    Assert.Equal("AssemblyName.dll", assemblyStarting.AssemblyFileName);
                    Assert.Equal("ConfigFile.config", assemblyStarting.ConfigFileName);
                    Assert.Contains("-bit .NET ", assemblyStarting.TestEnvironment);
                    Assert.Equal("Test framework display name", assemblyStarting.TestFrameworkDisplayName);
                },
                message =>
                {
                    var testCollectionStarting = Assert.IsAssignableFrom<ITestCollectionStarting>(message);
                    Assert.Same(testCollection, testCollectionStarting.TestCollection);
                },
                message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom<ITestClassStarting>(message);
                    Assert.Equal("type1", testClassStarting.ClassName);
                    Assert.Same(testCollection, testClassStarting.TestCollection);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type1.passing", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type1.passing", testStarting.TestCase.DisplayName);
                    Assert.Equal("type1", testStarting.TestCase.Class.Name);
                    Assert.Equal("passing", testStarting.TestCase.Method.Name);
                    Assert.Same(testCollection, testStarting.TestCase.TestCollection);
                },
                message =>
                {
                    var testPassed = Assert.IsAssignableFrom<ITestPassed>(message);
                    Assert.Equal("type1.passing", testPassed.TestCase.DisplayName);
                    Assert.Equal(1M, testPassed.ExecutionTime);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type1.passing", testFinished.TestCase.DisplayName);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type1.passing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(1M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type1.failing", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type1.failing", testStarting.TestCase.DisplayName);
                    Assert.Equal("type1", testStarting.TestCase.Class.Name);
                    Assert.Equal("failing", testStarting.TestCase.Method.Name);
                    Assert.Same(testCollection, testStarting.TestCase.TestCollection);
                },
                message =>
                {
                    var testFailed = Assert.IsAssignableFrom<ITestFailed>(message);
                    Assert.Equal("type1.failing", testFailed.TestCase.DisplayName);
                    Assert.Equal(0.234M, testFailed.ExecutionTime);
                    Assert.Equal("Xunit.MockFailureException", testFailed.ExceptionType);
                    Assert.Equal("Failure message", testFailed.Message);
                    Assert.Equal("Stack trace", testFailed.StackTrace);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type1.failing", testFinished.TestCase.DisplayName);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type1.failing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0.234M, testCaseFinished.ExecutionTime);
                    Assert.Equal(1, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom<ITestClassFinished>(message);
                    Assert.Equal("type1", testClassFinished.ClassName);
                    Assert.Equal(1.234M, testClassFinished.ExecutionTime);
                    Assert.Equal(1, testClassFinished.TestsFailed);
                    Assert.Equal(2, testClassFinished.TestsRun);
                    Assert.Equal(0, testClassFinished.TestsSkipped);
                },
                message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom<ITestClassStarting>(message);
                    Assert.Equal("type2", testClassStarting.ClassName);
                    Assert.Same(testCollection, testClassStarting.TestCollection);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type2.skipping", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type2.skipping", testStarting.TestCase.DisplayName);
                    Assert.Equal("type2", testStarting.TestCase.Class.Name);
                    Assert.Equal("skipping", testStarting.TestCase.Method.Name);
                    Assert.Same(testCollection, testStarting.TestCase.TestCollection);
                },
                message =>
                {
                    var testSkipped = Assert.IsAssignableFrom<ITestSkipped>(message);
                    Assert.Equal("type2.skipping", testSkipped.TestCase.DisplayName);
                    Assert.Equal(0M, testSkipped.ExecutionTime);
                    Assert.Equal("Skip message", testSkipped.Reason);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type2.skipping", testFinished.TestCase.DisplayName);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type2.skipping", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(1, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom<ITestClassFinished>(message);
                    Assert.Equal("type2", testClassFinished.ClassName);
                    Assert.Equal(0M, testClassFinished.ExecutionTime);
                    Assert.Equal(0, testClassFinished.TestsFailed);
                    Assert.Equal(1, testClassFinished.TestsRun);
                    Assert.Equal(1, testClassFinished.TestsSkipped);
                },
                message =>
                {
                    var testCollectionFinished = Assert.IsAssignableFrom<ITestCollectionFinished>(message);
                    Assert.Equal(1.234M, testCollectionFinished.ExecutionTime);
                    Assert.Equal(1, testCollectionFinished.TestsFailed);
                    Assert.Equal(3, testCollectionFinished.TestsRun);
                    Assert.Equal(1, testCollectionFinished.TestsSkipped);
                    Assert.Same(testCollection, testCollectionFinished.TestCollection);
                },
                message =>
                {
                    var assemblyFinished = Assert.IsAssignableFrom<ITestAssemblyFinished>(message);
                    Assert.Equal(1.234M, assemblyFinished.ExecutionTime);
                    Assert.Equal(1, assemblyFinished.TestsFailed);
                    Assert.Equal(3, assemblyFinished.TestsRun);
                    Assert.Equal(1, assemblyFinished.TestsSkipped);
                }
            );
        }
Exemplo n.º 24
0
    public static void TheoryWithNonSerializableData_ReturnsAsASingleTestCase()
    {
        var sourceProvider = new NullSourceInformationProvider();
        var assemblyInfo   = Reflector.Wrap(Assembly.GetExecutingAssembly());
        var discoverer     = new XunitTestFrameworkDiscoverer(assemblyInfo, sourceProvider, SpyMessageSink.Create());
        var visitor        = new TestDiscoveryVisitor();

        discoverer.Find(typeof(ClassWithNonSerializableTheoryData).FullName, false, visitor, TestFrameworkOptions.ForDiscovery());
        visitor.Finished.WaitOne();

        var testCase = Assert.Single(visitor.TestCases);

        Assert.IsType <XunitTheoryTestCase>(testCase);

        var deserialized = SerializationHelper.Deserialize <ITestCase>(SerializationHelper.Serialize(testCase));

        Assert.IsType <XunitTheoryTestCase>(deserialized);
    }
Exemplo n.º 25
0
    public static void ReturnsDefaultTestCollectionForUndecoratedTestClass()
    {
        var type1            = Mocks.TypeInfo("type1");
        var type2            = Mocks.TypeInfo("type2");
        var assemblyFileName = Path.DirectorySeparatorChar == '/' ? "/foo/bar.dll" : @"C:\Foo\bar.dll";
        var assembly         = Mocks.TestAssembly(assemblyFileName);
        var factory          = new CollectionPerAssemblyTestCollectionFactory(assembly, SpyMessageSink.Create());

        var result1 = factory.Get(type1);
        var result2 = factory.Get(type2);

        Assert.Same(result1, result2);
        Assert.Equal("Test collection for bar.dll", result1.DisplayName);
    }
Exemplo n.º 26
0
        public void RunWithTestCases()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "type1", "passing", "type1.passing"),
                new Xunit1TestCase("assembly", "config", "type1", "failing", "type1.failing"),
                new Xunit1TestCase("assembly", "config", "type2", "skipping", "type2.skipping"),
                new Xunit1TestCase("assembly", "config", "type2", "skipping_with_start", "type2.skipping_with_start")
            };
            var xunit1 = new TestableXunit1();
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .When(x => x.RunTests("type1", Arg.Any<List<string>>(), Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='type1.passing' type='type1' method='passing'/>");
                      callback.RaiseCallbackEvent("<test name='type1.passing' type='type1' method='passing' result='Pass' time='1.000'/>");
                      callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                      callback.RaiseCallbackEvent("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='Xunit.MockFailureException'><message>Failure message</message><stack-trace>Stack trace</stack-trace></failure></test>");
                      callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='2' failed='1' skipped='0'/>");
                  });
            xunit1.Executor
                  .When(x => x.RunTests("type2", Arg.Any<List<string>>(), Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      // Note. Skip does not send a start packet, unless you use a custom Fact
                      callback.RaiseCallbackEvent("<test name='type2.skipping' type='type2' method='skipping' result='Skip'><reason><message>Skip message</message></reason></test>");
                      callback.RaiseCallbackEvent("<start name='type2.skipping_with_start' type='type2' method='skipping_with_start'/>");
                      callback.RaiseCallbackEvent("<test name='type2.skipping_with_start' type='type2' method='skipping_with_start' result='Skip'><reason><message>Skip message</message></reason></test>");
                      callback.RaiseCallbackEvent("<class name='type2' time='0.000' total='1' failed='0' skipped='1'/>");
                  });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var firstTestCase = testCases[0];
            var testCollection = firstTestCase.TestMethod.TestClass.TestCollection;
            var testAssembly = testCollection.TestAssembly;
            Assert.Collection(sink.Messages,
                message =>
                {
                    var assemblyStarting = Assert.IsAssignableFrom<ITestAssemblyStarting>(message);
                    Assert.Same(testAssembly, assemblyStarting.TestAssembly);
                    Assert.Contains("-bit .NET ", assemblyStarting.TestEnvironment);
                    Assert.Equal("Test framework display name", assemblyStarting.TestFrameworkDisplayName);
                    Assert.Equal(testCases, assemblyStarting.TestCases);

                    Assert.Equal("assembly", assemblyStarting.TestAssembly.Assembly.AssemblyPath);
                    Assert.Equal("config", assemblyStarting.TestAssembly.ConfigFileName);
                },
                message =>
                {
                    var testCollectionStarting = Assert.IsAssignableFrom<ITestCollectionStarting>(message);
                    Assert.Same(testAssembly, testCollectionStarting.TestAssembly);
                    Assert.Same(testCollection, testCollectionStarting.TestCollection);
                    Assert.Equal(testCases, testCollectionStarting.TestCases);

                    Assert.Equal(Guid.Empty, testCollection.UniqueID);
                    Assert.Equal("xUnit.net v1 Tests for assembly", testCollection.DisplayName);
                    Assert.Null(testCollection.CollectionDefinition);
                },
                message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom<ITestClassStarting>(message);
                    Assert.Same(testAssembly, testClassStarting.TestAssembly);
                    Assert.Same(testCollection, testClassStarting.TestCollection);
                    Assert.Equal("type1", testClassStarting.TestClass.Class.Name);
                    Assert.Collection(testClassStarting.TestCases,
                        testCase => Assert.Same(testCases[0], testCase),
                        testCase => Assert.Same(testCases[1], testCase)
                    );
                },
                message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom<ITestMethodStarting>(message);
                    Assert.Same(testAssembly, testMethodStarting.TestAssembly);
                    Assert.Same(testCollection, testMethodStarting.TestCollection);
                    Assert.Equal("type1", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testMethodStarting.TestMethod.Method.Name);
                    Assert.Same(testCases[0], testMethodStarting.TestCases.Single());
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Same(testAssembly, testCaseStarting.TestAssembly);
                    Assert.Same(testCollection, testCaseStarting.TestCollection);
                    Assert.Equal("type1", testCaseStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testCaseStarting.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Same(testAssembly, testStarting.TestAssembly);
                    Assert.Same(testCollection, testStarting.TestCollection);
                    Assert.Equal("type1", testStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testStarting.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testPassed = Assert.IsAssignableFrom<ITestPassed>(message);
                    Assert.Same(testAssembly, testPassed.TestAssembly);
                    Assert.Same(testCollection, testPassed.TestCollection);
                    Assert.Equal("type1", testPassed.TestClass.Class.Name);
                    Assert.Equal("passing", testPassed.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testPassed.TestCase.DisplayName);
                    Assert.Equal(1M, testPassed.ExecutionTime);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Same(testAssembly, testFinished.TestAssembly);
                    Assert.Same(testCollection, testFinished.TestCollection);
                    Assert.Equal("type1", testFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testFinished.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testFinished.TestCase.DisplayName);
                    Assert.Equal(1M, testFinished.ExecutionTime);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Same(testAssembly, testCaseFinished.TestAssembly);
                    Assert.Same(testCollection, testCaseFinished.TestCollection);
                    Assert.Equal("type1", testCaseFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testCaseFinished.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(1M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom<ITestMethodFinished>(message);
                    Assert.Same(testAssembly, testMethodFinished.TestAssembly);
                    Assert.Same(testCollection, testMethodFinished.TestCollection);
                    Assert.Equal("type1", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testMethodFinished.TestMethod.Method.Name);
                    Assert.Equal(1M, testMethodFinished.ExecutionTime);
                    Assert.Equal(0, testMethodFinished.TestsFailed);
                    Assert.Equal(1, testMethodFinished.TestsRun);
                    Assert.Equal(0, testMethodFinished.TestsSkipped);
                },
                message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom<ITestMethodStarting>(message);
                    Assert.Equal("type1", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("failing", testMethodStarting.TestMethod.Method.Name);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type1.failing", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type1.failing", testStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testFailed = Assert.IsAssignableFrom<ITestFailed>(message);
                    Assert.Equal("type1.failing", testFailed.TestCase.DisplayName);
                    Assert.Equal(0.234M, testFailed.ExecutionTime);
                    Assert.Equal("Xunit.MockFailureException", testFailed.ExceptionTypes.Single());
                    Assert.Equal("Failure message", testFailed.Messages.Single());
                    Assert.Equal("Stack trace", testFailed.StackTraces.Single());
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type1.failing", testFinished.TestCase.DisplayName);
                    Assert.Equal(0.234M, testFinished.ExecutionTime);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type1.failing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0.234M, testCaseFinished.ExecutionTime);
                    Assert.Equal(1, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom<ITestMethodFinished>(message);
                    Assert.Equal("type1", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("failing", testMethodFinished.TestMethod.Method.Name);
                },
                message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom<ITestClassFinished>(message);
                    Assert.Equal("type1", testClassFinished.TestClass.Class.Name);
                    Assert.Equal(1.234M, testClassFinished.ExecutionTime);
                    Assert.Equal(1, testClassFinished.TestsFailed);
                    Assert.Equal(2, testClassFinished.TestsRun);
                    Assert.Equal(0, testClassFinished.TestsSkipped);
                },
                message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom<ITestClassStarting>(message);
                    Assert.Equal("type2", testClassStarting.TestClass.Class.Name);
                },
                message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom<ITestMethodStarting>(message);
                    Assert.Equal("type2", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("skipping", testMethodStarting.TestMethod.Method.Name);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type2.skipping", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type2.skipping", testStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testSkipped = Assert.IsAssignableFrom<ITestSkipped>(message);
                    Assert.Equal("type2.skipping", testSkipped.TestCase.DisplayName);
                    Assert.Equal(0M, testSkipped.ExecutionTime);
                    Assert.Equal("Skip message", testSkipped.Reason);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type2.skipping", testFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testFinished.ExecutionTime);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type2.skipping", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(1, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom<ITestMethodFinished>(message);
                    Assert.Equal("type2", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("skipping", testMethodFinished.TestMethod.Method.Name);
                },
                message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom<ITestMethodStarting>(message);
                    Assert.Equal("type2", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("skipping_with_start", testMethodStarting.TestMethod.Method.Name);
                },
                message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom<ITestCaseStarting>(message);
                    Assert.Equal("type2.skipping_with_start", testCaseStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testStarting = Assert.IsAssignableFrom<ITestStarting>(message);
                    Assert.Equal("type2.skipping_with_start", testStarting.TestCase.DisplayName);
                },
                message =>
                {
                    var testSkipped = Assert.IsAssignableFrom<ITestSkipped>(message);
                    Assert.Equal("type2.skipping_with_start", testSkipped.TestCase.DisplayName);
                    Assert.Equal(0M, testSkipped.ExecutionTime);
                    Assert.Equal("Skip message", testSkipped.Reason);
                },
                message =>
                {
                    var testFinished = Assert.IsAssignableFrom<ITestFinished>(message);
                    Assert.Equal("type2.skipping_with_start", testFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testFinished.ExecutionTime);
                },
                message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom<ITestCaseFinished>(message);
                    Assert.Equal("type2.skipping_with_start", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(1, testCaseFinished.TestsSkipped);
                },
                message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom<ITestMethodFinished>(message);
                    Assert.Equal("type2", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("skipping_with_start", testMethodFinished.TestMethod.Method.Name);
                },
                message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom<ITestClassFinished>(message);
                    Assert.Equal("type2", testClassFinished.TestClass.Class.Name);
                    Assert.Equal(0M, testClassFinished.ExecutionTime);
                    Assert.Equal(0, testClassFinished.TestsFailed);
                    Assert.Equal(1, testClassFinished.TestsRun);
                    Assert.Equal(1, testClassFinished.TestsSkipped);
                },
                message =>
                {
                    var testCollectionFinished = Assert.IsAssignableFrom<ITestCollectionFinished>(message);
                    Assert.Equal(1.234M, testCollectionFinished.ExecutionTime);
                    Assert.Equal(1, testCollectionFinished.TestsFailed);
                    Assert.Equal(3, testCollectionFinished.TestsRun);
                    Assert.Equal(1, testCollectionFinished.TestsSkipped);
                    Assert.Same(testCollection, testCollectionFinished.TestCollection);
                },
                message =>
                {
                    var assemblyFinished = Assert.IsAssignableFrom<ITestAssemblyFinished>(message);
                    Assert.Equal(1.234M, assemblyFinished.ExecutionTime);
                    Assert.Equal(1, assemblyFinished.TestsFailed);
                    Assert.Equal(3, assemblyFinished.TestsRun);
                    Assert.Equal(1, assemblyFinished.TestsSkipped);
                }
            );
        }
        public static void TheoryWithNonSerializableData_ReturnsAsASingleTestCase()
        {
            var assemblyInfo = Reflector.Wrap(Assembly.GetExecutingAssembly());
            var discoverer   = new XunitTestFrameworkDiscoverer(assemblyInfo, configFileName: null, _NullSourceInformationProvider.Instance, SpyMessageSink.Create());
            var sink         = new TestDiscoverySink();

            discoverer.Find(typeof(ClassWithNonSerializableTheoryData).FullName !, sink, _TestFrameworkOptions.ForDiscovery());
            sink.Finished.WaitOne();

            Assert.Single(sink.TestCases);
        }
Exemplo n.º 28
0
        public void NestedExceptionsThrownDuringRunTests_ResultsInErrorMessage()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "type1", "passing", "type1.passing")
            };
            var exception = GetNestedExceptions();
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .WhenForAnyArgs(x => x.RunTests(null, null, null))
                  .Do(callInfo => { throw exception; });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var errorMessage = Assert.Single(sink.Messages.OfType<IErrorMessage>());
            Assert.Equal(exception.GetType().FullName, errorMessage.ExceptionTypes[0]);
            Assert.Equal(exception.InnerException.GetType().FullName, errorMessage.ExceptionTypes[1]);
            Assert.Equal(exception.Message, errorMessage.Messages[0]);
            Assert.Equal(exception.InnerException.Message, errorMessage.Messages[1]);
            Assert.Equal(exception.StackTrace, errorMessage.StackTraces[0]);
            Assert.Equal(exception.InnerException.StackTrace, errorMessage.StackTraces[1]);
        }
Exemplo n.º 29
0
 public static TestableTheoryDiscoverer Create()
 {
     return(new TestableTheoryDiscoverer(SpyMessageSink.Create()));
 }
Exemplo n.º 30
0
        public void ExceptionThrownDuringClassFinish_ResultsInErrorMessage()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "failingtype", "passingmethod", "failingtype.passingmethod")
            };
            var exception = new InvalidOperationException("Cannot use a test class as its own fixture data");
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");
            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
                  .When(x => x.RunTests("failingtype", Arg.Any<List<string>>(),
                    Arg.Any<ICallbackEventHandler>()))
                  .Do(callInfo =>
                  {
                      // Ensure the exception has a callstack
                      try { throw exception; }
                      catch { }
                      var callback = callInfo.Arg<ICallbackEventHandler>();
                      callback.RaiseCallbackEvent("<start name='failingtype.passingmethod' type='failingtype' method='passingmethod'/>");
                      callback.RaiseCallbackEvent("<test name='failingtype.passingmethod' type='failingtype' method='passingmethod' result='Pass' time='1.000'/>");
                      callback.RaiseCallbackEvent(string.Format("<class name='failingtype' time='0.000' total='0' passed='1' failed='1' skipped='0'><failure exception-type='Xunit.Some.Exception'><message>Cannot use a test class as its own fixture data</message><stack-trace><![CDATA[{0}]]></stack-trace></failure></class>", exception.StackTrace));
                  });
            var sink = new SpyMessageSink<ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            var errorMessage = Assert.Single(sink.Messages.OfType<IErrorMessage>());
            Assert.Equal("Xunit.Some.Exception", errorMessage.ExceptionTypes.Single());
            Assert.Equal("Cannot use a test class as its own fixture data", errorMessage.Messages.Single());
            Assert.Equal(exception.StackTrace, errorMessage.StackTraces.Single());
        }
Exemplo n.º 31
0
    public static void TheoriesWithSerializableData_ReturnAsIndividualTestCases()
    {
        var sourceProvider = new NullSourceInformationProvider();
        var assemblyInfo   = Reflector.Wrap(Assembly.GetExecutingAssembly());
        var discoverer     = new XunitTestFrameworkDiscoverer(assemblyInfo, sourceProvider, SpyMessageSink.Create());
        var visitor        = new TestDiscoveryVisitor();

        discoverer.Find(typeof(ClassWithTheory).FullName, false, visitor, TestFrameworkOptions.ForDiscovery());
        visitor.Finished.WaitOne();

        var first  = visitor.TestCases[0];
        var second = visitor.TestCases[1];

        Assert.NotEqual(first.UniqueID, second.UniqueID);

        Assert.True(TestCollectionComparer.Instance.Equals(first.TestMethod.TestClass.TestCollection, second.TestMethod.TestClass.TestCollection));

        var serializedFirst  = SerializationHelper.Deserialize <ITestCase>(SerializationHelper.Serialize(first));
        var serializedSecond = SerializationHelper.Deserialize <ITestCase>(SerializationHelper.Serialize(second));

        Assert.NotSame(serializedFirst.TestMethod.TestClass.TestCollection, serializedSecond.TestMethod.TestClass.TestCollection);
        Assert.True(TestCollectionComparer.Instance.Equals(serializedFirst.TestMethod.TestClass.TestCollection, serializedSecond.TestMethod.TestClass.TestCollection));
    }
Exemplo n.º 32
0
        public void RunWithTestCases()
        {
            var testCases = new[] {
                new Xunit1TestCase("assembly", "config", "type1", "passing", "type1.passing"),
                new Xunit1TestCase("assembly", "config", "type1", "failing", "type1.failing"),
                new Xunit1TestCase("assembly", "config", "type2", "skipping", "type2.skipping"),
                new Xunit1TestCase("assembly", "config", "type2", "skipping_with_start", "type2.skipping_with_start")
            };

            using (var xunit1 = new TestableXunit1())
            {
                xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
                xunit1.Executor
                .When(x => x.RunTests("type1", Arg.Any <List <string> >(), Arg.Any <ICallbackEventHandler>()))
                .Do(callInfo =>
                {
                    var callback = callInfo.Arg <ICallbackEventHandler>();
                    callback.RaiseCallbackEvent("<start name='type1.passing' type='type1' method='passing'/>");
                    callback.RaiseCallbackEvent("<test name='type1.passing' type='type1' method='passing' result='Pass' time='1.000'/>");
                    callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                    callback.RaiseCallbackEvent("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='Xunit.MockFailureException'><message>Failure message</message><stack-trace>Stack trace</stack-trace></failure></test>");
                    callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='2' failed='1' skipped='0'/>");
                });
                xunit1.Executor
                .When(x => x.RunTests("type2", Arg.Any <List <string> >(), Arg.Any <ICallbackEventHandler>()))
                .Do(callInfo =>
                {
                    var callback = callInfo.Arg <ICallbackEventHandler>();
                    // Note. Skip does not send a start packet, unless you use a custom Fact
                    callback.RaiseCallbackEvent("<test name='type2.skipping' type='type2' method='skipping' result='Skip'><reason><message>Skip message</message></reason></test>");
                    callback.RaiseCallbackEvent("<start name='type2.skipping_with_start' type='type2' method='skipping_with_start'/>");
                    callback.RaiseCallbackEvent("<test name='type2.skipping_with_start' type='type2' method='skipping_with_start' result='Skip'><reason><message>Skip message</message></reason></test>");
                    callback.RaiseCallbackEvent("<class name='type2' time='0.000' total='1' failed='0' skipped='1'/>");
                });
                var sink = new SpyMessageSink <ITestAssemblyFinished>();

                xunit1.Run(testCases, sink);
                sink.Finished.WaitOne();

                var firstTestCase  = testCases[0];
                var testCollection = firstTestCase.TestMethod.TestClass.TestCollection;
                var testAssembly   = testCollection.TestAssembly;
                Assert.Collection(sink.Messages,
                                  message =>
                {
                    var assemblyStarting = Assert.IsAssignableFrom <ITestAssemblyStarting>(message);
                    Assert.Same(testAssembly, assemblyStarting.TestAssembly);
                    Assert.Contains("-bit .NET ", assemblyStarting.TestEnvironment);
                    Assert.Equal("Test framework display name", assemblyStarting.TestFrameworkDisplayName);
                    Assert.Equal(testCases, assemblyStarting.TestCases);

                    Assert.Equal("assembly", assemblyStarting.TestAssembly.Assembly.AssemblyPath);
                    Assert.Equal("config", assemblyStarting.TestAssembly.ConfigFileName);
                },
                                  message =>
                {
                    var testCollectionStarting = Assert.IsAssignableFrom <ITestCollectionStarting>(message);
                    Assert.Same(testAssembly, testCollectionStarting.TestAssembly);
                    Assert.Same(testCollection, testCollectionStarting.TestCollection);
                    Assert.Equal(testCases, testCollectionStarting.TestCases);

                    Assert.Equal(Guid.Empty, testCollection.UniqueID);
                    Assert.Equal("xUnit.net v1 Tests for assembly", testCollection.DisplayName);
                    Assert.Null(testCollection.CollectionDefinition);
                },
                                  message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom <ITestClassStarting>(message);
                    Assert.Same(testAssembly, testClassStarting.TestAssembly);
                    Assert.Same(testCollection, testClassStarting.TestCollection);
                    Assert.Equal("type1", testClassStarting.TestClass.Class.Name);
                    Assert.Collection(testClassStarting.TestCases,
                                      testCase => Assert.Same(testCases[0], testCase),
                                      testCase => Assert.Same(testCases[1], testCase)
                                      );
                },
                                  message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom <ITestMethodStarting>(message);
                    Assert.Same(testAssembly, testMethodStarting.TestAssembly);
                    Assert.Same(testCollection, testMethodStarting.TestCollection);
                    Assert.Equal("type1", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testMethodStarting.TestMethod.Method.Name);
                    Assert.Same(testCases[0], testMethodStarting.TestCases.Single());
                },
                                  message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                    Assert.Same(testAssembly, testCaseStarting.TestAssembly);
                    Assert.Same(testCollection, testCaseStarting.TestCollection);
                    Assert.Equal("type1", testCaseStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testCaseStarting.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testCaseStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                    Assert.Same(testAssembly, testStarting.TestAssembly);
                    Assert.Same(testCollection, testStarting.TestCollection);
                    Assert.Equal("type1", testStarting.TestClass.Class.Name);
                    Assert.Equal("passing", testStarting.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testPassed = Assert.IsAssignableFrom <ITestPassed>(message);
                    Assert.Same(testAssembly, testPassed.TestAssembly);
                    Assert.Same(testCollection, testPassed.TestCollection);
                    Assert.Equal("type1", testPassed.TestClass.Class.Name);
                    Assert.Equal("passing", testPassed.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testPassed.TestCase.DisplayName);
                    Assert.Equal(1M, testPassed.ExecutionTime);
                },
                                  message =>
                {
                    var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                    Assert.Same(testAssembly, testFinished.TestAssembly);
                    Assert.Same(testCollection, testFinished.TestCollection);
                    Assert.Equal("type1", testFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testFinished.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testFinished.TestCase.DisplayName);
                    Assert.Equal(1M, testFinished.ExecutionTime);
                },
                                  message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                    Assert.Same(testAssembly, testCaseFinished.TestAssembly);
                    Assert.Same(testCollection, testCaseFinished.TestCollection);
                    Assert.Equal("type1", testCaseFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testCaseFinished.TestMethod.Method.Name);
                    Assert.Equal("type1.passing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(1M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom <ITestMethodFinished>(message);
                    Assert.Same(testAssembly, testMethodFinished.TestAssembly);
                    Assert.Same(testCollection, testMethodFinished.TestCollection);
                    Assert.Equal("type1", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("passing", testMethodFinished.TestMethod.Method.Name);
                    Assert.Equal(1M, testMethodFinished.ExecutionTime);
                    Assert.Equal(0, testMethodFinished.TestsFailed);
                    Assert.Equal(1, testMethodFinished.TestsRun);
                    Assert.Equal(0, testMethodFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom <ITestMethodStarting>(message);
                    Assert.Equal("type1", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("failing", testMethodStarting.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                    Assert.Equal("type1.failing", testCaseStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                    Assert.Equal("type1.failing", testStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testFailed = Assert.IsAssignableFrom <ITestFailed>(message);
                    Assert.Equal("type1.failing", testFailed.TestCase.DisplayName);
                    Assert.Equal(0.234M, testFailed.ExecutionTime);
                    Assert.Equal("Xunit.MockFailureException", testFailed.ExceptionTypes.Single());
                    Assert.Equal("Failure message", testFailed.Messages.Single());
                    Assert.Equal("Stack trace", testFailed.StackTraces.Single());
                },
                                  message =>
                {
                    var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                    Assert.Equal("type1.failing", testFinished.TestCase.DisplayName);
                    Assert.Equal(0.234M, testFinished.ExecutionTime);
                },
                                  message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                    Assert.Equal("type1.failing", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0.234M, testCaseFinished.ExecutionTime);
                    Assert.Equal(1, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(0, testCaseFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom <ITestMethodFinished>(message);
                    Assert.Equal("type1", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("failing", testMethodFinished.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom <ITestClassFinished>(message);
                    Assert.Equal("type1", testClassFinished.TestClass.Class.Name);
                    Assert.Equal(1.234M, testClassFinished.ExecutionTime);
                    Assert.Equal(1, testClassFinished.TestsFailed);
                    Assert.Equal(2, testClassFinished.TestsRun);
                    Assert.Equal(0, testClassFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testClassStarting = Assert.IsAssignableFrom <ITestClassStarting>(message);
                    Assert.Equal("type2", testClassStarting.TestClass.Class.Name);
                },
                                  message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom <ITestMethodStarting>(message);
                    Assert.Equal("type2", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("skipping", testMethodStarting.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                    Assert.Equal("type2.skipping", testCaseStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                    Assert.Equal("type2.skipping", testStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testSkipped = Assert.IsAssignableFrom <ITestSkipped>(message);
                    Assert.Equal("type2.skipping", testSkipped.TestCase.DisplayName);
                    Assert.Equal(0M, testSkipped.ExecutionTime);
                    Assert.Equal("Skip message", testSkipped.Reason);
                },
                                  message =>
                {
                    var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                    Assert.Equal("type2.skipping", testFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testFinished.ExecutionTime);
                },
                                  message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                    Assert.Equal("type2.skipping", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(1, testCaseFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom <ITestMethodFinished>(message);
                    Assert.Equal("type2", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("skipping", testMethodFinished.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testMethodStarting = Assert.IsAssignableFrom <ITestMethodStarting>(message);
                    Assert.Equal("type2", testMethodStarting.TestClass.Class.Name);
                    Assert.Equal("skipping_with_start", testMethodStarting.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                    Assert.Equal("type2.skipping_with_start", testCaseStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                    Assert.Equal("type2.skipping_with_start", testStarting.TestCase.DisplayName);
                },
                                  message =>
                {
                    var testSkipped = Assert.IsAssignableFrom <ITestSkipped>(message);
                    Assert.Equal("type2.skipping_with_start", testSkipped.TestCase.DisplayName);
                    Assert.Equal(0M, testSkipped.ExecutionTime);
                    Assert.Equal("Skip message", testSkipped.Reason);
                },
                                  message =>
                {
                    var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                    Assert.Equal("type2.skipping_with_start", testFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testFinished.ExecutionTime);
                },
                                  message =>
                {
                    var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                    Assert.Equal("type2.skipping_with_start", testCaseFinished.TestCase.DisplayName);
                    Assert.Equal(0M, testCaseFinished.ExecutionTime);
                    Assert.Equal(0, testCaseFinished.TestsFailed);
                    Assert.Equal(1, testCaseFinished.TestsRun);
                    Assert.Equal(1, testCaseFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testMethodFinished = Assert.IsAssignableFrom <ITestMethodFinished>(message);
                    Assert.Equal("type2", testMethodFinished.TestClass.Class.Name);
                    Assert.Equal("skipping_with_start", testMethodFinished.TestMethod.Method.Name);
                },
                                  message =>
                {
                    var testClassFinished = Assert.IsAssignableFrom <ITestClassFinished>(message);
                    Assert.Equal("type2", testClassFinished.TestClass.Class.Name);
                    Assert.Equal(0M, testClassFinished.ExecutionTime);
                    Assert.Equal(0, testClassFinished.TestsFailed);
                    Assert.Equal(1, testClassFinished.TestsRun);
                    Assert.Equal(1, testClassFinished.TestsSkipped);
                },
                                  message =>
                {
                    var testCollectionFinished = Assert.IsAssignableFrom <ITestCollectionFinished>(message);
                    Assert.Equal(1.234M, testCollectionFinished.ExecutionTime);
                    Assert.Equal(1, testCollectionFinished.TestsFailed);
                    Assert.Equal(3, testCollectionFinished.TestsRun);
                    Assert.Equal(1, testCollectionFinished.TestsSkipped);
                    Assert.Same(testCollection, testCollectionFinished.TestCollection);
                },
                                  message =>
                {
                    var assemblyFinished = Assert.IsAssignableFrom <ITestAssemblyFinished>(message);
                    Assert.Equal(1.234M, assemblyFinished.ExecutionTime);
                    Assert.Equal(1, assemblyFinished.TestsFailed);
                    Assert.Equal(3, assemblyFinished.TestsRun);
                    Assert.Equal(1, assemblyFinished.TestsSkipped);
                }
                                  );
            }
        }
Exemplo n.º 33
0
        public void NoTestMethods()
        {
            using (var assm = new AcceptanceTestAssembly(code: ""))
            using (var controller = new Xunit2(assm.FileName, null, true))
            {
                var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();

                controller.Find(includeSourceInformation: false, messageSink: sink);

                sink.Finished.WaitOne();

                Assert.False(sink.Messages.Any(msg => msg is ITestCaseDiscoveryMessage));
            }
        }
        public static void TheoriesWithSerializableData_ReturnAsIndividualTestCases()
        {
            var assemblyInfo = Reflector.Wrap(Assembly.GetExecutingAssembly());
            var discoverer   = new XunitTestFrameworkDiscoverer(assemblyInfo, configFileName: null, _NullSourceInformationProvider.Instance, SpyMessageSink.Create());
            var sink         = new TestDiscoverySink();

            discoverer.Find(typeof(ClassWithTheory).FullName !, sink, _TestFrameworkOptions.ForDiscovery());
            sink.Finished.WaitOne();

            Assert.Collection(
                sink.TestCases.OrderBy(tc => tc.TestCaseDisplayName),
                testCase => Assert.Equal("XunitTestFrameworkDiscovererTests+SerializationAcceptanceTests+ClassWithTheory.Test(x: \"hello\")", testCase.TestCaseDisplayName),
                testCase => Assert.Equal("XunitTestFrameworkDiscovererTests+SerializationAcceptanceTests+ClassWithTheory.Test(x: 1)", testCase.TestCaseDisplayName)
                );

            var first  = sink.TestCases[0];
            var second = sink.TestCases[1];

            Assert.NotEqual(first.TestCaseUniqueID, second.TestCaseUniqueID);
        }
Exemplo n.º 35
0
    public static void SerializedTestsInSameCollectionRemainInSameCollection()
    {
        var sourceProvider = new NullSourceInformationProvider();
        var assemblyInfo   = Reflector.Wrap(Assembly.GetExecutingAssembly());
        var discoverer     = new XunitTestFrameworkDiscoverer(assemblyInfo, sourceProvider, SpyMessageSink.Create());
        var sink           = new TestDiscoverySink();

        discoverer.Find(typeof(ClassWithFacts).FullName, false, sink, TestFrameworkOptions.ForDiscovery());
        sink.Finished.WaitOne();

        var first  = sink.TestCases[0];
        var second = sink.TestCases[1];

        Assert.NotEqual(first.UniqueID, second.UniqueID);

        Assert.True(TestCollectionComparer.Instance.Equals(first.TestMethod.TestClass.TestCollection, second.TestMethod.TestClass.TestCollection));

        var serializedFirst  = SerializationHelper.Deserialize <ITestCase>(SerializationHelper.Serialize(first));
        var serializedSecond = SerializationHelper.Deserialize <ITestCase>(SerializationHelper.Serialize(second));

        Assert.NotSame(serializedFirst.TestMethod.TestClass.TestCollection, serializedSecond.TestMethod.TestClass.TestCollection);
        Assert.True(TestCollectionComparer.Instance.Equals(serializedFirst.TestMethod.TestClass.TestCollection, serializedSecond.TestMethod.TestClass.TestCollection));
    }
Exemplo n.º 36
0
 public TestableTheoryDiscoverer(List <IMessageSinkMessage> diagnosticMessages)
     : base(SpyMessageSink.Create(messages: diagnosticMessages))
 {
     DiagnosticMessages = diagnosticMessages;
 }
Exemplo n.º 37
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)
                );
            }
        }
Exemplo n.º 38
0
        public void TheoryWithInlineData()
        {
            string code = @"
                using System;
                using Xunit;

                public class TestClass
                {
                    [Theory]
                    [InlineData]
                    [InlineData(42)]
                    [InlineData(42, 21.12)]
                    public void TestMethod(int x) { }
                }
            ";

            using (var assembly = new AcceptanceTestAssembly(code))
            using (var controller = new Xunit2(assembly.FileName, null, true))
            {
                var sink = new SpyMessageSink<IDiscoveryCompleteMessage>();

                controller.Find(includeSourceInformation: false, messageSink: sink);

                sink.Finished.WaitOne();
                string[] testCaseNames = sink.Messages.OfType<ITestCaseDiscoveryMessage>().Select(tcdm => tcdm.TestCase.DisplayName).ToArray();

                Assert.Equal(3, testCaseNames.Length);

                Assert.Contains("TestClass.TestMethod(x: ???)", testCaseNames);
                Assert.Contains("TestClass.TestMethod(x: 42)", testCaseNames);
                Assert.Contains("TestClass.TestMethod(x: 42, ???: 21.12)", testCaseNames);
            }
        }
Exemplo n.º 39
0
        public void RunWithTestCases()
        {
            var testCollection = new Xunit1TestCollection("AssemblyName.dll");
            var testCases      = new[] {
                new Xunit1TestCase("assembly", "type1", "passing", "type1.passing")
                {
                    TestCollection = testCollection
                },
                new Xunit1TestCase("assembly", "type1", "failing", "type1.failing")
                {
                    TestCollection = testCollection
                },
                new Xunit1TestCase("assembly", "type2", "skipping", "type2.skipping")
                {
                    TestCollection = testCollection
                }
            };
            var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config");

            xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
            xunit1.Executor
            .When(x => x.RunTests("type1", Arg.Any <List <string> >(), Arg.Any <ICallbackEventHandler>()))
            .Do(callInfo =>
            {
                var callback = callInfo.Arg <ICallbackEventHandler>();
                callback.RaiseCallbackEvent("<start name='type1.passing' type='type1' method='passing'/>");
                callback.RaiseCallbackEvent("<test name='type1.passing' type='type1' method='passing' result='Pass' time='1.000'/>");
                callback.RaiseCallbackEvent("<start name='type1.failing' type='type1' method='failing'/>");
                callback.RaiseCallbackEvent("<test name='type1.failing' type='type1' method='failing' result='Fail' time='0.234'><failure exception-type='Xunit.MockFailureException'><message>Failure message</message><stack-trace>Stack trace</stack-trace></failure></test>");
                callback.RaiseCallbackEvent("<class name='type1' time='1.234' total='2' failed='1' skipped='0'/>");
            });
            xunit1.Executor
            .When(x => x.RunTests("type2", Arg.Any <List <string> >(), Arg.Any <ICallbackEventHandler>()))
            .Do(callInfo =>
            {
                var callback = callInfo.Arg <ICallbackEventHandler>();
                callback.RaiseCallbackEvent("<start name='type2.skipping' type='type2' method='skipping'/>");
                callback.RaiseCallbackEvent("<test name='type2.skipping' type='type2' method='skipping' result='Skip'><reason><message>Skip message</message></reason></test>");
                callback.RaiseCallbackEvent("<class name='type2' time='0.000' total='1' failed='0' skipped='1'/>");
            });
            var sink = new SpyMessageSink <ITestAssemblyFinished>();

            xunit1.Run(testCases, sink);
            sink.Finished.WaitOne();

            Assert.Collection(sink.Messages,
                              message =>
            {
                var assemblyStarting = Assert.IsAssignableFrom <ITestAssemblyStarting>(message);
                Assert.Equal("AssemblyName.dll", assemblyStarting.AssemblyFileName);
                Assert.Equal("ConfigFile.config", assemblyStarting.ConfigFileName);
                Assert.Contains("-bit .NET ", assemblyStarting.TestEnvironment);
                Assert.Equal("Test framework display name", assemblyStarting.TestFrameworkDisplayName);
            },
                              message =>
            {
                var testCollectionStarting = Assert.IsAssignableFrom <ITestCollectionStarting>(message);
                Assert.Same(testCollection, testCollectionStarting.TestCollection);
            },
                              message =>
            {
                var testClassStarting = Assert.IsAssignableFrom <ITestClassStarting>(message);
                Assert.Equal("type1", testClassStarting.ClassName);
                Assert.Same(testCollection, testClassStarting.TestCollection);
            },
                              message =>
            {
                var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                Assert.Equal("type1.passing", testCaseStarting.TestCase.DisplayName);
            },
                              message =>
            {
                var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                Assert.Equal("type1.passing", testStarting.TestCase.DisplayName);
                Assert.Equal("type1", testStarting.TestCase.Class.Name);
                Assert.Equal("passing", testStarting.TestCase.Method.Name);
                Assert.Same(testCollection, testStarting.TestCase.TestCollection);
            },
                              message =>
            {
                var testPassed = Assert.IsAssignableFrom <ITestPassed>(message);
                Assert.Equal("type1.passing", testPassed.TestCase.DisplayName);
                Assert.Equal(1M, testPassed.ExecutionTime);
            },
                              message =>
            {
                var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                Assert.Equal("type1.passing", testFinished.TestCase.DisplayName);
            },
                              message =>
            {
                var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                Assert.Equal("type1.passing", testCaseFinished.TestCase.DisplayName);
                Assert.Equal(1M, testCaseFinished.ExecutionTime);
                Assert.Equal(0, testCaseFinished.TestsFailed);
                Assert.Equal(1, testCaseFinished.TestsRun);
                Assert.Equal(0, testCaseFinished.TestsSkipped);
            },
                              message =>
            {
                var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                Assert.Equal("type1.failing", testCaseStarting.TestCase.DisplayName);
            },
                              message =>
            {
                var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                Assert.Equal("type1.failing", testStarting.TestCase.DisplayName);
                Assert.Equal("type1", testStarting.TestCase.Class.Name);
                Assert.Equal("failing", testStarting.TestCase.Method.Name);
                Assert.Same(testCollection, testStarting.TestCase.TestCollection);
            },
                              message =>
            {
                var testFailed = Assert.IsAssignableFrom <ITestFailed>(message);
                Assert.Equal("type1.failing", testFailed.TestCase.DisplayName);
                Assert.Equal(0.234M, testFailed.ExecutionTime);
                Assert.Equal("Xunit.MockFailureException", testFailed.ExceptionType);
                Assert.Equal("Failure message", testFailed.Message);
                Assert.Equal("Stack trace", testFailed.StackTrace);
            },
                              message =>
            {
                var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                Assert.Equal("type1.failing", testFinished.TestCase.DisplayName);
            },
                              message =>
            {
                var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                Assert.Equal("type1.failing", testCaseFinished.TestCase.DisplayName);
                Assert.Equal(0.234M, testCaseFinished.ExecutionTime);
                Assert.Equal(1, testCaseFinished.TestsFailed);
                Assert.Equal(1, testCaseFinished.TestsRun);
                Assert.Equal(0, testCaseFinished.TestsSkipped);
            },
                              message =>
            {
                var testClassFinished = Assert.IsAssignableFrom <ITestClassFinished>(message);
                Assert.Equal("type1", testClassFinished.ClassName);
                Assert.Equal(1.234M, testClassFinished.ExecutionTime);
                Assert.Equal(1, testClassFinished.TestsFailed);
                Assert.Equal(2, testClassFinished.TestsRun);
                Assert.Equal(0, testClassFinished.TestsSkipped);
            },
                              message =>
            {
                var testClassStarting = Assert.IsAssignableFrom <ITestClassStarting>(message);
                Assert.Equal("type2", testClassStarting.ClassName);
                Assert.Same(testCollection, testClassStarting.TestCollection);
            },
                              message =>
            {
                var testCaseStarting = Assert.IsAssignableFrom <ITestCaseStarting>(message);
                Assert.Equal("type2.skipping", testCaseStarting.TestCase.DisplayName);
            },
                              message =>
            {
                var testStarting = Assert.IsAssignableFrom <ITestStarting>(message);
                Assert.Equal("type2.skipping", testStarting.TestCase.DisplayName);
                Assert.Equal("type2", testStarting.TestCase.Class.Name);
                Assert.Equal("skipping", testStarting.TestCase.Method.Name);
                Assert.Same(testCollection, testStarting.TestCase.TestCollection);
            },
                              message =>
            {
                var testSkipped = Assert.IsAssignableFrom <ITestSkipped>(message);
                Assert.Equal("type2.skipping", testSkipped.TestCase.DisplayName);
                Assert.Equal(0M, testSkipped.ExecutionTime);
                Assert.Equal("Skip message", testSkipped.Reason);
            },
                              message =>
            {
                var testFinished = Assert.IsAssignableFrom <ITestFinished>(message);
                Assert.Equal("type2.skipping", testFinished.TestCase.DisplayName);
            },
                              message =>
            {
                var testCaseFinished = Assert.IsAssignableFrom <ITestCaseFinished>(message);
                Assert.Equal("type2.skipping", testCaseFinished.TestCase.DisplayName);
                Assert.Equal(0M, testCaseFinished.ExecutionTime);
                Assert.Equal(0, testCaseFinished.TestsFailed);
                Assert.Equal(1, testCaseFinished.TestsRun);
                Assert.Equal(1, testCaseFinished.TestsSkipped);
            },
                              message =>
            {
                var testClassFinished = Assert.IsAssignableFrom <ITestClassFinished>(message);
                Assert.Equal("type2", testClassFinished.ClassName);
                Assert.Equal(0M, testClassFinished.ExecutionTime);
                Assert.Equal(0, testClassFinished.TestsFailed);
                Assert.Equal(1, testClassFinished.TestsRun);
                Assert.Equal(1, testClassFinished.TestsSkipped);
            },
                              message =>
            {
                var testCollectionFinished = Assert.IsAssignableFrom <ITestCollectionFinished>(message);
                Assert.Equal(1.234M, testCollectionFinished.ExecutionTime);
                Assert.Equal(1, testCollectionFinished.TestsFailed);
                Assert.Equal(3, testCollectionFinished.TestsRun);
                Assert.Equal(1, testCollectionFinished.TestsSkipped);
                Assert.Same(testCollection, testCollectionFinished.TestCollection);
            },
                              message =>
            {
                var assemblyFinished = Assert.IsAssignableFrom <ITestAssemblyFinished>(message);
                Assert.Equal(1.234M, assemblyFinished.ExecutionTime);
                Assert.Equal(1, assemblyFinished.TestsFailed);
                Assert.Equal(3, assemblyFinished.TestsRun);
                Assert.Equal(1, assemblyFinished.TestsSkipped);
            }
                              );
        }
Exemplo n.º 40
0
 public ExtensibilityPointFactoryTests()
 {
     spy = SpyMessageSink.Create(messages: messages);
 }
Exemplo n.º 41
0
        public void ExceptionThrownDuringRunTests_ResultsInErrorMessage()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            using(var xunit1 = new TestableXunit1("AssemblyName.dll", "ConfigFile.config"))
            {
                var testCases = new[] {
                    new Xunit1TestCase("assembly", "config", "type1", "passing", "type1.passing")
                };
                var exception = new DivideByZeroException();
                xunit1.Executor.TestFrameworkDisplayName.Returns("Test framework display name");
                xunit1.Executor
                      .WhenForAnyArgs(x => x.RunTests(null, null, null))
                      .Do(callInfo => { throw exception; });
                var sink = new SpyMessageSink<ITestAssemblyFinished>();

                xunit1.Run(testCases, sink);
                sink.Finished.WaitOne();

                var errorMessage = Assert.Single(sink.Messages.OfType<IErrorMessage>());
                Assert.Equal("System.DivideByZeroException", errorMessage.ExceptionTypes.Single());
                Assert.Equal("Attempted to divide by zero.", errorMessage.Messages.Single());
                Assert.Equal(exception.StackTrace, errorMessage.StackTraces.Single());
            }
        }