Skip to content

Using Xunit test output helper in some tests #1362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/WorkflowCore.Testing/WorkflowCore.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
<ProjectReference Include="..\..\src\WorkflowCore\WorkflowCore.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="xunit.abstractions">
<HintPath>..\..\..\..\..\.nuget\packages\xunit.abstractions\2.0.3\lib\netstandard2.0\xunit.abstractions.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions src/WorkflowCore.Testing/WorkflowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using Xunit.Abstractions;

namespace WorkflowCore.Testing
{
Expand All @@ -18,11 +19,16 @@ public abstract class WorkflowTest<TWorkflow, TData> : IDisposable
protected IPersistenceProvider PersistenceProvider;
protected List<StepError> UnhandledStepErrors = new List<StepError>();

protected virtual void Setup()
protected virtual void Setup(ITestOutputHelper testOutputHelper = null)
{
//setup dependency injection
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddLogging(l => l.SetMinimumLevel(LogLevel.Trace));
services.AddSingleton<ILoggerProvider>(p => new XUnitLoggerProvider(testOutputHelper));
services.Add(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.Add(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(XUnitLogger<>)));
services.AddSingleton(sp => testOutputHelper);

ConfigureServices(services);

var serviceProvider = services.BuildServiceProvider();
Expand Down
91 changes: 91 additions & 0 deletions src/WorkflowCore.Testing/XUnitLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Text;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

namespace WorkflowCore.Testing
{
internal class XUnitLogger : ILogger
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly string _categoryName;
private readonly LoggerExternalScopeProvider _scopeProvider;

public static ILogger CreateLogger(ITestOutputHelper testOutputHelper) =>
new XUnitLogger(testOutputHelper, new LoggerExternalScopeProvider(), "");

public static ILogger<T> CreateLogger<T>(ITestOutputHelper testOutputHelper) =>
new XUnitLogger<T>(testOutputHelper, new LoggerExternalScopeProvider());

public XUnitLogger(ITestOutputHelper testOutputHelper, LoggerExternalScopeProvider scopeProvider,
string categoryName)
{
_testOutputHelper = testOutputHelper;
_scopeProvider = scopeProvider;
_categoryName = categoryName;
}

public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public IDisposable BeginScope<TState>(TState state) => _scopeProvider.Push(state);

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
Func<TState, Exception, string> formatter)
{
if (_testOutputHelper == null) return;
var sb = new StringBuilder();
sb.Append(DateTime.Now.ToString("HH:mm:ss.fff"))
.Append(" ")
.Append(GetLogLevelString(logLevel))
.Append(" [").Append(_categoryName).Append("] ")
.Append(formatter(state, exception));

if (exception != null)
{
sb.Append('\n').Append(exception);
}

// Append scopes
_scopeProvider.ForEachScope((scope, s) =>
{
s.Append("\n => ");
s.Append(scope);
}, sb);

_testOutputHelper.WriteLine(sb.ToString());
}

private static string GetLogLevelString(LogLevel logLevel)
{
return logLevel.ToString().ToUpper();
}
}

internal sealed class XUnitLogger<T> : XUnitLogger, ILogger<T>
{
public XUnitLogger(ITestOutputHelper testOutputHelper, LoggerExternalScopeProvider scopeProvider)
: base(testOutputHelper, scopeProvider, typeof(T).FullName)
{
}
}

internal sealed class XUnitLoggerProvider : ILoggerProvider
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly LoggerExternalScopeProvider _scopeProvider = new LoggerExternalScopeProvider();

public XUnitLoggerProvider(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

public ILogger CreateLogger(string categoryName)
{
return new XUnitLogger(_testOutputHelper, _scopeProvider, categoryName);
}

public void Dispose()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Xunit;
using FluentAssertions;
using WorkflowCore.Testing;
using Xunit.Abstractions;

namespace WorkflowCore.IntegrationTests.Scenarios
{
Expand Down Expand Up @@ -31,9 +32,9 @@ public class MyDataClass

public class DelayScenario : WorkflowTest<DelayWorkflow, DelayWorkflow.MyDataClass>
{
public DelayScenario()
public DelayScenario(ITestOutputHelper testOutputHelper)
{
Setup();
Setup(testOutputHelper);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using MongoDB.Bson.Serialization;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.MongoDB.Scenarios
{
[Collection("Mongo collection")]
public class MongoDelayScenario : DelayScenario
{
public MongoDelayScenario() : base()
public MongoDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
BsonClassMap.RegisterClassMap<DelayWorkflow.MyDataClass>(map => map.AutoMap());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
using System;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.MySQL.Scenarios
{
[Collection("Mysql collection")]
public class MysqlDelayScenario : DelayScenario
{
public MysqlDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(cfg =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
using WorkflowCore.Tests.Oracle;

using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.Oracle.Scenarios
{
[Collection("Oracle collection")]
public class OracleDelayScenario : DelayScenario
{
{
public OracleDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(cfg =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.PostgreSQL.Scenarios
{
[Collection("Postgres collection")]
public class PostgresDelayScenario : DelayScenario
{
{
public PostgresDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(cfg =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.SqlServer.Scenarios
{
[Collection("SqlServer collection")]
public class SqlServerDelayScenario : DelayScenario
{
{
public SqlServerDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(cfg =>
Expand Down
26 changes: 26 additions & 0 deletions test/WorkflowCore.Tests.Sqlite/Scenarios/SqliteDelayScenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.IntegrationTests.Scenarios;
using Xunit;
using Xunit.Abstractions;

namespace WorkflowCore.Tests.Sqlite.Scenarios
{
[Collection("Sqlite collection")]
public class SqliteDelayScenario : DelayScenario
{
public SqliteDelayScenario(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void ConfigureServices(IServiceCollection services)
{
services.AddWorkflow(cfg =>
{
cfg.UseSqlite($"Data Source=wfc-tests-{DateTime.Now.Ticks}.db;", true);
cfg.UsePollInterval(TimeSpan.FromSeconds(2));
});
}
}
}