2025迁移版本,多项规则修改
This commit is contained in:
16
MesETL.Test/XUnit/Configuration/XUnitConfiguration.cs
Normal file
16
MesETL.Test/XUnit/Configuration/XUnitConfiguration.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace TestProject1.XUnit.Configuration;
|
||||
|
||||
public static class XUnitConfiguration
|
||||
{
|
||||
public static IConfiguration Configuration { get; }
|
||||
|
||||
static XUnitConfiguration()
|
||||
{
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(AppContext.BaseDirectory)
|
||||
.AddJsonFile("appsettings.json", false, true)
|
||||
.Build();
|
||||
}
|
||||
}
|
39
MesETL.Test/XUnit/Logging/XUnitLogger.cs
Normal file
39
MesETL.Test/XUnit/Logging/XUnitLogger.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject1.XUnit.Logging;
|
||||
|
||||
/// <summary>
|
||||
/// 适用于Xunit的日志记录器,使用ITestOutputHelper输出
|
||||
/// </summary>
|
||||
public class XunitLogger : ILogger
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
private readonly string _categoryName;
|
||||
|
||||
public XunitLogger(ITestOutputHelper testOutputHelper, string categoryName)
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
_categoryName = categoryName;
|
||||
}
|
||||
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
|
||||
=> NoopDisposable.Instance;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
=> true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
_testOutputHelper.WriteLine($"{_categoryName} [{eventId}] {formatter(state, exception)}");
|
||||
if (exception != null)
|
||||
_testOutputHelper.WriteLine(exception.ToString());
|
||||
}
|
||||
|
||||
private class NoopDisposable : IDisposable
|
||||
{
|
||||
public static readonly NoopDisposable Instance = new();
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
13
MesETL.Test/XUnit/Logging/XUnitLoggerExtensions.cs
Normal file
13
MesETL.Test/XUnit/Logging/XUnitLoggerExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject1.XUnit.Logging;
|
||||
|
||||
internal static class XUnitLoggerExtensions
|
||||
{
|
||||
public static ILoggingBuilder AddXUnitLogger(this ILoggingBuilder builder, ITestOutputHelper output)
|
||||
{
|
||||
builder.AddProvider(new XunitLoggerProvider(output));
|
||||
return builder;
|
||||
}
|
||||
}
|
20
MesETL.Test/XUnit/Logging/XUnitLoggerProvider.cs
Normal file
20
MesETL.Test/XUnit/Logging/XUnitLoggerProvider.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject1.XUnit.Logging;
|
||||
|
||||
public class XunitLoggerProvider : ILoggerProvider
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutputHelper;
|
||||
|
||||
public XunitLoggerProvider(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_testOutputHelper = testOutputHelper;
|
||||
}
|
||||
|
||||
public ILogger CreateLogger(string categoryName)
|
||||
=> new XunitLogger(_testOutputHelper, categoryName);
|
||||
|
||||
public void Dispose()
|
||||
{ }
|
||||
}
|
38
MesETL.Test/XUnit/TestBase.cs
Normal file
38
MesETL.Test/XUnit/TestBase.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using TestProject1.XUnit.Logging;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject1.XUnit;
|
||||
|
||||
public class TestBase
|
||||
{
|
||||
private readonly LoggerFactory _loggerFactory;
|
||||
protected readonly ITestOutputHelper Output;
|
||||
|
||||
private readonly JsonSerializerOptions _jsonSerializerOptions =
|
||||
new(JsonSerializerDefaults.Web) { WriteIndented = true };
|
||||
|
||||
public TestBase(ITestOutputHelper output)
|
||||
{
|
||||
Output = output;
|
||||
Console.SetOut(new XUnitConsoleWriter(output));
|
||||
_loggerFactory = new LoggerFactory([new XunitLoggerProvider(Output)]);
|
||||
}
|
||||
|
||||
protected void Write(object obj)
|
||||
{
|
||||
Output.WriteLine(obj.ToString());
|
||||
}
|
||||
|
||||
protected void WriteJson<T>(T obj)
|
||||
{
|
||||
Console.WriteLine(JsonSerializer.Serialize(obj, _jsonSerializerOptions));
|
||||
}
|
||||
|
||||
protected ILogger<T> CreateXUnitLogger<T>()
|
||||
{
|
||||
return _loggerFactory.CreateLogger<T>();
|
||||
}
|
||||
}
|
18
MesETL.Test/XUnit/XUnitConsoleWriter.cs
Normal file
18
MesETL.Test/XUnit/XUnitConsoleWriter.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace TestProject1.XUnit;
|
||||
|
||||
public class XUnitConsoleWriter : StringWriter
|
||||
{
|
||||
private ITestOutputHelper output;
|
||||
|
||||
public XUnitConsoleWriter(ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
public override void WriteLine(string? m)
|
||||
{
|
||||
output.WriteLine(m);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user