How to use TestCollection method of Xunit.v3.Mocks class

Best Xunit code snippet using Xunit.v3.Mocks.TestCollection

Mocks.cs

Source:Mocks.cs Github

copy

Full Screen

...252 AppDomainOption appDomain = AppDomainOption.Disabled,253 bool shadowCopy = false)254 {255 var assembly = new XunitProjectAssembly { AssemblyFilename = "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };256 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, MethodDisplay = Xunit.TestMethodDisplay.ClassAndMethod, MaxParallelThreads = 42, ParallelizeTestCollections = true, ShadowCopy = shadowCopy };257 var result = Substitute.For<ITestAssemblyDiscoveryStarting, InterfaceProxy<ITestAssemblyDiscoveryStarting>>();258 result.AppDomain.Returns(appDomain);259 result.Assembly.Returns(assembly);260 result.DiscoveryOptions.Returns(TestFrameworkOptions.ForDiscovery(config));261 result.ShadowCopy.Returns(shadowCopy);262 return result;263 }264 public static ITestAssemblyExecutionFinished TestAssemblyExecutionFinished(265 bool diagnosticMessages = false,266 int total = 2112,267 int failed = 42,268 int skipped = 8,269 int errors = 6,270 decimal time = 123.456M)271 {272 var assembly = new XunitProjectAssembly { AssemblyFilename = "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };273 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, ShadowCopy = true };274 var summary = new ExecutionSummary { Total = total, Failed = failed, Skipped = skipped, Errors = errors, Time = time };275 var result = Substitute.For<ITestAssemblyExecutionFinished, InterfaceProxy<ITestAssemblyExecutionFinished>>();276 result.Assembly.Returns(assembly);277 result.ExecutionOptions.Returns(TestFrameworkOptions.ForExecution(config));278 result.ExecutionSummary.Returns(summary);279 return result;280 }281 public static ITestAssemblyExecutionStarting TestAssemblyExecutionStarting(282 bool diagnosticMessages = false,283 string? assemblyFilename = null)284 {285 var assembly = new XunitProjectAssembly { AssemblyFilename = assemblyFilename ?? "testAssembly.dll", ConfigFilename = "testAssembly.dll.config" };286 var config = new TestAssemblyConfiguration { DiagnosticMessages = diagnosticMessages, MethodDisplay = Xunit.TestMethodDisplay.ClassAndMethod, MaxParallelThreads = 42, ParallelizeTestCollections = true, ShadowCopy = true };287 var result = Substitute.For<ITestAssemblyExecutionStarting, InterfaceProxy<ITestAssemblyExecutionStarting>>();288 result.Assembly.Returns(assembly);289 result.ExecutionOptions.Returns(TestFrameworkOptions.ForExecution(config));290 return result;291 }292 public static ITestAssemblyFinished TestAssemblyFinished(293 int testsRun = 2112,294 int testsFailed = 42,295 int testsSkipped = 6,296 decimal executionTime = 123.4567M)297 {298 var testAssembly = TestAssembly("testAssembly.dll");299 var result = Substitute.For<ITestAssemblyFinished, InterfaceProxy<ITestAssemblyFinished>>();300 result.TestAssembly.Returns(testAssembly);301 result.TestsRun.Returns(testsRun);302 result.TestsFailed.Returns(testsFailed);303 result.TestsSkipped.Returns(testsSkipped);304 result.ExecutionTime.Returns(executionTime);305 return result;306 }307 public static ITestAssemblyStarting TestAssemblyStarting()308 {309 var testAssembly = TestAssembly("testAssembly.dll");310 var result = Substitute.For<ITestAssemblyStarting, InterfaceProxy<ITestAssemblyStarting>>();311 result.TestAssembly.Returns(testAssembly);312 return result;313 }314 public static ITestCase TestCase(ITestCollection? collection = null)315 {316 if (collection == null)317 collection = TestCollection();318 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();319 result.TestMethod.TestClass.TestCollection.Returns(collection);320 return result;321 }322 public static ITestCase TestCase(ITestMethod testMethod)323 {324 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();325 result.TestMethod.Returns(testMethod);326 return result;327 }328 public static ITestCase TestCase<TClassUnderTest>(329 string methodName,330 string? displayName = null,331 string? skipReason = null,332 string? uniqueID = null,333 string? fileName = null,334 int? lineNumber = null)335 {336 return TestCase(typeof(TClassUnderTest), methodName, displayName, skipReason, uniqueID, fileName, lineNumber);337 }338 public static ITestCase TestCase(339 Type type,340 string methodName,341 string? displayName = null,342 string? skipReason = null,343 string? uniqueID = null,344 string? fileName = null,345 int? lineNumber = null)346 {347 var testMethod = TestMethod(type, methodName);348 var traits = GetTraits(testMethod.Method);349 var result = Substitute.For<ITestCase, InterfaceProxy<ITestCase>>();350 result.DisplayName.Returns(displayName ?? $"{type}.{methodName}");351 result.SkipReason.Returns(skipReason);352 result.TestMethod.Returns(testMethod);353 result.Traits.Returns(traits);354 result.UniqueID.Returns(uniqueID ?? Guid.NewGuid().ToString());355 if (fileName != null && lineNumber != null)356 {357 var sourceInfo = new Xunit.SourceInformation { FileName = fileName, LineNumber = lineNumber };358 result.SourceInformation.Returns(sourceInfo);359 }360 return result;361 }362 public static IReflectionAttributeInfo TestCaseOrdererAttribute(string typeName, string assemblyName)363 {364 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();365 result.Attribute.Returns(new TestCaseOrdererAttribute(typeName, assemblyName));366 result.GetConstructorArguments().Returns(new object[] { typeName, assemblyName });367 return result;368 }369 public static IReflectionAttributeInfo TestCaseOrdererAttribute(Type type)370 {371 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();372 result.Attribute.Returns(new TestCaseOrdererAttribute(type));373 result.GetConstructorArguments().Returns(new object[] { type });374 return result;375 }376 public static IReflectionAttributeInfo TestCaseOrdererAttribute<TOrderer>() =>377 TestCaseOrdererAttribute(typeof(TOrderer));378 public static ITestClass TestClass(379 string? typeName = null,380 IReflectionAttributeInfo[]? attributes = null)381 {382 var testCollection = TestCollection();383 var typeInfo = TypeInfo(typeName, attributes: attributes);384 var result = Substitute.For<ITestClass, InterfaceProxy<ITestClass>>();385 result.Class.Returns(typeInfo);386 result.TestCollection.Returns(testCollection);387 return result;388 }389 public static TestClass TestClass(390 Type type,391 ITestCollection? collection = null)392 {393 if (collection == null)394 collection = TestCollection(type.Assembly);395 return new TestClass(collection, Reflector.Wrap(type));396 }397 public static TestCollection TestCollection(398 Assembly? assembly = null,399 ITypeInfo? definition = null,400 string? displayName = null)401 {402 if (assembly == null)403 assembly = typeof(Mocks).Assembly;404 if (displayName == null)405 displayName = "Mock test collection for " + assembly.CodeBase;406 return new TestCollection(TestAssembly(assembly), definition, displayName);407 }408 public static ITestCollectionFinished TestCollectionFinished(409 string displayName = "Display Name",410 int testsRun = 2112,411 int testsFailed = 42,412 int testsSkipped = 6,413 decimal executionTime = 123.4567M)414 {415 var result = Substitute.For<ITestCollectionFinished, InterfaceProxy<ITestCollectionFinished>>();416 result.TestsRun.Returns(testsRun);417 result.TestsFailed.Returns(testsFailed);418 result.TestsSkipped.Returns(testsSkipped);419 result.ExecutionTime.Returns(executionTime);420 result.TestCollection.DisplayName.Returns(displayName);421 return result;422 }423 public static ITestCollectionStarting TestCollectionStarting()424 {425 var result = Substitute.For<ITestCollectionStarting, InterfaceProxy<ITestCollectionStarting>>();426 result.TestCollection.DisplayName.Returns("Display Name");427 return result;428 }429 public static IReflectionAttributeInfo TestCollectionOrdererAttribute(string typeName, string assemblyName)430 {431 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();432 result.Attribute.Returns(new TestCollectionOrdererAttribute(typeName, assemblyName));433 result.GetConstructorArguments().Returns(new object[] { typeName, assemblyName });434 return result;435 }436 public static IReflectionAttributeInfo TestCollectionOrdererAttribute(Type type)437 {438 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();439 result.Attribute.Returns(new TestCollectionOrdererAttribute(type));440 result.GetConstructorArguments().Returns(new object[] { type });441 return result;442 }443 public static IReflectionAttributeInfo TestCollectionOrdererAttribute<TOrderer>() =>444 TestCollectionOrdererAttribute(typeof(TOrderer));445 public static ITestFailed TestFailed(446 Type type,447 string methodName,448 string? displayName = null,449 string? output = null,450 decimal executionTime = 0M,451 Exception? ex = null)452 {453 var testCase = TestCase(type, methodName);454 var test = Test(testCase, displayName ?? "NO DISPLAY NAME");455 var failureInfo = Xunit.Sdk.ExceptionUtility.ConvertExceptionToFailureInformation(ex ?? new Exception());456 var result = Substitute.For<ITestFailed, InterfaceProxy<ITestFailed>>();457 result.ExceptionParentIndices.Returns(failureInfo.ExceptionParentIndices);458 result.ExceptionTypes.Returns(failureInfo.ExceptionTypes);459 result.ExecutionTime.Returns(executionTime);460 result.Messages.Returns(failureInfo.Messages);461 result.Output.Returns(output);462 result.StackTraces.Returns(failureInfo.StackTraces);463 result.TestCase.Returns(testCase);464 result.Test.Returns(test);465 return result;466 }467 public static ITestFailed TestFailed(468 string displayName,469 decimal executionTime,470 string? exceptionType = null,471 string? exceptionMessage = null,472 string? stackTrace = null,473 string? output = null)474 {475 var testCase = TestCase();476 var test = Test(testCase, displayName);477 var result = Substitute.For<ITestFailed, InterfaceProxy<ITestFailed>>();478 result.ExceptionParentIndices.Returns(new[] { -1 });479 result.ExceptionTypes.Returns(new[] { exceptionType });480 result.ExecutionTime.Returns(executionTime);481 result.Messages.Returns(new[] { exceptionMessage });482 result.Output.Returns(output);483 result.StackTraces.Returns(new[] { stackTrace });484 result.TestCase.Returns(testCase);485 result.Test.Returns(test);486 return result;487 }488 public static IReflectionAttributeInfo TestFrameworkAttribute(Type type)489 {490 var attribute = Activator.CreateInstance(type);491 if (attribute == null)492 throw new InvalidOperationException($"Unable to create attribute instance: '{type.FullName}'");493 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();494 result.Attribute.Returns(attribute);495 result.GetCustomAttributes(null).ReturnsForAnyArgs(496 callInfo => LookupAttribute(497 callInfo.Arg<string>(),498 CustomAttributeData.GetCustomAttributes(attribute.GetType()).Select(x => Reflector.Wrap(x)).ToArray()499 )500 );501 return result;502 }503 public static ITestMethod TestMethod(504 string? typeName = null,505 string? methodName = null,506 string? displayName = null,507 string? skip = null,508 int timeout = 0,509 IEnumerable<IParameterInfo>? parameters = null,510 IEnumerable<IReflectionAttributeInfo>? classAttributes = null,511 IEnumerable<IReflectionAttributeInfo>? methodAttributes = null)512 {513 if (classAttributes == null)514 classAttributes = Enumerable.Empty<IReflectionAttributeInfo>();515 if (methodAttributes == null)516 methodAttributes = Enumerable.Empty<IReflectionAttributeInfo>();517 if (parameters == null)518 parameters = Enumerable.Empty<IParameterInfo>();519 var factAttribute = methodAttributes.FirstOrDefault(attr => typeof(FactAttribute).IsAssignableFrom(attr.Attribute.GetType()));520 if (factAttribute == null)521 {522 factAttribute = FactAttribute(displayName, skip, timeout);523 methodAttributes = methodAttributes.Concat(new[] { factAttribute });524 }525 var testClass = TestClass(typeName, attributes: classAttributes.ToArray());526 var methodInfo = MethodInfo(methodName, methodAttributes.ToArray(), parameters.ToArray(), testClass.Class);527 var result = Substitute.For<ITestMethod, InterfaceProxy<ITestMethod>>();528 result.Method.Returns(methodInfo);529 result.TestClass.Returns(testClass);530 return result;531 }532 public static TestMethod TestMethod(533 Type type,534 string methodName,535 ITestCollection? collection = null)536 {537 var @class = TestClass(type, collection);538 var methodInfo = type.GetMethod(methodName);539 if (methodInfo == null)540 throw new Exception($"Unknown method: {type.FullName}.{methodName}");541 return new TestMethod(@class, Reflector.Wrap(methodInfo));542 }543 public static ITestPassed TestPassed(544 Type type,545 string methodName,546 string? displayName = null,547 string? output = null,548 decimal executionTime = 0M)549 {550 var testCase = TestCase(type, methodName);551 var test = Test(testCase, displayName ?? "NO DISPLAY NAME");552 var result = Substitute.For<ITestPassed, InterfaceProxy<ITestPassed>>();553 result.ExecutionTime.Returns(executionTime);554 result.Output.Returns(output);555 result.TestCase.Returns(testCase);556 result.Test.Returns(test);557 return result;558 }559 public static ITestPassed TestPassed(560 string displayName,561 string? output = null)562 {563 var testCase = TestCase();564 var test = Test(testCase, displayName);565 var result = Substitute.For<ITestPassed, InterfaceProxy<ITestPassed>>();566 result.Test.Returns(test);567 result.ExecutionTime.Returns(1.2345M);568 result.Output.Returns(output);569 return result;570 }571 public static ITestResultMessage TestResult<TClassUnderTest>(572 string methodName,573 string displayName,574 decimal executionTime)575 {576 var testCase = TestCase<TClassUnderTest>(methodName);577 var test = Test(testCase, displayName);578 var result = Substitute.For<ITestResultMessage, InterfaceProxy<ITestResultMessage>>();579 result.TestCase.Returns(testCase);580 result.Test.Returns(test);581 result.ExecutionTime.Returns(executionTime);582 return result;583 }584 public static ITestSkipped TestSkipped(585 Type type,586 string methodName,587 string? displayName = null,588 string? output = null,589 decimal executionTime = 0M,590 string? skipReason = null)591 {592 var testCase = TestCase(type, methodName);593 var test = Test(testCase, displayName ?? "NO DISPLAY NAME");594 var result = Substitute.For<ITestSkipped, InterfaceProxy<ITestSkipped>>();595 result.ExecutionTime.Returns(executionTime);596 result.Output.Returns(output);597 result.Reason.Returns(skipReason);598 result.TestCase.Returns(testCase);599 result.Test.Returns(test);600 return result;601 }602 public static ITestSkipped TestSkipped(603 string displayName,604 string? skipReason = null)605 {606 var testCase = TestCase();607 var test = Test(testCase, displayName);608 var result = Substitute.For<ITestSkipped, InterfaceProxy<ITestSkipped>>();609 result.Reason.Returns(skipReason);610 result.TestCase.Returns(testCase);611 result.Test.Returns(test);612 return result;613 }614 public static ITestStarting TestStarting(string displayName)615 {616 var testCase = TestCase();617 var test = Test(testCase, displayName);618 var result = Substitute.For<ITestStarting, InterfaceProxy<ITestStarting>>();619 result.Test.Returns(test);620 return result;621 }622 public static IReflectionAttributeInfo TheoryAttribute(623 string? displayName = null,624 string? skip = null,625 int timeout = 0)626 {627 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();628 result.Attribute.Returns(new TheoryAttribute { DisplayName = displayName, Skip = skip });629 result.GetNamedArgument<string>("DisplayName").Returns(displayName);630 result.GetNamedArgument<string>("Skip").Returns(skip);631 result.GetNamedArgument<int>("Timeout").Returns(timeout);632 return result;633 }634 public static IReflectionAttributeInfo TraitAttribute<T>()635 where T : Attribute, new()636 {637 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();638 result.Attribute.Returns(new T());639 return result;640 }641 public static IReflectionAttributeInfo TraitAttribute(642 string name,643 string value)644 {645 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();646 var traitDiscovererAttributes = new[] { TraitDiscovererAttribute() };647 result.GetCustomAttributes(typeof(TraitDiscovererAttribute)).Returns(traitDiscovererAttributes);648 result.Attribute.Returns(new TraitAttribute(name, value));649 result.GetConstructorArguments().Returns(new object[] { name, value });650 return result;651 }652 public static IAttributeInfo TraitDiscovererAttribute(653 string typeName = "Xunit.Sdk.TraitDiscoverer",654 string assemblyName = "xunit.v3.core")655 {656 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();657 result.Attribute.Returns(new TraitDiscovererAttribute(typeName, assemblyName));658 result.GetConstructorArguments().Returns(new object[] { typeName, assemblyName });659 return result;660 }661 public static IAttributeInfo TraitDiscovererAttribute(Type discovererType)662 {663 var result = Substitute.For<IReflectionAttributeInfo, InterfaceProxy<IReflectionAttributeInfo>>();664 result.Attribute.Returns(new TraitDiscovererAttribute(discovererType));665 result.GetConstructorArguments().Returns(new object[] { discovererType });666 return result;667 }668 public static IAttributeInfo TraitDiscovererAttribute<TDiscoverer>() =>669 TraitDiscovererAttribute(typeof(TDiscoverer));670 public static ITypeInfo TypeInfo(671 string? typeName = null,672 IMethodInfo[]? methods = null,673 IReflectionAttributeInfo[]? attributes = null,674 string? assemblyFileName = null)675 {676 var result = Substitute.For<ITypeInfo, InterfaceProxy<ITypeInfo>>();677 result.Name.Returns(typeName ?? "type:" + Guid.NewGuid().ToString("n"));678 result.GetMethods(false).ReturnsForAnyArgs(methods ?? new IMethodInfo[0]);679 var assemblyInfo = AssemblyInfo(assemblyFileName: assemblyFileName);680 result.Assembly.Returns(assemblyInfo);681 result.GetCustomAttributes("").ReturnsForAnyArgs(callInfo => LookupAttribute(callInfo.Arg<string>(), attributes));682 return result;683 }684 public static XunitTestCase XunitTestCase<TClassUnderTest>(685 string methodName,686 ITestCollection? collection = null,687 object[]? testMethodArguments = null,688 IMessageSink? diagnosticMessageSink = null)689 {690 var method = TestMethod(typeof(TClassUnderTest), methodName, collection);691 return new XunitTestCase(diagnosticMessageSink ?? new NullMessageSink(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, method, testMethodArguments);692 }693 public static XunitTheoryTestCase XunitTheoryTestCase<TClassUnderTest>(694 string methodName,695 ITestCollection? collection = null,696 IMessageSink? diagnosticMessageSink = null)697 {698 var method = TestMethod(typeof(TClassUnderTest), methodName, collection);699 return new XunitTheoryTestCase(diagnosticMessageSink ?? new NullMessageSink(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, method);700 }701 // Helpers702 static Dictionary<string, List<string>> GetTraits(IMethodInfo method)703 {704 var result = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);705 foreach (var traitAttribute in method.GetCustomAttributes(typeof(TraitAttribute)))706 {707 var ctorArgs = traitAttribute.GetConstructorArguments().ToList();708 result.Add((string)ctorArgs[0], (string)ctorArgs[1]);709 }...

Full Screen

Full Screen

TestAssemblyRunnerTests.cs

Source:TestAssemblyRunnerTests.cs Github

copy

Full Screen

...54 }55 );56 }57 [Fact]58 public static async ValueTask FailureInQueueOfTestAssemblyStarting_DoesNotQueueTestAssemblyFinished_DoesNotRunTestCollections()59 {60 var messages = new List<_MessageSinkMessage>();61 var messageSink = Substitute.For<_IMessageSink>();62 messageSink63 .OnMessage(null!)64 .ReturnsForAnyArgs(callInfo =>65 {66 var msg = callInfo.Arg<_MessageSinkMessage>();67 messages.Add(msg);68 if (msg is _TestAssemblyStarting)69 throw new InvalidOperationException();70 return true;71 });72 var runner = TestableTestAssemblyRunner.Create(messageSink);73 var ex = await Record.ExceptionAsync(() => runner.RunAsync());74 Assert.IsType<InvalidOperationException>(ex);75 var starting = Assert.Single(messages);76 Assert.IsAssignableFrom<_TestAssemblyStarting>(starting);77 Assert.Empty(runner.CollectionsRun);78 }79 [Fact]80 public static async ValueTask FailureInAfterTestAssemblyStarting_GivesErroredAggregatorToTestCollectionRunner_NoCleanupFailureMessage()81 {82 var messages = new List<_MessageSinkMessage>();83 var messageSink = SpyMessageSink.Create(messages: messages);84 var runner = TestableTestAssemblyRunner.Create(messageSink);85 var ex = new DivideByZeroException();86 runner.AfterTestAssemblyStarting_Callback = aggregator => aggregator.Add(ex);87 await runner.RunAsync();88 Assert.Same(ex, runner.RunTestCollectionAsync_AggregatorResult);89 Assert.Empty(messages.OfType<_TestAssemblyCleanupFailure>());90 }91 [Fact]92 public static async ValueTask FailureInBeforeTestAssemblyFinished_ReportsCleanupFailure_DoesNotIncludeExceptionsFromAfterTestAssemblyStarting()93 {94 var thisAssembly = Assembly.GetExecutingAssembly();95 var messages = new List<_MessageSinkMessage>();96 var messageSink = SpyMessageSink.Create(messages: messages);97 var testCases = new[] { TestCaseForTestCollection() };98 var runner = TestableTestAssemblyRunner.Create(messageSink, testCases: testCases);99 var startingException = new DivideByZeroException();100 var finishedException = new InvalidOperationException();101 runner.AfterTestAssemblyStarting_Callback = aggregator => aggregator.Add(startingException);102 runner.BeforeTestAssemblyFinished_Callback = aggregator => aggregator.Add(finishedException);103 await runner.RunAsync();104 var assemblyStarting = Assert.Single(messages.OfType<_TestAssemblyStarting>());105 var cleanupFailure = Assert.Single(messages.OfType<_TestAssemblyCleanupFailure>());106#if NETFRAMEWORK107 Assert.Equal(thisAssembly.GetLocalCodeBase(), assemblyStarting.AssemblyPath);108 Assert.Equal(runner.TestAssembly.ConfigFileName, assemblyStarting.ConfigFilePath);109#endif110 Assert.Equal(typeof(InvalidOperationException).FullName, cleanupFailure.ExceptionTypes.Single());111 }112 [Fact]113 public static async ValueTask Cancellation_TestAssemblyStarting_DoesNotCallExtensibilityCallbacks()114 {115 var messageSink = SpyMessageSink.Create(msg => !(msg is _TestAssemblyStarting));116 var runner = TestableTestAssemblyRunner.Create(messageSink);117 await runner.RunAsync();118 Assert.False(runner.AfterTestAssemblyStarting_Called);119 Assert.False(runner.BeforeTestAssemblyFinished_Called);120 }121 [Fact]122 public static async ValueTask Cancellation_TestAssemblyFinished_CallsCallExtensibilityCallbacks()123 {124 var messageSink = SpyMessageSink.Create(msg => !(msg is _TestAssemblyFinished));125 var runner = TestableTestAssemblyRunner.Create(messageSink);126 await runner.RunAsync();127 Assert.True(runner.AfterTestAssemblyStarting_Called);128 Assert.True(runner.BeforeTestAssemblyFinished_Called);129 }130 [Fact]131 public static async ValueTask TestsAreGroupedByCollection()132 {133 var collection1 = Mocks.TestCollection(displayName: "1", uniqueID: "collection-1");134 var testCase1a = TestCaseForTestCollection(collection1);135 var testCase1b = TestCaseForTestCollection(collection1);136 var collection2 = Mocks.TestCollection(displayName: "2", uniqueID: "collection-2");137 var testCase2a = TestCaseForTestCollection(collection2);138 var testCase2b = TestCaseForTestCollection(collection2);139 var runner = TestableTestAssemblyRunner.Create(testCases: new[] { testCase1a, testCase2a, testCase2b, testCase1b });140 await runner.RunAsync();141 Assert.Collection(142 runner.CollectionsRun.OrderBy(c => c.Item1.DisplayName),143 tuple =>144 {145 Assert.Same(collection1, tuple.Item1);146 Assert.Collection(tuple.Item2,147 testCase => Assert.Same(testCase1a, testCase),148 testCase => Assert.Same(testCase1b, testCase)149 );150 },151 tuple =>152 {153 Assert.Same(collection2, tuple.Item1);154 Assert.Collection(tuple.Item2,155 testCase => Assert.Same(testCase2a, testCase),156 testCase => Assert.Same(testCase2b, testCase)157 );158 }159 );160 }161 [Fact]162 public static async void SignalingCancellationStopsRunningCollections()163 {164 var collection1 = Mocks.TestCollection();165 var testCase1 = TestCaseForTestCollection(collection1);166 var collection2 = Mocks.TestCollection();167 var testCase2 = TestCaseForTestCollection(collection2);168 var runner = TestableTestAssemblyRunner.Create(testCases: new[] { testCase1, testCase2 }, cancelInRunTestCollectionAsync: true);169 await runner.RunAsync();170 Assert.Single(runner.CollectionsRun);171 }172 [Fact]173 public static async void TestContextInspection()174 {175 var runner = TestableTestAssemblyRunner.Create();176 await runner.RunAsync();177 Assert.NotNull(runner.AfterTestAssemblyStarting_Context);178 Assert.Equal(TestEngineStatus.Initializing, runner.AfterTestAssemblyStarting_Context.TestAssemblyStatus);179 Assert.Equal(TestPipelineStage.TestAssemblyExecution, runner.AfterTestAssemblyStarting_Context.PipelineStage);180 Assert.Null(runner.AfterTestAssemblyStarting_Context.TestCollectionStatus);181 Assert.Null(runner.AfterTestAssemblyStarting_Context.TestClassStatus);182 Assert.Null(runner.AfterTestAssemblyStarting_Context.TestMethodStatus);183 Assert.Null(runner.AfterTestAssemblyStarting_Context.TestCaseStatus);184 Assert.Null(runner.AfterTestAssemblyStarting_Context.TestStatus);185 Assert.Same(runner.TestAssembly, runner.AfterTestAssemblyStarting_Context.TestAssembly);186 Assert.NotNull(runner.RunTestCollectionAsync_Context);187 Assert.Equal(TestEngineStatus.Running, runner.RunTestCollectionAsync_Context.TestAssemblyStatus);188 Assert.Null(runner.RunTestCollectionAsync_Context.TestCollectionStatus);189 Assert.Null(runner.RunTestCollectionAsync_Context.TestClassStatus);190 Assert.Null(runner.RunTestCollectionAsync_Context.TestMethodStatus);191 Assert.Null(runner.RunTestCollectionAsync_Context.TestCaseStatus);192 Assert.Null(runner.RunTestCollectionAsync_Context.TestStatus);193 Assert.Same(runner.TestAssembly, runner.RunTestCollectionAsync_Context.TestAssembly);194 Assert.NotNull(runner.BeforeTestAssemblyFinished_Context);195 Assert.Equal(TestEngineStatus.CleaningUp, runner.BeforeTestAssemblyFinished_Context.TestAssemblyStatus);196 Assert.Null(runner.BeforeTestAssemblyFinished_Context.TestCollectionStatus);197 Assert.Null(runner.BeforeTestAssemblyFinished_Context.TestClassStatus);198 Assert.Null(runner.BeforeTestAssemblyFinished_Context.TestMethodStatus);199 Assert.Null(runner.BeforeTestAssemblyFinished_Context.TestCaseStatus);200 Assert.Null(runner.BeforeTestAssemblyFinished_Context.TestStatus);201 Assert.Same(runner.TestAssembly, runner.BeforeTestAssemblyFinished_Context.TestAssembly);202 }203 }204 public class TestCaseOrderer205 {206 [Fact]207 public static void DefaultTestCaseOrderer()208 {209 var runner = TestableTestAssemblyRunner.Create();210 Assert.IsType<DefaultTestCaseOrderer>(runner.DefaultTestCaseOrderer);211 }212 }213 public class TestCollectionOrderer214 {215 [Fact]216 public static void DefaultTestCollectionOrderer()217 {218 var runner = TestableTestAssemblyRunner.Create();219 Assert.IsType<DefaultTestCollectionOrderer>(runner.DefaultTestCollectionOrderer);220 }221 [Fact]222 public static async ValueTask OrdererUsedToOrderTestCollections()223 {224 var collection1 = Mocks.TestCollection(displayName: "AAA", uniqueID: "collection-1");225 var testCase1a = TestCaseForTestCollection(collection1);226 var testCase1b = TestCaseForTestCollection(collection1);227 var collection2 = Mocks.TestCollection(displayName: "ZZZZ", uniqueID: "collection-2");228 var testCase2a = TestCaseForTestCollection(collection2);229 var testCase2b = TestCaseForTestCollection(collection2);230 var collection3 = Mocks.TestCollection(displayName: "MM", uniqueID: "collection-3");231 var testCase3a = TestCaseForTestCollection(collection3);232 var testCase3b = TestCaseForTestCollection(collection3);233 var testCases = new[] { testCase1a, testCase3a, testCase2a, testCase3b, testCase2b, testCase1b };234 var runner = TestableTestAssemblyRunner.Create(testCases: testCases, testCollectionOrderer: new DescendingDisplayNameCollectionOrderer());235 await runner.RunAsync();236 Assert.Collection(237 runner.CollectionsRun,238 collection =>239 {240 Assert.Same(collection2, collection.Item1);241 Assert.Equal(new[] { testCase2a, testCase2b }, collection.Item2);242 },243 collection =>244 {245 Assert.Same(collection3, collection.Item1);246 Assert.Equal(new[] { testCase3a, testCase3b }, collection.Item2);247 },248 collection =>249 {250 Assert.Same(collection1, collection.Item1);251 Assert.Equal(new[] { testCase1a, testCase1b }, collection.Item2);252 }253 );254 }255 class DescendingDisplayNameCollectionOrderer : ITestCollectionOrderer256 {257 public IReadOnlyCollection<_ITestCollection> OrderTestCollections(IReadOnlyCollection<_ITestCollection> TestCollections) =>258 TestCollections259 .OrderByDescending(c => c.DisplayName)260 .CastOrToReadOnlyCollection();261 }262 [Fact]263 public static async ValueTask TestCaseOrdererWhichThrowsLogsMessageAndDoesNotReorderTestCollections()264 {265 var spy = SpyMessageSink.Capture();266 TestContext.Current!.DiagnosticMessageSink = spy;267 var collection1 = Mocks.TestCollection(displayName: "AAA", uniqueID: "collection-1");268 var testCase1 = TestCaseForTestCollection(collection1);269 var collection2 = Mocks.TestCollection(displayName: "ZZZZ", uniqueID: "collection-2");270 var testCase2 = TestCaseForTestCollection(collection2);271 var collection3 = Mocks.TestCollection(displayName: "MM", uniqueID: "collection-3");272 var testCase3 = TestCaseForTestCollection(collection3);273 var testCases = new[] { testCase1, testCase2, testCase3 };274 var runner = TestableTestAssemblyRunner.Create(testCases: testCases, testCollectionOrderer: new ThrowingCollectionOrderer());275 await runner.RunAsync();276 Assert.Collection(277 runner.CollectionsRun,278 collection => Assert.Same(collection1, collection.Item1),279 collection => Assert.Same(collection2, collection.Item1),280 collection => Assert.Same(collection3, collection.Item1)281 );282 var diagnosticMessage = Assert.Single(spy.Messages.OfType<_DiagnosticMessage>());283 Assert.StartsWith("Test collection orderer 'TestAssemblyRunnerTests+TestCollectionOrderer+ThrowingCollectionOrderer' threw 'System.DivideByZeroException' during ordering: Attempted to divide by zero.", diagnosticMessage.Message);284 }285 class ThrowingCollectionOrderer : ITestCollectionOrderer286 {287 public IReadOnlyCollection<_ITestCollection> OrderTestCollections(IReadOnlyCollection<_ITestCollection> testCollections)288 {289 throw new DivideByZeroException();290 }291 }292 }293 class TestableTestAssemblyRunnerContext : TestAssemblyRunnerContext<_ITestCase>294 {295 public TestableTestAssemblyRunnerContext(296 _ITestAssembly testAssembly,297 IReadOnlyCollection<_ITestCase> testCases,298 _IMessageSink executionMessageSink,299 _ITestFrameworkExecutionOptions executionOptions) :300 base(testAssembly, testCases, executionMessageSink, executionOptions)301 { }302 public override string TestFrameworkDisplayName =>303 "The test framework display name";304 public override string TestFrameworkEnvironment =>305 "The test framework environment";306 // Use the sync message bus, so that we can immediately react to cancellations307 protected override IMessageBus CreateMessageBus() =>308 new SynchronousMessageBus(ExecutionMessageSink);309 }310 class TestableTestAssemblyRunner : TestAssemblyRunner<TestableTestAssemblyRunnerContext, _ITestCase>311 {312 readonly bool cancelInRunTestCollectionAsync;313 readonly TestableTestAssemblyRunnerContext ctxt;314 readonly RunSummary result;315 readonly ITestCollectionOrderer? testCollectionOrderer;316 public List<Tuple<_ITestCollection, IReadOnlyCollection<_ITestCase>>> CollectionsRun = new();317 public Action<ExceptionAggregator> AfterTestAssemblyStarting_Callback = _ => { };318 public bool AfterTestAssemblyStarting_Called;319 public TestContext? AfterTestAssemblyStarting_Context;320 public Action<ExceptionAggregator> BeforeTestAssemblyFinished_Callback = _ => { };321 public bool BeforeTestAssemblyFinished_Called;322 public TestContext? BeforeTestAssemblyFinished_Context;323 public ITestCaseOrderer DefaultTestCaseOrderer;324 public ITestCollectionOrderer DefaultTestCollectionOrderer;325 public Exception? RunTestCollectionAsync_AggregatorResult;326 public TestContext? RunTestCollectionAsync_Context;327 TestableTestAssemblyRunner(328 TestableTestAssemblyRunnerContext ctxt,329 RunSummary result,330 bool cancelInRunTestCollectionAsync,331 ITestCollectionOrderer? testCollectionOrderer)332 {333 DefaultTestCaseOrderer = GetTestCaseOrderer(ctxt);334 DefaultTestCollectionOrderer = GetTestCollectionOrderer(ctxt);335 this.ctxt = ctxt;336 this.result = result;337 this.cancelInRunTestCollectionAsync = cancelInRunTestCollectionAsync;338 this.testCollectionOrderer = testCollectionOrderer;339 }340 public static TestableTestAssemblyRunner Create(341 _IMessageSink? executionMessageSink = null,342 RunSummary? result = null,343 _ITestCase[]? testCases = null,344 _ITestFrameworkExecutionOptions? executionOptions = null,345 bool cancelInRunTestCollectionAsync = false,346 ITestCollectionOrderer? testCollectionOrderer = null)347 {348 var ctxt = new TestableTestAssemblyRunnerContext(349 Mocks.TestAssembly(Assembly.GetExecutingAssembly()),350 testCases ?? new[] { Substitute.For<_ITestCase>() }, // Need at least one so it calls RunTestCollectionAsync351 executionMessageSink ?? SpyMessageSink.Create(),352 executionOptions ?? _TestFrameworkOptions.ForExecution()353 );354 return new(ctxt, result ?? new RunSummary(), cancelInRunTestCollectionAsync, testCollectionOrderer);355 }356 public _ITestAssembly TestAssembly => ctxt.TestAssembly;357 protected override ITestCollectionOrderer GetTestCollectionOrderer(TestableTestAssemblyRunnerContext ctxt) =>358 testCollectionOrderer ?? base.GetTestCollectionOrderer(ctxt);359 protected override ValueTask AfterTestAssemblyStartingAsync(TestableTestAssemblyRunnerContext ctxt)360 {361 AfterTestAssemblyStarting_Called = true;362 AfterTestAssemblyStarting_Context = TestContext.Current;363 AfterTestAssemblyStarting_Callback(ctxt.Aggregator);364 return default;365 }366 protected override ValueTask BeforeTestAssemblyFinishedAsync(TestableTestAssemblyRunnerContext ctxt)367 {368 BeforeTestAssemblyFinished_Called = true;369 BeforeTestAssemblyFinished_Context = TestContext.Current;370 BeforeTestAssemblyFinished_Callback(ctxt.Aggregator);371 return default;372 }373 public ValueTask<RunSummary> RunAsync() =>374 RunAsync(ctxt);375 protected override ValueTask<RunSummary> RunTestCollectionAsync(376 TestableTestAssemblyRunnerContext ctxt,377 _ITestCollection testCollection,378 IReadOnlyCollection<_ITestCase> testCases)379 {380 if (cancelInRunTestCollectionAsync)381 ctxt.CancellationTokenSource.Cancel();382 RunTestCollectionAsync_AggregatorResult = ctxt.Aggregator.ToException();383 RunTestCollectionAsync_Context = TestContext.Current;384 CollectionsRun.Add(Tuple.Create(testCollection, testCases));385 return new(result);386 }387 }388 static _ITestCase TestCaseForTestCollection(_ITestCollection? collection = null)389 {390 collection ??= Mocks.TestCollection();391 var result = Substitute.For<_ITestCase, InterfaceProxy<_ITestCase>>();392 result.TestCollection.Returns(collection);393 return result;394 }395}...

Full Screen

Full Screen

XunitTestCollectionRunnerTests.cs

Source:XunitTestCollectionRunnerTests.cs Github

copy

Full Screen

...5using System.Threading.Tasks;6using Xunit;7using Xunit.Sdk;8using Xunit.v3;9public class XunitTestCollectionRunnerTests10{11 [Fact]12 public static async void CreatesFixtures()13 {14 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionUnderTest)), "Mock Test Collection");15 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("CreatesFixtures", collection);16 var runner = TestableXunitTestCollectionRunner.Create(testCase);17 await runner.RunAsync();18 Assert.NotNull(runner.RunTestClassesAsync_CollectionFixtureMappings);19 Assert.Collection(20 runner.RunTestClassesAsync_CollectionFixtureMappings.OrderBy(mapping => mapping.Key.Name),21 mapping => Assert.IsType<FixtureUnderTest>(mapping.Value),22 mapping => Assert.IsType<object>(mapping.Value)23 );24 }25 [Fact]26 public static async void DisposesFixtures()27 {28 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionUnderTest)), "Mock Test Collection");29 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("DisposesFixtures", collection);30 var runner = TestableXunitTestCollectionRunner.Create(testCase);31 await runner.RunAsync();32 Assert.NotNull(runner.RunTestClassesAsync_CollectionFixtureMappings);33 var fixtureUnderTest = runner.RunTestClassesAsync_CollectionFixtureMappings.Values.OfType<FixtureUnderTest>().Single();34 Assert.True(fixtureUnderTest.Disposed);35 }36 [Fact]37 public static async void DisposeAndAsyncDisposableShouldBeCalledInTheRightOrder()38 {39 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionForFixtureAsyncDisposableUnderTest)), "Mock Test Collection");40 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("DisposeAndAsyncDisposableShouldBeCalledInTheRightOrder", collection);41 var runner = TestableXunitTestCollectionRunner.Create(testCase);42 var runnerSessionTask = runner.RunAsync();43 await Task.Delay(500);44 Assert.NotNull(runner.RunTestClassesAsync_CollectionFixtureMappings);45 var fixtureUnderTest = runner.RunTestClassesAsync_CollectionFixtureMappings.Values.OfType<FixtureAsyncDisposableUnderTest>().Single();46 Assert.True(fixtureUnderTest.DisposeAsyncCalled);47 Assert.False(fixtureUnderTest.Disposed);48 fixtureUnderTest.DisposeAsyncSignaler.SetResult(true);49 await runnerSessionTask;50 Assert.True(fixtureUnderTest.Disposed);51 }52 class CollectionForFixtureAsyncDisposableUnderTest : ICollectionFixture<FixtureAsyncDisposableUnderTest> { }53 class FixtureAsyncDisposableUnderTest : IAsyncDisposable, IDisposable54 {55 public bool Disposed;56 public bool DisposeAsyncCalled;57 public TaskCompletionSource<bool> DisposeAsyncSignaler = new();58 public void Dispose()59 {60 Disposed = true;61 }62 public async ValueTask DisposeAsync()63 {64 DisposeAsyncCalled = true;65 await DisposeAsyncSignaler.Task;66 }67 }68 [Fact]69 public static async void MultiplePublicConstructorsOnCollectionFixture_ReturnsError()70 {71 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionsWithMultiCtorCollectionFixture)), "Mock Test Collection");72 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("CreatesFixtures", collection);73 var runner = TestableXunitTestCollectionRunner.Create(testCase);74 await runner.RunAsync();75 var ex = Assert.IsType<TestClassException>(runner.RunTestClassAsync_AggregatorResult);76 Assert.Equal("Collection fixture type 'XunitTestCollectionRunnerTests+CollectionFixtureWithMultipleConstructors' may only define a single public constructor.", ex.Message);77 }78 class CollectionFixtureWithMultipleConstructors79 {80 public CollectionFixtureWithMultipleConstructors() { }81 public CollectionFixtureWithMultipleConstructors(int unused) { }82 }83 class CollectionsWithMultiCtorCollectionFixture : ICollectionFixture<CollectionFixtureWithMultipleConstructors> { }84 [Fact]85 public static async void UnresolvedConstructorParameterOnCollectionFixture_ReturnsError()86 {87 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionWithCollectionFixtureWithDependency)), "Mock Test Collection");88 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("CreatesFixtures", collection);89 var runner = TestableXunitTestCollectionRunner.Create(testCase);90 await runner.RunAsync();91 var ex = Assert.IsType<TestClassException>(runner.RunTestClassAsync_AggregatorResult);92 Assert.Equal("Collection fixture type 'XunitTestCollectionRunnerTests+CollectionFixtureWithCollectionFixtureDependency' had one or more unresolved constructor arguments: DependentCollectionFixture collectionFixture", ex.Message);93 }94 class DependentCollectionFixture { }95 class CollectionFixtureWithCollectionFixtureDependency96 {97 public DependentCollectionFixture CollectionFixture;98 public CollectionFixtureWithCollectionFixtureDependency(DependentCollectionFixture collectionFixture)99 {100 CollectionFixture = collectionFixture;101 }102 }103 class CollectionWithCollectionFixtureWithDependency : ICollectionFixture<CollectionFixtureWithCollectionFixtureDependency> { }104 [Fact]105 public static async void CanInjectMessageSinkIntoCollectionFixture()106 {107 var spy = SpyMessageSink.Capture();108 TestContext.Current!.DiagnosticMessageSink = spy;109 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionWithCollectionFixtureWithMessageSinkDependency)), "Mock Test Collection");110 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("CreatesFixtures", collection);111 var runner = TestableXunitTestCollectionRunner.Create(testCase);112 await runner.RunAsync();113 Assert.Null(runner.RunTestClassAsync_AggregatorResult);114 Assert.NotNull(runner.RunTestClassesAsync_CollectionFixtureMappings);115 var classFixture = runner.RunTestClassesAsync_CollectionFixtureMappings.Values.OfType<CollectionFixtureWithMessageSinkDependency>().Single();116 Assert.NotNull(classFixture.MessageSink);117 Assert.Same(spy, classFixture.MessageSink);118 }119 [Fact]120 public static async void CanLogSinkMessageFromCollectionFixture()121 {122 var spy = SpyMessageSink.Capture();123 TestContext.Current!.DiagnosticMessageSink = spy;124 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionWithCollectionFixtureWithMessageSinkDependency)), "Mock Test Collection");125 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("CreatesFixtures", collection);126 var runner = TestableXunitTestCollectionRunner.Create(testCase);127 await runner.RunAsync();128 var diagnosticMessage = Assert.Single(spy.Messages.Cast<_DiagnosticMessage>());129 Assert.Equal("CollectionFixtureWithMessageSinkDependency constructor message", diagnosticMessage.Message);130 }131 class CollectionFixtureWithMessageSinkDependency132 {133 public _IMessageSink MessageSink;134 public CollectionFixtureWithMessageSinkDependency(_IMessageSink messageSink)135 {136 MessageSink = messageSink;137 MessageSink.OnMessage(new _DiagnosticMessage { Message = "CollectionFixtureWithMessageSinkDependency constructor message" });138 }139 }140 class CollectionWithCollectionFixtureWithMessageSinkDependency : ICollectionFixture<CollectionFixtureWithMessageSinkDependency> { }141 public class TestCaseOrderer142 {143 [Fact]144 public static async void UsesCustomTestOrderer()145 {146 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionUnderTest)), "Mock Test Collection");147 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("DisposesFixtures", collection);148 var runner = TestableXunitTestCollectionRunner.Create(testCase);149 await runner.RunAsync();150 Assert.IsType<CustomTestCaseOrderer>(runner.RunTestClassesAsync_TestCaseOrderer);151 }152 [Fact]153 public static async void SettingUnknownTestCaseOrderLogsDiagnosticMessage()154 {155 var spy = SpyMessageSink.Capture();156 TestContext.Current!.DiagnosticMessageSink = spy;157 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionWithUnknownTestCaseOrderer)), "TestCollectionDisplayName");158 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("DisposesFixtures", collection);159 var runner = TestableXunitTestCollectionRunner.Create(testCase);160 await runner.RunAsync();161 Assert.IsType<MockTestCaseOrderer>(runner.RunTestClassesAsync_TestCaseOrderer);162 var diagnosticMessage = Assert.Single(spy.Messages.Cast<_DiagnosticMessage>());163 Assert.Equal("Could not find type 'UnknownType' in UnknownAssembly for collection-level test case orderer on test collection 'TestCollectionDisplayName'", diagnosticMessage.Message);164 }165 [TestCaseOrderer("UnknownType", "UnknownAssembly")]166 class CollectionWithUnknownTestCaseOrderer { }167 [Fact]168 public static async void SettingTestCaseOrdererWithThrowingConstructorLogsDiagnosticMessage()169 {170 var spy = SpyMessageSink.Capture();171 TestContext.Current!.DiagnosticMessageSink = spy;172 var collection = new TestCollection(Mocks.TestAssembly(), Reflector.Wrap(typeof(CollectionWithCtorThrowingTestCaseOrderer)), "TestCollectionDisplayName");173 var testCase = TestData.XunitTestCase<XunitTestCollectionRunnerTests>("DisposesFixtures", collection);174 var runner = TestableXunitTestCollectionRunner.Create(testCase);175 await runner.RunAsync();176 Assert.IsType<MockTestCaseOrderer>(runner.RunTestClassesAsync_TestCaseOrderer);177 var diagnosticMessage = Assert.Single(spy.Messages.Cast<_DiagnosticMessage>());178 Assert.StartsWith("Collection-level test case orderer 'XunitTestCollectionRunnerTests+TestCaseOrderer+MyCtorThrowingTestCaseOrderer' for test collection 'TestCollectionDisplayName' threw 'System.DivideByZeroException' during construction: Attempted to divide by zero.", diagnosticMessage.Message);179 }180 [TestCaseOrderer(typeof(MyCtorThrowingTestCaseOrderer))]181 class CollectionWithCtorThrowingTestCaseOrderer { }182 class MyCtorThrowingTestCaseOrderer : ITestCaseOrderer183 {184 public MyCtorThrowingTestCaseOrderer()185 {186 throw new DivideByZeroException();187 }188 public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases)189 where TTestCase : notnull, _ITestCase190 {191 return Array.Empty<TTestCase>();192 }193 }194 }195 class FixtureUnderTest : IDisposable196 {197 public bool Disposed;198 public void Dispose()199 {200 Disposed = true;201 }202 }203 [TestCaseOrderer(typeof(CustomTestCaseOrderer))]204 class CollectionUnderTest : ICollectionFixture<FixtureUnderTest>, ICollectionFixture<object> { }205 class CustomTestCaseOrderer : ITestCaseOrderer206 {207 public IReadOnlyCollection<TTestCase> OrderTestCases<TTestCase>(IReadOnlyCollection<TTestCase> testCases)208 where TTestCase : notnull, _ITestCase209 {210 return testCases;211 }212 }213 class TestableXunitTestCollectionRunner : XunitTestCollectionRunner214 {215 readonly ExceptionAggregator aggregator;216 readonly IReadOnlyDictionary<Type, object> assemblyFixtureMappings;217 readonly CancellationTokenSource cancellationTokenSource;218 readonly IMessageBus messageBus;219 readonly ITestCaseOrderer testCaseOrderer;220 readonly IReadOnlyCollection<IXunitTestCase> testCases;221 readonly _ITestCollection testCollection;222 public Exception? RunTestClassAsync_AggregatorResult;223 public Dictionary<Type, object>? RunTestClassesAsync_CollectionFixtureMappings;224 public ITestCaseOrderer? RunTestClassesAsync_TestCaseOrderer;225 TestableXunitTestCollectionRunner(226 _ITestCollection testCollection,227 IReadOnlyCollection<IXunitTestCase> testCases,228 IMessageBus messageBus,229 ITestCaseOrderer testCaseOrderer,230 ExceptionAggregator aggregator,231 CancellationTokenSource cancellationTokenSource,232 IReadOnlyDictionary<Type, object> assemblyFixtureMappings)233 {234 this.testCollection = testCollection;235 this.testCases = testCases;236 this.messageBus = messageBus;237 this.testCaseOrderer = testCaseOrderer;238 this.aggregator = aggregator;239 this.cancellationTokenSource = cancellationTokenSource;240 this.assemblyFixtureMappings = assemblyFixtureMappings;241 }242 public static TestableXunitTestCollectionRunner Create(243 IXunitTestCase testCase,244 params object[] assemblyFixtures) =>245 new(246 testCase.TestCollection,247 new[] { testCase },248 new SpyMessageBus(),249 new MockTestCaseOrderer(),250 new ExceptionAggregator(),251 new CancellationTokenSource(),252 assemblyFixtures.ToDictionary(fixture => fixture.GetType())253 );254 public ValueTask<RunSummary> RunAsync() =>255 RunAsync(new(testCollection, testCases, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, assemblyFixtureMappings));256 protected override ValueTask<RunSummary> RunTestClassesAsync(XunitTestCollectionRunnerContext ctxt)257 {258 var result = base.RunTestClassesAsync(ctxt);259 RunTestClassesAsync_CollectionFixtureMappings = ctxt.CollectionFixtureMappings;260 RunTestClassesAsync_TestCaseOrderer = ctxt.TestCaseOrderer;261 return result;262 }263 protected override ValueTask<RunSummary> RunTestClassAsync(264 XunitTestCollectionRunnerContext ctxt,265 _ITestClass? testClass,266 _IReflectionTypeInfo? @class,267 IReadOnlyCollection<IXunitTestCase> testCases)268 {269 RunTestClassAsync_AggregatorResult = ctxt.Aggregator.ToException();270 return new(new RunSummary());271 }272 }273}...

