Skip to content

Commit

Permalink
Add test case context indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandini committed Sep 21, 2023
1 parent a62c381 commit 8b7203e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/KiBoards.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
49 changes: 49 additions & 0 deletions src/KiBoards.Tests/UnitTest3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Xunit.Abstractions;

namespace KiBoards.Tests
{

public class UnitTest3 : IClassFixture<TestContextFixture>
{

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()
{

}

}
}
12 changes: 7 additions & 5 deletions src/KiBoards/Models/KiBoardsModelsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using Xunit.Abstractions;
using Nest;
using Xunit.Abstractions;
using Xunit.Sdk;

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,
Expand All @@ -24,12 +25,13 @@ internal static class KiBoardsModelsExtensions
{
Name = testMethod?.Method.Name
}
}
},
Context = context
};


internal static IEnumerable<KiBoardsTestCaseStatusDto> ToKiBoardsTestCases(this IEnumerable<IXunitTestCase> testCases, KiBoardsTestCaseStatus status, KiBoardsTestCaseState state) =>
testCases.Select(x => x.ToKiBoardsTestCase(null, status, state));
internal static IEnumerable<KiBoardsTestCaseStatusDto> ToKiBoardsTestCases(this IEnumerable<IXunitTestCase> 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;
Expand Down
1 change: 1 addition & 0 deletions src/KiBoards/Models/KiBoardsTestCaseStatusDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
2 changes: 2 additions & 0 deletions src/KiBoards/Services/IKiBoardsTestRunnerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ namespace KiBoards.Services
internal interface IKiBoardsTestRunnerService
{
Guid RunId { get; }

Task BeginTestCasesRunAsync(IEnumerable<IXunitTestCase> 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<IXunitTestCase> testCases, Exception ex);
void SetContext(ITestContextMessage testContext);
}
}
18 changes: 10 additions & 8 deletions src/KiBoards/Services/KiBoardsTestRunnerService.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -38,7 +35,7 @@ public async Task BeginTestCasesRunAsync(IEnumerable<IXunitTestCase> 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));
}


Expand All @@ -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));
}


Expand All @@ -73,5 +70,10 @@ public async Task ErrorTestCasesRunAsync(IEnumerable<IXunitTestCase> testCases,
//_messageSink.OnMessage(new DiagnosticMessage($"Fatal: Run {RunId} failed. ({ex.Message})"));
await Task.CompletedTask;
}

public void SetContext(ITestContextMessage testContext)
{
Context = testContext.Context;
}
}
}
7 changes: 6 additions & 1 deletion src/KiBoards/TestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public TestFramework(IMessageSink messageSink)

serviceCollection
.AddSingleton(messageSink)
.AddElasticServices()
.AddElasticServices()
.AddSingleton<IKiBoardsTestRunnerService, KiBoardsTestRunnerService>();

_serviceProvider = serviceCollection.BuildServiceProvider();
Expand All @@ -27,6 +27,7 @@ protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyNa
{
return new TestFrameworkExecutor(assemblyName, SourceInformationProvider, DiagnosticMessageSink, _serviceProvider.GetRequiredService<IKiBoardsTestRunnerService>());
}

public new async void Dispose()
{
await Task.Delay(1);
Expand All @@ -51,6 +52,7 @@ protected override async void RunTestCases(IEnumerable<IXunitTestCase> 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);
}
Expand All @@ -75,6 +77,9 @@ public TestAssemblyRunner(ITestAssembly testAssembly, IEnumerable<IXunitTestCase
_messageSink = diagnosticMessageSink;
}




protected override async Task<RunSummary> RunTestCollectionAsync(IMessageBus messageBus, ITestCollection testCollection, IEnumerable<IXunitTestCase> testCases, CancellationTokenSource cancellationTokenSource)
{
var collectionRunner = new TestCollectionRunner(testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource, _testRunner);
Expand Down
4 changes: 3 additions & 1 deletion src/KiBoards/TestMessageSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private void HandleMessageSinkMessage(IMessageSinkMessage message)

case ITestContextMessage testContext:
LogMessage($"TestContext: {testContext}");
_testRunner.SetContext(testContext);

break;

case ITestAssemblyStarting testAssemblyStarting:
Expand Down Expand Up @@ -148,7 +150,7 @@ private void HandleMessageSinkMessage(IMessageSinkMessage message)
break;

default:
LogMessage($"UNKNOWN: {message.GetType().Name}");
LogMessage($"UNKNOWN: {message.GetType().Name}: {message}");
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/KiBoards/TestResultBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace KiBoards
{
internal class TestResultBus : IMessageBus
{
private IMessageBus _messageBus;
private readonly IMessageBus _messageBus;

public ITestResultMessage TestResult { get; private set; }

Expand Down

0 comments on commit 8b7203e

Please sign in to comment.