/// <summary> /// Attempts to retrieve <see cref="_ITestMetadata"/> from the cache, and if present, /// removes the metadata from the cache. /// </summary> /// <param name="message">The message that indicates which metadata to retrieve.</param> /// <returns>The cached metadata, if present; or <c>null</c> if there isn't any.</returns> public _ITestMetadata?TryRemove(_TestFinished message) { Guard.ArgumentNotNull(nameof(message), message); return((_ITestMetadata?)InternalGetAndRemove(message.TestUniqueID, true)); }
/// <summary/> public static RunSummary FailTestCases( IReadOnlyCollection <_ITestCase> testCases, IMessageBus messageBus, string messageFormat) { var result = new RunSummary(); foreach (var testCase in testCases) { var assemblyUniqueID = testCase.TestCollection.TestAssembly.UniqueID; var testUniqueID = UniqueIDGenerator.ForTest(testCase.UniqueID, -1); var caseStarting = new _TestCaseStarting { AssemblyUniqueID = assemblyUniqueID, SkipReason = testCase.SkipReason, SourceFilePath = testCase.SourceFilePath, SourceLineNumber = testCase.SourceLineNumber, TestCaseDisplayName = testCase.TestCaseDisplayName, TestCaseUniqueID = testCase.UniqueID, TestClassName = testCase.TestClassName, TestClassNamespace = testCase.TestClassNamespace, TestClassNameWithNamespace = testCase.TestClassNameWithNamespace, TestClassUniqueID = testCase.TestClass?.UniqueID, TestCollectionUniqueID = testCase.TestCollection.UniqueID, TestMethodName = testCase.TestMethod?.Method.Name, TestMethodUniqueID = testCase.TestMethod?.UniqueID, Traits = testCase.Traits }; messageBus.QueueMessage(caseStarting); var testStarting = new _TestStarting { AssemblyUniqueID = assemblyUniqueID, TestCaseUniqueID = testCase.UniqueID, TestClassUniqueID = testCase.TestClass?.UniqueID, TestCollectionUniqueID = testCase.TestCollection.UniqueID, TestDisplayName = testCase.TestCaseDisplayName, TestMethodUniqueID = testCase.TestMethod?.UniqueID, TestUniqueID = testUniqueID }; messageBus.QueueMessage(testStarting); var failed = new _TestFailed { AssemblyUniqueID = assemblyUniqueID, Cause = FailureCause.Exception, ExceptionParentIndices = new[] { -1 }, ExceptionTypes = new string?[] { null }, ExecutionTime = 0m, Messages = new[] { string.Format(messageFormat, testCase.TestCaseDisplayName) }, Output = string.Empty, StackTraces = new string?[] { null }, TestCaseUniqueID = testCase.UniqueID, TestClassUniqueID = testCase.TestClass?.UniqueID, TestCollectionUniqueID = testCase.TestCollection.UniqueID, TestMethodUniqueID = testCase.TestMethod?.UniqueID, TestUniqueID = testUniqueID }; messageBus.QueueMessage(failed); var testFinished = new _TestFinished { AssemblyUniqueID = assemblyUniqueID, ExecutionTime = 0m, Output = string.Empty, TestCaseUniqueID = testCase.UniqueID, TestClassUniqueID = testCase.TestClass?.UniqueID, TestCollectionUniqueID = testCase.TestCollection.UniqueID, TestMethodUniqueID = testCase.TestMethod?.UniqueID, TestUniqueID = testUniqueID }; messageBus.QueueMessage(testFinished); var caseFinished = new _TestCaseFinished { AssemblyUniqueID = assemblyUniqueID, ExecutionTime = 0m, TestCaseUniqueID = testCase.UniqueID, TestClassUniqueID = testCase.TestClass?.UniqueID, TestCollectionUniqueID = testCase.TestCollection.UniqueID, TestMethodUniqueID = testCase.TestMethod?.UniqueID, TestsFailed = 1, TestsRun = 1, TestsSkipped = 0 }; messageBus.QueueMessage(caseFinished); result.Total++; result.Failed++; } return(result); }
/// <summary> /// Runs the test. /// </summary> /// <param name="ctxt">The context that describes the current test</param> /// <returns>Returns summary information about the test that was run.</returns> protected async ValueTask <RunSummary> RunAsync(TContext ctxt) { await ctxt.InitializeAsync(); try { SetTestContext(ctxt, TestEngineStatus.Initializing); var runSummary = new RunSummary { Total = 1 }; var output = string.Empty; var testAssemblyUniqueID = ctxt.Test.TestCase.TestCollection.TestAssembly.UniqueID; var testCollectionUniqueID = ctxt.Test.TestCase.TestCollection.UniqueID; var testClassUniqueID = ctxt.Test.TestCase.TestClass?.UniqueID; var testMethodUniqueID = ctxt.Test.TestCase.TestMethod?.UniqueID; var testCaseUniqueID = ctxt.Test.TestCase.UniqueID; var testUniqueID = ctxt.Test.UniqueID; var testStarting = new _TestStarting { AssemblyUniqueID = testAssemblyUniqueID, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestDisplayName = ctxt.Test.DisplayName, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = testUniqueID }; if (!ctxt.MessageBus.QueueMessage(testStarting)) { ctxt.CancellationTokenSource.Cancel(); } else { AfterTestStarting(ctxt); _TestResultMessage testResult; if (!string.IsNullOrEmpty(ctxt.SkipReason)) { runSummary.Skipped++; testResult = new _TestSkipped { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = 0m, Output = "", Reason = ctxt.SkipReason, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = testUniqueID }; } else { if (!ctxt.Aggregator.HasExceptions) { var tuple = await ctxt.Aggregator.RunAsync(() => InvokeTestAsync(ctxt), (0m, string.Empty)); if (tuple.HasValue) { runSummary.Time = tuple.Value.ExecutionTime; output = tuple.Value.Output; } } var exception = ctxt.Aggregator.ToException(); if (exception == null) { testResult = new _TestPassed { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = runSummary.Time, Output = output, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = testUniqueID }; } // We don't want a strongly typed contract here; any exception can be a dynamically // skipped exception so long as its message starts with the special token. else if (exception.Message.StartsWith(DynamicSkipToken.Value)) { testResult = new _TestSkipped { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = runSummary.Time, Output = output, Reason = exception.Message.Substring(DynamicSkipToken.Value.Length), TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = testUniqueID }; runSummary.Skipped++; } else { testResult = _TestFailed.FromException( exception, testAssemblyUniqueID, testCollectionUniqueID, testClassUniqueID, testMethodUniqueID, testCaseUniqueID, testUniqueID, runSummary.Time, output ); runSummary.Failed++; } } SetTestContext(ctxt, TestEngineStatus.CleaningUp, TestState.FromTestResult(testResult)); if (!ctxt.CancellationTokenSource.IsCancellationRequested) { if (!ctxt.MessageBus.QueueMessage(testResult)) { ctxt.CancellationTokenSource.Cancel(); } } ctxt.Aggregator.Clear(); BeforeTestFinished(ctxt); if (ctxt.Aggregator.HasExceptions) { var testCleanupFailure = _TestCleanupFailure.FromException( ctxt.Aggregator.ToException() !, testAssemblyUniqueID, testCollectionUniqueID, testClassUniqueID, testMethodUniqueID, testCaseUniqueID, testUniqueID ); if (!ctxt.MessageBus.QueueMessage(testCleanupFailure)) { ctxt.CancellationTokenSource.Cancel(); } } var testFinished = new _TestFinished { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = runSummary.Time, Output = output, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = testUniqueID }; if (!ctxt.MessageBus.QueueMessage(testFinished)) { ctxt.CancellationTokenSource.Cancel(); } } return(runSummary); } finally { await ctxt.DisposeAsync(); } }
RunSummary RunTest_DataDiscoveryException() { // Use -1 for the index here so we don't collide with any legitimate test case IDs that might've been used var test = new XunitTest(TestCase, DisplayName, testIndex: -1); var testAssemblyUniqueID = TestCase.TestMethod.TestClass.TestCollection.TestAssembly.UniqueID; var testCollectionUniqueID = TestCase.TestMethod.TestClass.TestCollection.UniqueID; var testClassUniqueID = TestCase.TestMethod.TestClass.UniqueID; var testMethodUniqueID = TestCase.TestMethod.UniqueID; var testCaseUniqueID = TestCase.UniqueID; var testStarting = new _TestStarting { AssemblyUniqueID = testAssemblyUniqueID, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestDisplayName = test.DisplayName, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testStarting)) { CancellationTokenSource.Cancel(); } else { var errorMetadata = ExceptionUtility.ExtractMetadata(dataDiscoveryException !.Unwrap()); var testFailed = new _TestFailed { AssemblyUniqueID = testAssemblyUniqueID, ExceptionParentIndices = errorMetadata.ExceptionParentIndices, ExceptionTypes = errorMetadata.ExceptionTypes, ExecutionTime = 0m, Messages = errorMetadata.Messages, Output = "", StackTraces = errorMetadata.StackTraces, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testFailed)) { CancellationTokenSource.Cancel(); } } var testFinished = new _TestFinished { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = 0m, Output = "", TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testFinished)) { CancellationTokenSource.Cancel(); } return(new RunSummary { Total = 1, Failed = 1 }); }
/// <inheritdoc/> protected override Task <RunSummary> RunTestAsync() { // Use -1 for the index here so we don't collide with any legitimate test case IDs that might've been used var test = new XunitTest(TestCase, TestCase.DisplayName, testIndex: -1); var summary = new RunSummary { Total = 1 }; var testAssemblyUniqueID = TestCase.TestMethod.TestClass.TestCollection.TestAssembly.UniqueID; var testCaseUniqueID = TestCase.UniqueID; var testClassUniqueID = TestCase.TestMethod.TestClass.UniqueID; var testCollectionUniqueID = TestCase.TestMethod.TestClass.TestCollection.UniqueID; var testMethodUniqueID = TestCase.TestMethod.UniqueID; var testStarting = new _TestStarting { AssemblyUniqueID = testAssemblyUniqueID, TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestDisplayName = test.DisplayName, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testStarting)) { CancellationTokenSource.Cancel(); } else { summary.Failed = 1; var testFailed = new _TestFailed { AssemblyUniqueID = testAssemblyUniqueID, ExceptionParentIndices = new[] { -1 }, ExceptionTypes = new[] { typeof(InvalidOperationException).FullName }, ExecutionTime = 0m, Messages = new[] { TestCase.ErrorMessage }, StackTraces = new[] { "" }, Output = "", TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testFailed)) { CancellationTokenSource.Cancel(); } var testFinished = new _TestFinished { AssemblyUniqueID = testAssemblyUniqueID, ExecutionTime = 0m, Output = "", TestCaseUniqueID = testCaseUniqueID, TestClassUniqueID = testClassUniqueID, TestCollectionUniqueID = testCollectionUniqueID, TestMethodUniqueID = testMethodUniqueID, TestUniqueID = test.UniqueID }; if (!MessageBus.QueueMessage(testFinished)) { CancellationTokenSource.Cancel(); } } return(Task.FromResult(summary)); }