Full Screen

Full Screen

TestCollectionRunnerTests.cs

Source:TestCollectionRunnerTests.cs Github

copy

Full Screen

...6using NSubstitute;7using Xunit;8using Xunit.Sdk;9using Xunit.v3;10public class TestCollectionRunnerTests11{12 [Fact]13 public static async void Messages()14 {15 var summary = new RunSummary { Total = 4, Failed = 2, Skipped = 1, Time = 21.12m };16 var messageBus = new SpyMessageBus();17 var testCase = Mocks.TestCase<ClassUnderTest>("Passing");18 var runner = TestableTestCollectionRunner.Create(messageBus, new[] { testCase }, summary);19 var result = await runner.RunAsync();20 Assert.Equal(result.Total, summary.Total);21 Assert.Equal(result.Failed, summary.Failed);22 Assert.Equal(result.Skipped, summary.Skipped);23 Assert.Equal(result.Time, summary.Time);24 Assert.False(runner.TokenSource.IsCancellationRequested);25 Assert.Collection(26 messageBus.Messages,27 msg =>28 {29 var starting = Assert.IsAssignableFrom<_TestCollectionStarting>(msg);30 Assert.Equal("assembly-id", starting.AssemblyUniqueID);31 Assert.Null(starting.TestCollectionClass);32 Assert.Equal("Mock test collection", starting.TestCollectionDisplayName);33 Assert.Equal("collection-id", starting.TestCollectionUniqueID);34 },35 msg =>36 {37 var finished = Assert.IsAssignableFrom<_TestCollectionFinished>(msg);38 Assert.Equal("assembly-id", finished.AssemblyUniqueID);39 Assert.Equal(21.12m, finished.ExecutionTime);40 Assert.Equal("collection-id", finished.TestCollectionUniqueID);41 Assert.Equal(2, finished.TestsFailed);42 Assert.Equal(4, finished.TestsRun);43 Assert.Equal(1, finished.TestsSkipped);44 }45 );46 }47 [Fact]48 public static async void FailureInQueueOfTestCollectionStarting_DoesNotQueueTestCollectionFinished_DoesNotRunTestClasses()49 {50 var messages = new List<_MessageSinkMessage>();51 var messageBus = Substitute.For<IMessageBus>();52 messageBus53 .QueueMessage(null!)54 .ReturnsForAnyArgs(callInfo =>55 {56 var msg = callInfo.Arg<_MessageSinkMessage>();57 messages.Add(msg);58 if (msg is _TestCollectionStarting)59 throw new InvalidOperationException();60 return true;61 });62 var runner = TestableTestCollectionRunner.Create(messageBus);63 var ex = await Record.ExceptionAsync(() => runner.RunAsync());64 Assert.IsType<InvalidOperationException>(ex);65 var starting = Assert.Single(messages);66 Assert.IsAssignableFrom<_TestCollectionStarting>(starting);67 Assert.Empty(runner.ClassesRun);68 }69 [Fact]70 public static async void RunTestClassAsync_AggregatorIncludesPassedInExceptions()71 {72 var messageBus = new SpyMessageBus();73 var ex = new DivideByZeroException();74 var runner = TestableTestCollectionRunner.Create(messageBus, aggregatorSeedException: ex);75 await runner.RunAsync();76 Assert.Same(ex, runner.RunTestClassAsync_AggregatorResult);77 Assert.Empty(messageBus.Messages.OfType<_TestCollectionCleanupFailure>());78 }79 [Fact]80 public static async void FailureInAfterTestCollectionStarting_GivesErroredAggregatorToTestClassRunner_NoCleanupFailureMessage()81 {82 var messageBus = new SpyMessageBus();83 var runner = TestableTestCollectionRunner.Create(messageBus);84 var ex = new DivideByZeroException();85 runner.AfterTestCollectionStarting_Callback = aggregator => aggregator.Add(ex);86 await runner.RunAsync();87 Assert.Same(ex, runner.RunTestClassAsync_AggregatorResult);88 Assert.Empty(messageBus.Messages.OfType<_TestCollectionCleanupFailure>());89 }90 [Fact]91 public static async void FailureInBeforeTestCollectionFinished_ReportsCleanupFailure_DoesNotIncludeExceptionsFromAfterTestCollectionStarting()92 {93 var messageBus = new SpyMessageBus();94 var testCases = new[] { Mocks.TestCase<TestAssemblyRunnerTests.RunAsync>("Messages") };95 var runner = TestableTestCollectionRunner.Create(messageBus, testCases);96 var startingException = new DivideByZeroException();97 var finishedException = new InvalidOperationException();98 runner.AfterTestCollectionStarting_Callback = aggregator => aggregator.Add(startingException);99 runner.BeforeTestCollectionFinished_Callback = aggregator => aggregator.Add(finishedException);100 await runner.RunAsync();101 var cleanupFailure = Assert.Single(messageBus.Messages.OfType<_TestCollectionCleanupFailure>());102 Assert.Equal(typeof(InvalidOperationException).FullName, cleanupFailure.ExceptionTypes.Single());103 }104 [Fact]105 public static async void Cancellation_TestCollectionStarting_DoesNotCallExtensibilityCallbacks()106 {107 var messageBus = new SpyMessageBus(msg => !(msg is _TestCollectionStarting));108 var runner = TestableTestCollectionRunner.Create(messageBus);109 await runner.RunAsync();110 Assert.True(runner.TokenSource.IsCancellationRequested);111 Assert.False(runner.AfterTestCollectionStarting_Called);112 Assert.False(runner.BeforeTestCollectionFinished_Called);113 }114 [Fact]115 public static async void Cancellation_TestCollectionFinished_CallsExtensibilityCallbacks()116 {117 var messageBus = new SpyMessageBus(msg => !(msg is _TestCollectionFinished));118 var runner = TestableTestCollectionRunner.Create(messageBus);119 await runner.RunAsync();120 Assert.True(runner.TokenSource.IsCancellationRequested);121 Assert.True(runner.AfterTestCollectionStarting_Called);122 Assert.True(runner.BeforeTestCollectionFinished_Called);123 }124 [Fact]125 public static async void Cancellation_TestCollectionCleanupFailure_SetsCancellationToken()126 {127 var messageBus = new SpyMessageBus(msg => !(msg is _TestCollectionCleanupFailure));128 var runner = TestableTestCollectionRunner.Create(messageBus);129 runner.BeforeTestCollectionFinished_Callback = aggregator => aggregator.Add(new Exception());130 await runner.RunAsync();131 Assert.True(runner.TokenSource.IsCancellationRequested);132 }133 [Fact]134 public static async void TestsAreGroupedByCollection()135 {136 var passing1 = Mocks.TestCase<ClassUnderTest>("Passing");137 var other1 = Mocks.TestCase<ClassUnderTest>("Other");138 var passing2 = Mocks.TestCase<ClassUnderTest2>("Passing");139 var other2 = Mocks.TestCase<ClassUnderTest2>("Other");140 var runner = TestableTestCollectionRunner.Create(testCases: new[] { passing1, passing2, other2, other1 });141 await runner.RunAsync();142 Assert.Collection(143 runner.ClassesRun,144 tuple =>145 {146 Assert.Equal("TestCollectionRunnerTests+ClassUnderTest", tuple.Item1?.Name);147 Assert.Collection(tuple.Item2,148 testCase => Assert.Same(passing1, testCase),149 testCase => Assert.Same(other1, testCase)150 );151 },152 tuple =>153 {154 Assert.Equal("TestCollectionRunnerTests+ClassUnderTest2", tuple.Item1?.Name);155 Assert.Collection(tuple.Item2,156 testCase => Assert.Same(passing2, testCase),157 testCase => Assert.Same(other2, testCase)158 );159 }160 );161 }162 [Fact]163 public static async void SignalingCancellationStopsRunningClasses()164 {165 var passing1 = Mocks.TestCase<ClassUnderTest>("Passing");166 var passing2 = Mocks.TestCase<ClassUnderTest2>("Passing");167 var runner = TestableTestCollectionRunner.Create(testCases: new[] { passing1, passing2 }, cancelInRunTestClassAsync: true);168 await runner.RunAsync();169 var tuple = Assert.Single(runner.ClassesRun);170 Assert.Equal("TestCollectionRunnerTests+ClassUnderTest", tuple.Item1?.Name);171 }172 [Fact]173 public static async void TestContextInspection()174 {175 var runner = TestableTestCollectionRunner.Create();176 await runner.RunAsync();177 Assert.NotNull(runner.AfterTestCollectionStarting_Context);178 Assert.Equal(TestEngineStatus.Running, runner.AfterTestCollectionStarting_Context.TestAssemblyStatus);179 Assert.Equal(TestEngineStatus.Initializing, runner.AfterTestCollectionStarting_Context.TestCollectionStatus);180 Assert.Equal(TestPipelineStage.TestCollectionExecution, runner.AfterTestCollectionStarting_Context.PipelineStage);181 Assert.Null(runner.AfterTestCollectionStarting_Context.TestClassStatus);182 Assert.Null(runner.AfterTestCollectionStarting_Context.TestMethodStatus);183 Assert.Null(runner.AfterTestCollectionStarting_Context.TestCaseStatus);184 Assert.Null(runner.AfterTestCollectionStarting_Context.TestStatus);185 Assert.Same(runner.TestCollection, runner.AfterTestCollectionStarting_Context.TestCollection);186 Assert.NotNull(runner.RunTestClassAsync_Context);187 Assert.Equal(TestEngineStatus.Running, runner.RunTestClassAsync_Context.TestAssemblyStatus);188 Assert.Equal(TestEngineStatus.Running, runner.RunTestClassAsync_Context.TestCollectionStatus);189 Assert.Null(runner.RunTestClassAsync_Context.TestClassStatus);190 Assert.Null(runner.RunTestClassAsync_Context.TestMethodStatus);191 Assert.Null(runner.RunTestClassAsync_Context.TestCaseStatus);192 Assert.Null(runner.RunTestClassAsync_Context.TestStatus);193 Assert.Same(runner.TestCollection, runner.RunTestClassAsync_Context.TestCollection);194 Assert.NotNull(runner.BeforeTestCollectionFinished_Context);195 Assert.Equal(TestEngineStatus.Running, runner.BeforeTestCollectionFinished_Context.TestAssemblyStatus);196 Assert.Equal(TestEngineStatus.CleaningUp, runner.BeforeTestCollectionFinished_Context.TestCollectionStatus);197 Assert.Null(runner.BeforeTestCollectionFinished_Context.TestClassStatus);198 Assert.Null(runner.BeforeTestCollectionFinished_Context.TestMethodStatus);199 Assert.Null(runner.BeforeTestCollectionFinished_Context.TestCaseStatus);200 Assert.Null(runner.BeforeTestCollectionFinished_Context.TestStatus);201 Assert.Same(runner.TestCollection, runner.BeforeTestCollectionFinished_Context.TestCollection);202 }203 class ClassUnderTest204 {205 [Fact]206 public void Passing() { }207 [Fact]208 public void Other() { }209 }210 class ClassUnderTest2 : ClassUnderTest { }211 class TestableTestCollectionRunner : TestCollectionRunner<TestCollectionRunnerContext<_ITestCase>, _ITestCase>212 {213 readonly ExceptionAggregator aggregator;214 readonly bool cancelInRunTestClassAsync;215 readonly IMessageBus messageBus;216 readonly RunSummary result;217 readonly ITestCaseOrderer testCaseOrderer;218 readonly IReadOnlyCollection<_ITestCase> testCases;219 public readonly List<Tuple<_IReflectionTypeInfo?, IReadOnlyCollection<_ITestCase>>> ClassesRun = new();220 public Action<ExceptionAggregator> AfterTestCollectionStarting_Callback = _ => { };221 public bool AfterTestCollectionStarting_Called;222 public TestContext? AfterTestCollectionStarting_Context;223 public Action<ExceptionAggregator> BeforeTestCollectionFinished_Callback = _ => { };224 public bool BeforeTestCollectionFinished_Called;225 public TestContext? BeforeTestCollectionFinished_Context;226 public Exception? RunTestClassAsync_AggregatorResult;227 public TestContext? RunTestClassAsync_Context;228 public readonly _ITestCollection TestCollection;229 public readonly CancellationTokenSource TokenSource;230 TestableTestCollectionRunner(231 _ITestCollection testCollection,232 IReadOnlyCollection<_ITestCase> testCases,233 IMessageBus messageBus,234 ITestCaseOrderer testCaseOrderer,235 ExceptionAggregator aggregator,236 CancellationTokenSource cancellationTokenSource,237 RunSummary result,238 bool cancelInRunTestClassAsync)239 {240 TestCollection = testCollection;241 this.testCases = testCases;242 this.messageBus = messageBus;243 this.testCaseOrderer = testCaseOrderer;244 this.aggregator = aggregator;245 TokenSource = cancellationTokenSource;246 this.result = result;247 this.cancelInRunTestClassAsync = cancelInRunTestClassAsync;248 }249 public static TestableTestCollectionRunner Create(250 IMessageBus? messageBus = null,251 _ITestCase[]? testCases = null,252 RunSummary? result = null,253 Exception? aggregatorSeedException = null,254 bool cancelInRunTestClassAsync = false)255 {256 if (testCases == null)257 testCases = new[] { Mocks.TestCase<ClassUnderTest>("Passing") };258 var aggregator = new ExceptionAggregator();259 if (aggregatorSeedException != null)260 aggregator.Add(aggregatorSeedException);261 return new TestableTestCollectionRunner(262 testCases.First().TestCollection,263 testCases,264 messageBus ?? new SpyMessageBus(),265 new MockTestCaseOrderer(),266 aggregator,267 new CancellationTokenSource(),268 result ?? new RunSummary(),269 cancelInRunTestClassAsync270 );271 }272 protected override ValueTask AfterTestCollectionStartingAsync(TestCollectionRunnerContext<_ITestCase> ctxt)273 {274 AfterTestCollectionStarting_Called = true;275 AfterTestCollectionStarting_Context = TestContext.Current;276 AfterTestCollectionStarting_Callback(ctxt.Aggregator);277 return default;278 }279 protected override ValueTask BeforeTestCollectionFinishedAsync(TestCollectionRunnerContext<_ITestCase> ctxt)280 {281 BeforeTestCollectionFinished_Called = true;282 BeforeTestCollectionFinished_Context = TestContext.Current;283 BeforeTestCollectionFinished_Callback(ctxt.Aggregator);284 return default;285 }286 public ValueTask<RunSummary> RunAsync() =>287 RunAsync(new(TestCollection, testCases, messageBus, testCaseOrderer, aggregator, TokenSource));288 protected override ValueTask<RunSummary> RunTestClassAsync(289 TestCollectionRunnerContext<_ITestCase> ctxt,290 _ITestClass? testClass,291 _IReflectionTypeInfo? @class,292 IReadOnlyCollection<_ITestCase> testCases)293 {294 if (cancelInRunTestClassAsync)295 ctxt.CancellationTokenSource.Cancel();296 RunTestClassAsync_AggregatorResult = ctxt.Aggregator.ToException();297 RunTestClassAsync_Context = TestContext.Current;298 ClassesRun.Add(Tuple.Create(@class, testCases));299 return new(result);300 }301 }302}...

