diff --git a/src/KiBoards.Xunit/Models/KiBoardsTestCaseRun.cs b/src/KiBoards.Xunit/Models/KiBoardsTestCaseRun.cs index 52dc4ad..043ce24 100644 --- a/src/KiBoards.Xunit/Models/KiBoardsTestCaseRun.cs +++ b/src/KiBoards.Xunit/Models/KiBoardsTestCaseRun.cs @@ -3,7 +3,7 @@ class KiBoardsTestCaseRun { public string Id { get; internal set; } - public TestRun TestRun { get; set; } + public KiBoardsTestRun TestRun { get; set; } public KiBoardsTestCase TestCase { get; set; } public KiBoardsTestCaseRunSkipped Skipped { get; set; } public KiBoardsTestCaseRunFailed Failed { get; set; } diff --git a/src/KiBoards.Xunit/Models/KiBoardsTestRun.cs b/src/KiBoards.Xunit/Models/KiBoardsTestRun.cs new file mode 100644 index 0000000..6d13ce1 --- /dev/null +++ b/src/KiBoards.Xunit/Models/KiBoardsTestRun.cs @@ -0,0 +1,19 @@ +using Nest; +using System.Collections; + +namespace KiBoards.Models +{ + public class KiBoardsTestRun + { + public string Id { get; internal set; } + public string Name { get; internal set; } + public string Hash { get; internal set; } + public DateTime StartTime { get; internal set; } + public string MachineName { get; internal set; } + public string UserName { get; internal set; } + public Dictionary Variables { get; internal set; } + + [Object] + internal KiBoardsTestRunSummary Summary { get; set; } + } +} diff --git a/src/KiBoards.Xunit/TestRunSummary.cs b/src/KiBoards.Xunit/Models/KiBoardsTestRunSummary.cs similarity index 92% rename from src/KiBoards.Xunit/TestRunSummary.cs rename to src/KiBoards.Xunit/Models/KiBoardsTestRunSummary.cs index 6760fd9..20c7d07 100644 --- a/src/KiBoards.Xunit/TestRunSummary.cs +++ b/src/KiBoards.Xunit/Models/KiBoardsTestRunSummary.cs @@ -1,6 +1,6 @@ -namespace KiBoards +namespace KiBoards.Models { - class TestRunSummary + class KiBoardsTestRunSummary { /// /// The total number of tests run. diff --git a/src/KiBoards.Xunit/TestFramework.cs b/src/KiBoards.Xunit/TestFramework.cs index 05710bd..1028a5a 100644 --- a/src/KiBoards.Xunit/TestFramework.cs +++ b/src/KiBoards.Xunit/TestFramework.cs @@ -1,6 +1,4 @@ using System.Reflection; -using System.Security.Cryptography; -using System.Text; using KiBoards.Services; using Xunit.Abstractions; using Xunit.Sdk; @@ -8,17 +6,16 @@ namespace KiBoards { public class TestFramework : XunitTestFramework, IDisposable - { - public static readonly TestRun TestRun = new(); + { + readonly KiBoardsTestRunner _testRunner; - readonly KiBoardsTestRunner _testRunner; - public TestFramework(IMessageSink messageSink) : base(messageSink) - { + { _testRunner = new KiBoardsTestRunner(messageSink); } + protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName) { return new TestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink, _testRunner); @@ -34,30 +31,23 @@ public TestFrameworkExecutor(AssemblyName assemblyName, ISourceInformationProvid : base(assemblyName, sourceInformationProvider, diagnosticMessageSink) { _testRunner = testRunner; - _diagnosticMessageSink = diagnosticMessageSink; + _diagnosticMessageSink = diagnosticMessageSink; } + + protected override async void RunTestCases(IEnumerable testCases, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions) { try { using var assemblyRunner = new TestAssemblyRunner(TestAssembly, testCases, DiagnosticMessageSink, executionMessageSink, executionOptions, _testRunner); - var summary = await assemblyRunner.RunAsync(); - - TestRun.Summary = new TestRunSummary() - { - Total = summary.Total, - Failed = summary.Failed, - Skipped = summary.Skipped, - Time = summary.Time, - }; - - await _testRunner.IndexTestRunAsync(TestRun); + var summary = await assemblyRunner.RunAsync(); + await _testRunner.IndexTestRunAsync(summary); } catch (Exception ex) { - _diagnosticMessageSink.OnMessage(new DiagnosticMessage(ex.Message, ex.StackTrace)); + _diagnosticMessageSink.WriteException(ex); } } } @@ -80,11 +70,7 @@ protected override async Task RunTestCollectionAsync(IMessageBus mes return await collectionRunner.RunAsync(); } - protected override string GetTestFrameworkDisplayName() - { - var version = Assembly.GetExecutingAssembly().GetCustomAttribute()?.InformationalVersion; - return $"KiBoards {version}"; - } + protected override string GetTestFrameworkDisplayName() => $"KiBoards {_testRunner.Version}"; } @@ -110,21 +96,14 @@ private class TestClassRunner : XunitTestClassRunner public TestClassRunner(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ITestCaseOrderer testCaseOrderer, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, IDictionary collectionFixtureMappings, KiBoardsTestRunner testRunner) : base(testClass, @class, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings) - { - TestRun.Name = string.Join(",", testCases.Select(a => Path.GetFileNameWithoutExtension(a.TestMethod.TestClass.Class.Assembly.AssemblyPath)).Distinct()); - TestRun.Hash = string.Join(",", testCases.OrderBy(a => a.UniqueID).Select(a => a.UniqueID)).ComputeMD5(); - + { _testRunner = testRunner; + testRunner.AddTestCases(testCases); } protected override Task RunTestMethodAsync(ITestMethod testMethod, IReflectionMethodInfo method, IEnumerable testCases, object[] constructorArguments) => new TestMethodRunner(testMethod, Class, method, testCases, DiagnosticMessageSink, new TestResultBus(MessageBus), new ExceptionAggregator(Aggregator), CancellationTokenSource, constructorArguments, _testRunner) - .RunAsync(); - - protected override async Task RunTestMethodsAsync() - { - return await base.RunTestMethodsAsync(); - } + .RunAsync(); } @@ -177,7 +156,7 @@ protected override async Task RunTestCaseAsync(IXunitTestCase testCa } catch (Exception ex) { - _diagnosticMessageSink.OnMessage(new DiagnosticMessage(ex.Message, ex.StackTrace)); + _diagnosticMessageSink.WriteException(ex); } return result; diff --git a/src/KiBoards.Xunit/TestRun.cs b/src/KiBoards.Xunit/TestRun.cs deleted file mode 100644 index f74d8fe..0000000 --- a/src/KiBoards.Xunit/TestRun.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Nest; - -namespace KiBoards -{ - public class TestRun - { - public string Id { get; internal set; } - public string Name { get; internal set; } - public string Hash { get; internal set; } - public DateTime StartTime { get; internal set; } - public string MachineName { get; internal set; } - public string UserName { get; internal set; } - - [Object] - internal TestRunSummary Summary { get; set; } - - public TestRun() - { - Id = Environment.GetEnvironmentVariable("KIB_TEST_RUN_ID") ?? Guid.NewGuid().ToString(); - StartTime = DateTime.UtcNow; - MachineName = Environment.MachineName; - UserName = Environment.UserName; - } - } -}