diff --git a/src/KiBoards.Tests/UnitTest1.cs b/src/KiBoards.Tests/UnitTest1.cs index d71db3e..8ffea7b 100644 --- a/src/KiBoards.Tests/UnitTest1.cs +++ b/src/KiBoards.Tests/UnitTest1.cs @@ -16,7 +16,7 @@ public UnitTest1(TestContextFixture testContextFixture, ITestOutputHelper testOu _testContextFixture = testContextFixture; _testOutputHelper = testOutputHelper; - _testContextFixture.SetContext(new { Version = "Context via Fixture" }); + _testContextFixture.SetContext(new { Version = "Context via Fixture", Hello = "World", Input = 1 }); } [Fact] diff --git a/src/KiBoards.Tests/UnitTest3.cs b/src/KiBoards.Tests/UnitTest3.cs new file mode 100644 index 0000000..9918374 --- /dev/null +++ b/src/KiBoards.Tests/UnitTest3.cs @@ -0,0 +1,49 @@ +using Xunit.Abstractions; + +namespace KiBoards.Tests +{ + + public class UnitTest3 : IClassFixture + { + + public UnitTest3(TestContextFixture testContextFixture, ITestOutputHelper outputHelper) + { + // This is how to get messageBus + + testContextFixture.SetContext(new { Version = "1234" }); + + outputHelper.WriteLine("HELLO WORLD MESSAGE BUS"); + } + + [Fact(Timeout = 1000)] + public void Test5() + { + Thread.Sleep(5000); + + + } + + [Theory] + [InlineData(1, 2)] + [InlineData(2, 2)] + [InlineData(3, 3)] + public void Test6(int a, int b) + { + Assert.Equal(a, b); + } + + + [Fact] + public void Test7() + { + throw new NotImplementedException(); + } + + [Fact(Skip = "Not required.")] + public void Test8() + { + + } + + } +} \ No newline at end of file diff --git a/src/KiBoards/Models/KiBoardsModelsExtensions.cs b/src/KiBoards/Models/KiBoardsModelsExtensions.cs index 70b2532..5734afa 100644 --- a/src/KiBoards/Models/KiBoardsModelsExtensions.cs +++ b/src/KiBoards/Models/KiBoardsModelsExtensions.cs @@ -1,4 +1,5 @@ -using Xunit.Abstractions; +using Nest; +using Xunit.Abstractions; using Xunit.Sdk; namespace KiBoards.Models @@ -6,7 +7,7 @@ namespace KiBoards.Models internal static class KiBoardsModelsExtensions { - internal static KiBoardsTestCaseStatusDto ToKiBoardsTestCase(this IXunitTestCase testCase, ITestMethod testMethod, KiBoardsTestCaseStatus status, KiBoardsTestCaseState state) => new KiBoardsTestCaseStatusDto() + internal static KiBoardsTestCaseStatusDto ToKiBoardsTestCase(this IXunitTestCase testCase, ITestMethod testMethod, KiBoardsTestCaseStatus status, KiBoardsTestCaseState state, object context = null) => new KiBoardsTestCaseStatusDto() { UniqueId = testCase.UniqueID, DisplayName = testCase.DisplayName, @@ -24,12 +25,13 @@ internal static class KiBoardsModelsExtensions { Name = testMethod?.Method.Name } - } + }, + Context = context }; - internal static IEnumerable ToKiBoardsTestCases(this IEnumerable testCases, KiBoardsTestCaseStatus status, KiBoardsTestCaseState state) => - testCases.Select(x => x.ToKiBoardsTestCase(null, status, state)); + internal static IEnumerable ToKiBoardsTestCases(this IEnumerable testCases, KiBoardsTestCaseStatus status, KiBoardsTestCaseState state, object context = null) => + testCases.Select(x => x.ToKiBoardsTestCase(null, status, state, context)); internal static KiBoardsTestCaseStatus ToKiBoardsTestCaseStatus(this RunSummary summary) => summary.Failed > 0 ? KiBoardsTestCaseStatus.Failure : summary.Skipped > 0 ? KiBoardsTestCaseStatus.Skipped : KiBoardsTestCaseStatus.Success; diff --git a/src/KiBoards/Models/KiBoardsTestCaseStatusDto.cs b/src/KiBoards/Models/KiBoardsTestCaseStatusDto.cs index 4b5602c..11043de 100644 --- a/src/KiBoards/Models/KiBoardsTestCaseStatusDto.cs +++ b/src/KiBoards/Models/KiBoardsTestCaseStatusDto.cs @@ -9,5 +9,6 @@ class KiBoardsTestCaseStatusDto public string State { get; set; } public string SkipReason { get; set; } public KiBoardsTestMethodDto TestMethod { get; set; } + public object Context { get; set; } } } diff --git a/src/KiBoards/Services/IKiBoardsTestRunnerService.cs b/src/KiBoards/Services/IKiBoardsTestRunnerService.cs index f1f409d..4713b0b 100644 --- a/src/KiBoards/Services/IKiBoardsTestRunnerService.cs +++ b/src/KiBoards/Services/IKiBoardsTestRunnerService.cs @@ -6,11 +6,13 @@ namespace KiBoards.Services internal interface IKiBoardsTestRunnerService { Guid RunId { get; } + Task BeginTestCasesRunAsync(IEnumerable testCases); Task StartTestCaseAsync(IXunitTestCase testCase, ITestMethod testMethod); Task FinishTestCaseAsync(IXunitTestCase testCase, ITestMethod testMethod, ExceptionAggregator exceptionAggregator, RunSummary result); Task ErrorTestCaseAsync(IXunitTestCase testCase, ITestMethod testMethod, Exception ex); Task EndTestCasesRunAsync(RunSummary results); Task ErrorTestCasesRunAsync(IEnumerable testCases, Exception ex); + void SetContext(ITestContextMessage testContext); } } \ No newline at end of file diff --git a/src/KiBoards/Services/KiBoardsTestRunnerService.cs b/src/KiBoards/Services/KiBoardsTestRunnerService.cs index 6489269..cb767ac 100644 --- a/src/KiBoards/Services/KiBoardsTestRunnerService.cs +++ b/src/KiBoards/Services/KiBoardsTestRunnerService.cs @@ -1,10 +1,5 @@ using KiBoards.Models; -using System; -using System.Collections.Generic; -using System.Linq; using System.Reflection; -using System.Security.Cryptography; -using System.Threading.Tasks; using Xunit.Abstractions; using Xunit.Sdk; @@ -17,6 +12,8 @@ internal class KiBoardsTestRunnerService : IKiBoardsTestRunnerService, IDisposab public Guid RunId { get; private set; } = Guid.NewGuid(); + public object Context { get; private set; } + public KiBoardsTestRunnerService(IMessageSink messageSink, IKiBoardsElasticService elasticService) { _messageSink = messageSink; @@ -38,7 +35,7 @@ public async Task BeginTestCasesRunAsync(IEnumerable testCases) //foreach (var testCase in testCases) // _messageSink.OnMessage(new DiagnosticMessage($"Discovered: {testCase.UniqueID} {testCase.DisplayName}")); - await _elasticService.IndexTestCasesAsync(testCases.ToKiBoardsTestCases(KiBoardsTestCaseStatus.Discovered, KiBoardsTestCaseState.Active)); + await _elasticService.IndexTestCasesAsync(testCases.ToKiBoardsTestCases(KiBoardsTestCaseStatus.Discovered, KiBoardsTestCaseState.Active, Context)); } @@ -51,13 +48,13 @@ public async Task ErrorTestCaseAsync(IXunitTestCase testCase, ITestMethod testMe public async Task FinishTestCaseAsync(IXunitTestCase testCase, ITestMethod testMethod, ExceptionAggregator exceptionAggregator, RunSummary summary) { //_messageSink.OnMessage(new DiagnosticMessage($"{(summary.Failed > 0 ? "Failure" : summary.Skipped > 0 ? "Skipped" : "Success")}: {testCase.UniqueID} {testCase.DisplayName} ({summary.Time}s)")); - await _elasticService.IndexTestCaseAsync(testCase.ToKiBoardsTestCase(testMethod, summary.ToKiBoardsTestCaseStatus(), KiBoardsTestCaseState.Inactive)); + await _elasticService.IndexTestCaseAsync(testCase.ToKiBoardsTestCase(testMethod, summary.ToKiBoardsTestCaseStatus(), KiBoardsTestCaseState.Inactive, Context)); } public async Task StartTestCaseAsync(IXunitTestCase testCase, ITestMethod testMethod) { //_messageSink.OnMessage(new DiagnosticMessage($"Started: {testCase.UniqueID} {testCase.DisplayName}")); - await _elasticService.IndexTestCaseAsync(testCase.ToKiBoardsTestCase(testMethod, KiBoardsTestCaseStatus.Running, KiBoardsTestCaseState.Active)); + await _elasticService.IndexTestCaseAsync(testCase.ToKiBoardsTestCase(testMethod, KiBoardsTestCaseStatus.Running, KiBoardsTestCaseState.Active, Context)); } @@ -73,5 +70,10 @@ public async Task ErrorTestCasesRunAsync(IEnumerable testCases, //_messageSink.OnMessage(new DiagnosticMessage($"Fatal: Run {RunId} failed. ({ex.Message})")); await Task.CompletedTask; } + + public void SetContext(ITestContextMessage testContext) + { + Context = testContext.Context; + } } } diff --git a/src/KiBoards/TestFramework.cs b/src/KiBoards/TestFramework.cs index b48949b..b6b9dd3 100644 --- a/src/KiBoards/TestFramework.cs +++ b/src/KiBoards/TestFramework.cs @@ -17,7 +17,7 @@ public TestFramework(IMessageSink messageSink) serviceCollection .AddSingleton(messageSink) - .AddElasticServices() + .AddElasticServices() .AddSingleton(); _serviceProvider = serviceCollection.BuildServiceProvider(); @@ -27,6 +27,7 @@ protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyNa { return new TestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink, _serviceProvider.GetRequiredService()); } + public new async void Dispose() { await Task.Delay(1); @@ -51,6 +52,7 @@ protected override async void RunTestCases(IEnumerable testCases { await _testRunner.BeginTestCasesRunAsync(testCases); using var assemblyRunner = new TestAssemblyRunner(TestAssembly, testCases, new TestMessageSink(DiagnosticMessageSink, _testRunner), executionMessageSink, executionOptions, _testRunner); + var results = await assemblyRunner.RunAsync(); await _testRunner.EndTestCasesRunAsync(results); } @@ -75,6 +77,9 @@ public TestAssemblyRunner(ITestAssembly testAssembly, IEnumerable RunTestCollectionAsync(IMessageBus messageBus, ITestCollection testCollection, IEnumerable testCases, CancellationTokenSource cancellationTokenSource) { var collectionRunner = new TestCollectionRunner(testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource, _testRunner); diff --git a/src/KiBoards/TestMessageSink.cs b/src/KiBoards/TestMessageSink.cs index cfcbf74..42139c2 100644 --- a/src/KiBoards/TestMessageSink.cs +++ b/src/KiBoards/TestMessageSink.cs @@ -47,6 +47,8 @@ private void HandleMessageSinkMessage(IMessageSinkMessage message) case ITestContextMessage testContext: LogMessage($"TestContext: {testContext}"); + _testRunner.SetContext(testContext); + break; case ITestAssemblyStarting testAssemblyStarting: @@ -148,7 +150,7 @@ private void HandleMessageSinkMessage(IMessageSinkMessage message) break; default: - LogMessage($"UNKNOWN: {message.GetType().Name}"); + LogMessage($"UNKNOWN: {message.GetType().Name}: {message}"); break; } } diff --git a/src/KiBoards/TestResultBus.cs b/src/KiBoards/TestResultBus.cs index 87a3000..6aa1c6b 100644 --- a/src/KiBoards/TestResultBus.cs +++ b/src/KiBoards/TestResultBus.cs @@ -5,7 +5,7 @@ namespace KiBoards { internal class TestResultBus : IMessageBus { - private IMessageBus _messageBus; + private readonly IMessageBus _messageBus; public ITestResultMessage TestResult { get; private set; }