Full Screen

Full Screen

XunitTestFrameworkDiscovererTests.cs

Source:XunitTestFrameworkDiscovererTests.cs Github

copy

Full Screen

...24 [Fact]25 public static void TestMethod() { }26 }27 [Fact]28 public static async void DefaultTestCollection()29 {30 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();31 var type = Reflector.Wrap(typeof(ClassWithNoCollection));32 var testClass = await discoverer.CreateTestClass(type);33 Assert.NotNull(testClass.TestCollection);34 Assert.Equal("Test collection for XunitTestFrameworkDiscovererTests+CreateTestClass+ClassWithNoCollection", testClass.TestCollection.DisplayName);35 Assert.Null(testClass.TestCollection.CollectionDefinition);36 }37 [Collection("This a collection without declaration")]38 class ClassWithUndeclaredCollection39 {40 [Fact]41 public static void TestMethod() { }42 }43 [Fact]44 public static async void UndeclaredTestCollection()45 {46 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();47 var type = Reflector.Wrap(typeof(ClassWithUndeclaredCollection));48 var testClass = await discoverer.CreateTestClass(type);49 Assert.NotNull(testClass.TestCollection);50 Assert.Equal("This a collection without declaration", testClass.TestCollection.DisplayName);51 Assert.Null(testClass.TestCollection.CollectionDefinition);52 }53 [CollectionDefinition("This a defined collection")]54 public class DeclaredCollection { }55 [Collection("This a defined collection")]56 class ClassWithDefinedCollection57 {58 [Fact]59 public static void TestMethod() { }60 }61 [Fact]62 public static async void DefinedTestCollection()63 {64 var type = Reflector.Wrap(typeof(ClassWithDefinedCollection));65 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(type.Assembly);66 var testClass = await discoverer.CreateTestClass(type);67 Assert.NotNull(testClass.TestCollection);68 Assert.Equal("This a defined collection", testClass.TestCollection.DisplayName);69 Assert.NotNull(testClass.TestCollection.CollectionDefinition);70 Assert.Equal("XunitTestFrameworkDiscovererTests+CreateTestClass+DeclaredCollection", testClass.TestCollection.CollectionDefinition.Name);71 }72 }73 public class FindTestsForType74 {75 [Fact]76 public static async ValueTask RequestsPublicAndPrivateMethodsFromType()77 {78 var typeInfo = Mocks.TypeInfo();79 var testClass = Mocks.TestClass(typeInfo);80 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();81 await discoverer.FindTestsForType(testClass);82 typeInfo.Received(1).GetMethods(includePrivateMethods: true);83 }84 [Fact]85 public static async ValueTask TestMethodWithTooManyFactAttributes_ReturnsExecutionErrorTestCase()86 {87 var testClass = Mocks.TestClass<ClassWithTooManyFactAttributesOnTestMethod>();88 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();89 await discoverer.FindTestsForType(testClass);90 var testCase = Assert.Single(discoverer.FindTestsForType_TestCases);91 var errorTestCase = Assert.IsType<ExecutionErrorTestCase>(testCase);92 Assert.Equal($"Test method '{typeof(ClassWithTooManyFactAttributesOnTestMethod).FullName}.{nameof(ClassWithTooManyFactAttributesOnTestMethod.TestMethod)}' has multiple [Fact]-derived attributes", errorTestCase.ErrorMessage);93 }94 class ClassWithTooManyFactAttributesOnTestMethod95 {96 [Fact]97 [Theory]98 public void TestMethod() { }99 }100 [Fact]101 public static async ValueTask DoesNotDiscoverNonFactDecoratedTestMethod()102 {103 var testClass = Mocks.TestClass<ClassWithNoTests>();104 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();105 await discoverer.FindTestsForType(testClass);106 Assert.Empty(discoverer.FindTestsForType_TestCases);107 }108 class ClassWithNoTests109 {110 public void TestMethod() { }111 }112 [Fact]113 public static async ValueTask DiscoversFactDecoratedTestMethod()114 {115 var testClass = Mocks.TestClass<ClassWithOneTest>();116 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();117 await discoverer.FindTestsForType(testClass);118 var testCase = Assert.Single(discoverer.FindTestsForType_TestCases);119 Assert.IsType<XunitTestCase>(testCase);120 Assert.Equal($"{typeof(ClassWithOneTest).FullName}.{nameof(ClassWithOneTest.TestMethod)}", testCase.TestCaseDisplayName);121 }122 class ClassWithOneTest123 {124 [Fact]125 public void TestMethod() { }126 }127 [Fact]128 public static async void Theory_WithPreEnumeration_ReturnsOneTestCasePerDataRecord()129 {130 var testClass = Mocks.TestClass<TheoryWithInlineData>();131 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();132 var discoveryOptions = _TestFrameworkOptions.ForDiscovery(preEnumerateTheories: true);133 await discoverer.FindTestsForType(testClass, discoveryOptions);134 Assert.Collection(135 discoverer.FindTestsForType_TestCases.Select(t => t.TestCaseDisplayName).OrderBy(x => x),136 displayName => Assert.Equal($"{typeof(TheoryWithInlineData).FullName}.{nameof(TheoryWithInlineData.TheoryMethod)}(value: \"Hello world\")", displayName),137 displayName => Assert.Equal($"{typeof(TheoryWithInlineData).FullName}.{nameof(TheoryWithInlineData.TheoryMethod)}(value: 42)", displayName)138 );139 }140 [Fact]141 public static async void Theory_WithoutPreEnumeration_ReturnsOneTestCase()142 {143 var testClass = Mocks.TestClass<TheoryWithInlineData>();144 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();145 var discoveryOptions = _TestFrameworkOptions.ForDiscovery(preEnumerateTheories: false);146 await discoverer.FindTestsForType(testClass, discoveryOptions);147 var testCase = Assert.Single(discoverer.FindTestsForType_TestCases);148 Assert.Equal($"{typeof(TheoryWithInlineData).FullName}.{nameof(TheoryWithInlineData.TheoryMethod)}", testCase.TestCaseDisplayName);149 }150 class TheoryWithInlineData151 {152 [Theory]153 [InlineData("Hello world")]154 [InlineData(42)]155 public static void TheoryMethod(object value) { }156 }157 [Fact]158 public static async void AssemblyWithMultiLevelHierarchyWithFactOverridenInNonImmediateDerivedClass_ReturnsOneTestCase()159 {160 var testClass = Mocks.TestClass<Child>();161 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();162 await discoverer.FindTestsForType(testClass);163 var testCase = Assert.Single(discoverer.FindTestsForType_TestCases);164 Assert.Equal($"{typeof(Child).FullName}.{nameof(GrandParent.FactOverridenInNonImmediateDerivedClass)}", testCase.TestCaseDisplayName);165 }166 public abstract class GrandParent167 {168 [Fact]169 public virtual void FactOverridenInNonImmediateDerivedClass()170 {171 Assert.True(true);172 }173 }174 public abstract class Parent : GrandParent { }175 public class Child : Parent176 {177 public override void FactOverridenInNonImmediateDerivedClass()178 {179 base.FactOverridenInNonImmediateDerivedClass();180 Assert.False(false);181 }182 }183 }184 public static class TestCollectionFactory185 {186 [Fact]187 public static void DefaultTestCollectionFactory()188 {189 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();190 Assert.IsType<CollectionPerClassTestCollectionFactory>(discoverer.TestCollectionFactory);191 }192 [Theory(DisableDiscoveryEnumeration = true)]193 [InlineData(CollectionBehavior.CollectionPerAssembly, typeof(CollectionPerAssemblyTestCollectionFactory))]194 [InlineData(CollectionBehavior.CollectionPerClass, typeof(CollectionPerClassTestCollectionFactory))]195 public static void AssemblyAttributeOverride(196 CollectionBehavior behavior,197 Type expectedFactoryType)198 {199 var behaviorAttribute = Mocks.CollectionBehaviorAttribute(behavior);200 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });201 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);202 Assert.IsType(expectedFactoryType, discoverer.TestCollectionFactory);203 }204 [Fact]205 public static void ValidCustomFactory()206 {207 var behaviorAttribute = Mocks.CollectionBehaviorAttribute<CustomTestCollectionFactory>();208 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });209 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);210 Assert.IsType<CustomTestCollectionFactory>(discoverer.TestCollectionFactory);211 }212 [Fact]213 public static void InvalidCustomFactoryFallsBackToDefault()214 {215 var spyMessageSink = SpyMessageSink.Capture();216 TestContext.Current!.DiagnosticMessageSink = spyMessageSink;217 var behaviorAttribute = Mocks.CollectionBehaviorAttribute<object>();218 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });219 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);220 Assert.IsType<CollectionPerClassTestCollectionFactory>(discoverer.TestCollectionFactory);221 var message = Assert.Single(spyMessageSink.Messages);222 var diagMessage = Assert.IsType<_DiagnosticMessage>(message);223 Assert.Equal("Test collection factory type 'System.Object' does not implement IXunitTestCollectionFactory", diagMessage.Message);224 }225 }226 public static class TestFrameworkDisplayName227 {228 [Fact]229 public static void Defaults()230 {231 var discoverer = TestableXunitTestFrameworkDiscoverer.Create();232 Assert.Matches(@"xUnit.net v3 \d+\.\d+\.\d+(-pre\.\d+(-dev)?(\+[0-9a-f]+)?)? \[collection-per-class, parallel\]", discoverer.TestFrameworkDisplayName);233 }234 [Fact]235 public static void CollectionPerAssembly()236 {237 var behaviorAttribute = Mocks.CollectionBehaviorAttribute(CollectionBehavior.CollectionPerAssembly);238 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });239 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);240 Assert.Matches(@"xUnit.net v3 \d+\.\d+\.\d+(-pre\.\d+(-dev)?(\+[0-9a-f]+)?)? \[collection-per-assembly, parallel\]", discoverer.TestFrameworkDisplayName);241 }242 [Fact]243 public static void CustomCollectionFactory()244 {245 var behaviorAttribute = Mocks.CollectionBehaviorAttribute<CustomTestCollectionFactory>();246 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });247 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);248 Assert.Matches(@"xUnit.net v3 \d+\.\d+\.\d+(-pre\.\d+(-dev)?(\+[0-9a-f]+)?)? \[my-custom-test-collection-factory, parallel\]", discoverer.TestFrameworkDisplayName);249 }250 [Fact]251 public static void NonParallel()252 {253 var behaviorAttribute = Mocks.CollectionBehaviorAttribute(disableTestParallelization: true);254 var assembly = Mocks.AssemblyInfo(attributes: new[] { behaviorAttribute });255 var discoverer = TestableXunitTestFrameworkDiscoverer.Create(assembly);256 Assert.Matches(@"xUnit.net v3 \d+\.\d+\.\d+(-pre\.\d+(-dev)?(\+[0-9a-f]+)?)? \[collection-per-class, non-parallel\]", discoverer.TestFrameworkDisplayName);257 }258 }259 class ClassWithSingleTest260 {261 [Fact]262 public static void TestMethod() { }263 }264 class CustomTestCollectionFactory : IXunitTestCollectionFactory265 {266 public CustomTestCollectionFactory(_ITestAssembly testAssembly)267 { }268 public string DisplayName => "my-custom-test-collection-factory";269 public _ITestCollection Get(_ITypeInfo testClass) => throw new NotImplementedException();270 }271 class TestableXunitTestFrameworkDiscoverer : XunitTestFrameworkDiscoverer272 {273 public List<_ITestCaseMetadata> FindTestsForType_TestCases = new();274 TestableXunitTestFrameworkDiscoverer(275 _IAssemblyInfo assemblyInfo,276 IXunitTestCollectionFactory? collectionFactory)277 : base(assemblyInfo, configFileName: null, collectionFactory)278 {279 TestAssembly = Mocks.TestAssembly(assemblyInfo.AssemblyPath, uniqueID: "asm-id");280 }281 public new _IAssemblyInfo AssemblyInfo => base.AssemblyInfo;282 public override _ITestAssembly TestAssembly { get; }283 public new ValueTask<_ITestClass> CreateTestClass(_ITypeInfo @class) =>284 base.CreateTestClass(@class);285 public ValueTask<bool> FindTestsForType(286 _ITestClass testClass,287 _ITestFrameworkDiscoveryOptions? discoveryOptions = null) =>288 base.FindTestsForType(289 testClass,290 discoveryOptions ?? _TestFrameworkOptions.ForDiscovery(preEnumerateTheories: true),291 testCase =>292 {293 FindTestsForType_TestCases.Add(testCase);294 return new(true);295 }296 );297 protected sealed override bool IsValidTestClass(_ITypeInfo type) =>298 base.IsValidTestClass(type);299 public static TestableXunitTestFrameworkDiscoverer Create(300 _IAssemblyInfo? assembly = null,301 IXunitTestCollectionFactory? collectionFactory = null) =>302 new(assembly ?? Mocks.AssemblyInfo(), collectionFactory);303 }304}...

Full Screen

Full Screen

XunitTestCaseTests.cs

Source:XunitTestCaseTests.cs Github

copy

Full Screen

...149 [MemberData(nameof(CustomAttributeTestCases))]150 public void ReturnsCorrectCustomAttributes(Type classType, IEnumerable<string> expectedTraits)151 {152 var testAssembly = new TestAssembly(new ReflectionAssemblyInfo(classType.Assembly));153 var testCollection = new TestCollection(testAssembly, null, "Trait inheritance tests");154 var @class = new ReflectionTypeInfo(classType);155 var testClass = new TestClass(testCollection, @class);156 var methodInfo = new ReflectionMethodInfo(classType.GetMethod("TraitsTest")!);157 var testMethod = new TestMethod(testClass, methodInfo);158 var testCase = new TestableXunitTestCase(testMethod);159 var testTraits = testCase.Traits["Test"];160 Assert.NotNull(testTraits);161 foreach (var expectedTrait in expectedTraits)162 Assert.Contains(expectedTrait, testTraits);163 }164 class BaseClassWithoutTraits165 { }166 [Trait("Test", "BaseOne")]167 class BaseClassWithSingleTrait...

Full Screen

Full Screen

Mocks.TestCases.cs

Source:Mocks.TestCases.cs Github

copy

Full Screen

...84 testMethod ??= TestMethod();85 displayName ??= $"{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}";86 traits ??= GetTraits(testMethod.Method);87 uniqueID ??= "case-id";88 var testCollection = testMethod.TestClass.TestCollection;89 var testClass = testMethod.TestClass;90 var result = Substitute.For<_ITestCase, InterfaceProxy<_ITestCase>>();91 result.TestCaseDisplayName.Returns(displayName);92 result.SkipReason.Returns(skipReason);93 result.SourceFilePath.Returns(fileName);94 result.SourceLineNumber.Returns(lineNumber);95 result.TestCollection.Returns(testCollection);96 result.TestClass.Returns(testClass);97 result.TestMethod.Returns(testMethod);98 result.Traits.Returns(traits);99 result.UniqueID.Returns(uniqueID);100 return result;101 }102 public static _ITestClass TestClass<TClassUnderTest>(103 _ITestCollection? collection = null,104 string? uniqueID = null) =>105 TestClass(106 TypeInfo<TClassUnderTest>(),107 collection,108 uniqueID109 );110 public static _ITestClass TestClass(111 string? typeName,112 _IMethodInfo[]? methods = null,113 _IReflectionAttributeInfo[]? attributes = null,114 _ITypeInfo? baseType = null,115 _ITestCollection? collection = null,116 string? uniqueID = null) =>117 TestClass(TypeInfo(typeName, methods, attributes, baseType), collection, uniqueID);118 public static _ITestClass TestClass(119 _ITypeInfo? typeInfo = null,120 _ITestCollection? collection = null,121 string? uniqueID = null)122 {123 typeInfo ??= TypeInfo();124 collection ??= TestCollection();125 uniqueID ??= "class-id";126 var result = Substitute.For<_ITestClass, InterfaceProxy<_ITestClass>>();127 result.Class.Returns(typeInfo);128 result.TestCollection.Returns(collection);129 result.UniqueID.Returns(uniqueID);130 return result;131 }132 public static _ITestCollection TestCollection(133 Assembly assembly,134 _ITypeInfo? definition = null,135 string? displayName = null,136 string? uniqueID = null) =>137 TestCollection(TestAssembly(assembly), definition, displayName, uniqueID);138 public static _ITestCollection TestCollection(139 _ITestAssembly? assembly = null,140 _ITypeInfo? definition = null,141 string? displayName = null,142 string? uniqueID = null)143 {144 assembly ??= TestAssembly();145 displayName ??= "Mock test collection";146 uniqueID ??= "collection-id";147 var result = Substitute.For<_ITestCollection, InterfaceProxy<_ITestCollection>>();148 result.CollectionDefinition.Returns(definition);149 result.DisplayName.Returns(displayName);150 result.TestAssembly.Returns(assembly);151 result.UniqueID.Returns(uniqueID);152 return result;153 }154 public static _ITestMethod TestMethod(155 string? typeName = null,156 string? methodName = null,157 string? displayName = null,158 string? skip = null,159 int timeout = 0,160 _IParameterInfo[]? parameters = null,161 _IReflectionAttributeInfo[]? classAttributes = null,162 _IReflectionAttributeInfo[]? methodAttributes = null,163 _ITestCollection? collection = null,164 string? uniqueID = null)165 {166 parameters ??= EmptyParameterInfos;167 classAttributes ??= EmptyAttributeInfos;168 methodAttributes ??= EmptyAttributeInfos;169 // Ensure that there's a FactAttribute, or else it's not technically a test method170 var factAttribute = methodAttributes.FirstOrDefault(attr => typeof(FactAttribute).IsAssignableFrom(attr.Attribute.GetType()));171 if (factAttribute == null)172 {173 factAttribute = FactAttribute(displayName, skip, timeout);174 methodAttributes = methodAttributes.Concat(new[] { factAttribute }).ToArray();175 }176 var testClass = TestClass(typeName, attributes: classAttributes, collection: collection);177 var methodInfo = MethodInfo(methodName, methodAttributes, parameters, testClass.Class);178 return TestMethod(methodInfo, testClass, uniqueID);179 }180 public static _ITestMethod TestMethod<TClassUnderTest>(181 string methodName,182 _ITestCollection? collection = null,183 string? uniqueID = null) =>184 TestMethod(MethodInfo<TClassUnderTest>(methodName), TestClass<TClassUnderTest>(collection), uniqueID);185 public static _ITestMethod TestMethod(186 _IMethodInfo methodInfo,187 _ITestClass testClass,188 string? uniqueID = null)189 {190 uniqueID ??= "method-id";191 var result = Substitute.For<_ITestMethod, InterfaceProxy<_ITestMethod>>();192 result.Method.Returns(methodInfo);193 result.TestClass.Returns(testClass);194 result.UniqueID.Returns(uniqueID);195 return result;196 }...

Full Screen

Full Screen

XunitTestCaseRunnerBaseTests.cs

Source:XunitTestCaseRunnerBaseTests.cs Github

copy

Full Screen

...6[assembly: XunitTestCaseRunnerBaseTests.BeforeAfterOnAssembly]7public class XunitTestCaseRunnerBaseTests8{9 [Fact]10 public static void BeforeAfterTestAttributesComeFromTestCollectionAndTestClassAndTestMethod()11 {12 var collection = Mocks.TestCollection(definition: Reflector.Wrap(typeof(BeforeAfterCollection)));13 var testCase = TestData.XunitTestCase<ClassUnderTest>("Passing", collection);14 var runner = new TestableXunitTestCaseRunnerBase();15 var result = runner.GetBeforeAfterTestAttributes(testCase);16 Assert.Collection(17 result.OrderBy(a => a.GetType().Name),18 attr => Assert.IsType<BeforeAfterOnAssembly>(attr),19 attr => Assert.IsType<BeforeAfterOnClass>(attr),20 attr => Assert.IsType<BeforeAfterOnCollection>(attr),21 attr => Assert.IsType<BeforeAfterOnMethod>(attr)22 );23 }24 [BeforeAfterOnCollection]25 class BeforeAfterCollection { }26 [BeforeAfterOnClass]...

Full Screen

Full Screen

TestCollection

Using AI Code Generation

copy

Full Screen

1using Xunit;2using Xunit.v3;3{4 public void TestMethod()5 {6 var testCollection = Xunit.v3.Mocks.TestCollection("Test Collection");7 }8}9{10 {11 IAssemblyInfo Assembly { get; }12 string CollectionDefinition { get; }13 string DisplayName { get; }14 string Name { get; }15 }16}17using Xunit;18using Xunit.v3;19{20 public void TestMethod()21 {22 var testCollection = Xunit.v3.Mocks.TestCollection("Test Collection");23 var assembly = testCollection.Assembly;24 var collectionDefinition = testCollection.CollectionDefinition;25 var displayName = testCollection.DisplayName;26 var name = testCollection.Name;27 }28}29using Xunit;

Full Screen

Full Screen

TestCollection

Using AI Code Generation

copy

Full Screen

1{2 {3 public void Test1()4 {5 Xunit.v3.Mocks.TestCollection("Test1");6 }7 }8}

Full Screen

Full Screen

TestCollection

Using AI Code Generation

copy

Full Screen

1var mock = new Xunit.v3.Mocks();2var testCollection = mock.TestCollection("TestCollectionDisplayName");3var testMethod = mock.TestMethod("TestMethodName", "TestMethodSignature", "TestMethodClass", "TestMethodMethod");4var testClass = mock.TestClass("TestClass", "TestClassClass");5var testCase = mock.TestCase("TestCaseDisplayName", "TestCaseSkipReason", "TestCaseSourceInformation", testMethod, testClass, testCollection);6var testAssembly = mock.TestAssembly("TestAssemblyName", "TestAssemblyAssembly");7var testCollection = mock.TestCollection("TestCollectionDisplayName");8var testMethod = mock.TestMethod("TestMethodName", "TestMethodSignature", "TestMethodClass", "TestMethodMethod");9var testClass = mock.TestClass("TestClass", "TestClassClass");10var testCase = mock.TestCase("TestCaseDisplayName", "TestCaseSkipReason", "TestCaseSourceInformation", testMethod, testClass, testCollection);11var testAssembly = mock.TestAssembly("TestAssemblyName", "TestAssemblyAssembly");12var testCollection = mock.TestCollection("TestCollectionDisplayName");13var testMethod = mock.TestMethod("TestMethodName", "TestMethodSignature", "TestMethodClass", "TestMethodMethod");14var testClass = mock.TestClass("TestClass", "TestClassClass");15var testCase = mock.TestCase("TestCaseDisplayName", "TestCaseSkipReason", "TestCaseSourceInformation", testMethod, testClass, testCollection);16var testAssembly = mock.TestAssembly("TestAssemblyName", "TestAssemblyAssembly

Full Screen

Full Screen

TestCollection

Using AI Code Generation

copy

Full Screen

1using Xunit.v3;2using Xunit.v3.Mocks;3using Xunit.v3.xUnitMock;4using Xunit.v3.xUnitMock.Attributes;5using Xunit.v3.xUnitMock.Interfaces;6using Xunit.v3.xUnitMock.Models;7using Xunit.v3.xUnitMock.Services;8{9 {10 public void TestCollection()11 {12 var mock = new Mock<IClassFixture<TestClass>>();13 var testClass = new TestClass();14 mock.Setup(x => x.Fixture).Returns(testClass);15 var collection = new TestCollection<TestClass>(mock.Object);16 var service = new TestCollectionService();17 service.Add(collection);18 var result = service.Get<TestClass>();19 Assert.Equal(testClass, result);20 Assert.Equal(collection, service.Get<TestClass>());21 }22 }23}

Full Screen

Full Screen

TestCollection

Using AI Code Generation

copy

Full Screen

1var collection = Xunit.v3.Mocks.TestCollection("TestCollection");2var testMethod = Xunit.v3.Mocks.TestMethod("TestMethod");3var testClass = Xunit.v3.Mocks.TestClass("TestClass");4var testAssembly = Xunit.v3.Mocks.TestAssembly("TestAssembly");5var testCase = Xunit.v3.Mocks.TestCase("TestCase");6var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");7var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");8var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");9var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");10var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");11var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");12var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");13var testCollection = Xunit.v3.Mocks.TestCollection("TestCollection");

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful