From ad044372c3dfdd9593b01770e5e297ec1abb8d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Thu, 19 Dec 2024 10:53:49 +0100 Subject: [PATCH] Experiment with using TUnit instead of xUnit Remaining issues ## Coverage * `dotnet run --coverage` doesn't run the VSTest target so ReportGenerator can't be hooked * `dotnet test --coverage` fails with MSB1001: Unknown switch ## Stryker * Currently not supported for Microsoft.Testing.Platform, see https://github.com/stryker-mutator/stryker-net/issues/3094 --- tests/IndentationSettingsTest.cs | 15 +- tests/LineEndingTest.cs | 3 +- tests/Log4NetTextFormatterTest.cs | 158 +++++++++--------- tests/PublicApi.cs | 25 ++- tests/Serilog.Formatting.Log4Net.Tests.csproj | 10 +- 5 files changed, 104 insertions(+), 107 deletions(-) diff --git a/tests/IndentationSettingsTest.cs b/tests/IndentationSettingsTest.cs index 7923c34..cfcd548 100644 --- a/tests/IndentationSettingsTest.cs +++ b/tests/IndentationSettingsTest.cs @@ -1,16 +1,15 @@ using System; using FluentAssertions; -using Xunit; namespace Serilog.Formatting.Log4Net.Tests; public class IndentationSettingsTest { - [Theory] - [InlineData(Indentation.Space, 2, " ")] - [InlineData(Indentation.Tab, 2, "\t\t")] - [InlineData(Indentation.Space, 4, " ")] - [InlineData(Indentation.Tab, 4, "\t\t\t\t")] + [Test] + [Arguments(Indentation.Space, (byte)2, " ")] + [Arguments(Indentation.Tab, (byte)2, "\t\t")] + [Arguments(Indentation.Space, (byte)4, " ")] + [Arguments(Indentation.Tab, (byte)4, "\t\t\t\t")] public void IndentationSettingsToString(Indentation indentation, byte size, string expectedString) { // Arrange @@ -23,7 +22,7 @@ public void IndentationSettingsToString(Indentation indentation, byte size, stri indentationString.Should().Be(expectedString); } - [Fact] + [Test] public void InvalidIndentation() { // Act @@ -34,7 +33,7 @@ public void InvalidIndentation() .Which.Message.Should().StartWith("The value of argument 'indentation' (-1) is invalid for enum type 'Indentation'."); } - [Fact] + [Test] public void InvalidSize() { // Act diff --git a/tests/LineEndingTest.cs b/tests/LineEndingTest.cs index 992d563..6b0e712 100644 --- a/tests/LineEndingTest.cs +++ b/tests/LineEndingTest.cs @@ -1,12 +1,11 @@ using System; using FluentAssertions; -using Xunit; namespace Serilog.Formatting.Log4Net.Tests; public class LineEndingTest { - [Fact] + [Test] public void InvalidLineEnding() { Action action = () => _ = new Log4NetTextFormatter(c => c.UseLineEnding((LineEnding)4)); diff --git a/tests/Log4NetTextFormatterTest.cs b/tests/Log4NetTextFormatterTest.cs index 0700242..88c653a 100644 --- a/tests/Log4NetTextFormatterTest.cs +++ b/tests/Log4NetTextFormatterTest.cs @@ -9,15 +9,14 @@ using Serilog.Events; using Serilog.Parsing; using VerifyTests; -using VerifyXunit; -using Xunit; +using VerifyTUnit; namespace Serilog.Formatting.Log4Net.Tests; -public sealed class Log4NetTextFormatterTest : IDisposable +public sealed class Log4NetTextFormatterTest { - private readonly TextWriter _selfLogWriter; - private string? SelfLogValue => _selfLogWriter.ToString(); + private TextWriter? _selfLogWriter; + private string? SelfLogValue => _selfLogWriter?.ToString(); /// /// Create a containing two entries, mapping scalar values 1 to "one" and "two" to 2. @@ -41,19 +40,22 @@ private static LogEvent CreateLogEvent(LogEventLevel level = Events.LogEventLeve ); } - public Log4NetTextFormatterTest() + [Before(Test)] + public void EnableSelfLog() { _selfLogWriter = new StringWriter(); Debugging.SelfLog.Enable(_selfLogWriter); } - public void Dispose() + [After(Test)] + public void DisableSelfLog() { Debugging.SelfLog.Disable(); - _selfLogWriter.Dispose(); + _selfLogWriter?.Dispose(); + _selfLogWriter = null; } - [Fact] + [Test] public void NullLogEventThrowsArgumentNullException() { // Arrange @@ -67,7 +69,7 @@ public void NullLogEventThrowsArgumentNullException() .Which.StackTrace!.TrimStart().Should().StartWith("at Serilog.Formatting.Log4Net.Log4NetTextFormatter.Format"); } - [Fact] + [Test] public void NullOutputThrowsArgumentNullException() { // Arrange @@ -82,7 +84,7 @@ public void NullOutputThrowsArgumentNullException() .Which.StackTrace!.TrimStart().Should().StartWith("at Serilog.Formatting.Log4Net.Log4NetTextFormatter.Format"); } - [Fact] + [Test] public void SettingPropertyFilterToNullThrowsArgumentNullException() { // Act @@ -94,7 +96,7 @@ public void SettingPropertyFilterToNullThrowsArgumentNullException() .And.ParamName.Should().Be("filterProperty"); } - [Fact] + [Test] public void SettingExceptionFormatterToNullThrowsArgumentNullException() { // Act @@ -106,13 +108,13 @@ public void SettingExceptionFormatterToNullThrowsArgumentNullException() .And.ParamName.Should().Be("formatException"); } - [Theory] - [InlineData(Events.LogEventLevel.Verbose)] - [InlineData(Events.LogEventLevel.Debug)] - [InlineData(Events.LogEventLevel.Information)] - [InlineData(Events.LogEventLevel.Warning)] - [InlineData(Events.LogEventLevel.Error)] - [InlineData(Events.LogEventLevel.Fatal)] + [Test] + [Arguments(Events.LogEventLevel.Verbose)] + [Arguments(Events.LogEventLevel.Debug)] + [Arguments(Events.LogEventLevel.Information)] + [Arguments(Events.LogEventLevel.Warning)] + [Arguments(Events.LogEventLevel.Error)] + [Arguments(Events.LogEventLevel.Fatal)] public Task LogEventLevel(LogEventLevel level) { // Arrange @@ -127,7 +129,7 @@ public Task LogEventLevel(LogEventLevel level) return Verify(output).UseParameters(level); } - [Fact] + [Test] public void InvalidLogEventLevelThrowsArgumentOutOfRangeException() { // Arrange @@ -142,13 +144,13 @@ public void InvalidLogEventLevelThrowsArgumentOutOfRangeException() .And.Message.Should().StartWith("The value of argument 'level' (-1) is invalid for enum type 'LogEventLevel'."); } - [Theory] - [InlineData(CDataMode.Always, true)] - [InlineData(CDataMode.Always, false)] - [InlineData(CDataMode.Never, true)] - [InlineData(CDataMode.Never, false)] - [InlineData(CDataMode.IfNeeded, true)] - [InlineData(CDataMode.IfNeeded, false)] + [Test] + [Arguments(CDataMode.Always, true)] + [Arguments(CDataMode.Always, false)] + [Arguments(CDataMode.Never, true)] + [Arguments(CDataMode.Never, false)] + [Arguments(CDataMode.IfNeeded, true)] + [Arguments(CDataMode.IfNeeded, false)] public Task MessageCDataMode(CDataMode mode, bool needsEscaping) { // Arrange @@ -163,11 +165,11 @@ public Task MessageCDataMode(CDataMode mode, bool needsEscaping) return Verify(output).UseParameters(mode, needsEscaping); } - [Theory] - [InlineData(LineEnding.None)] - [InlineData(LineEnding.LineFeed)] - [InlineData(LineEnding.CarriageReturn)] - [InlineData(LineEnding.CarriageReturn | LineEnding.LineFeed)] + [Test] + [Arguments(LineEnding.None)] + [Arguments(LineEnding.LineFeed)] + [Arguments(LineEnding.CarriageReturn)] + [Arguments(LineEnding.CarriageReturn | LineEnding.LineFeed)] public Task XmlElementsLineEnding(LineEnding lineEnding) { // Arrange @@ -182,11 +184,11 @@ public Task XmlElementsLineEnding(LineEnding lineEnding) return Verify(output).UseParameters(lineEnding); } - [Theory] - [InlineData(Indentation.Space, 2)] - [InlineData(Indentation.Space, 4)] - [InlineData(Indentation.Tab, 2)] - [InlineData(Indentation.Tab, 4)] + [Test] + [Arguments(Indentation.Space, (byte)2)] + [Arguments(Indentation.Space, (byte)4)] + [Arguments(Indentation.Tab, (byte)2)] + [Arguments(Indentation.Tab, (byte)4)] public Task IndentationSettings(Indentation indentation, byte size) { // Arrange @@ -201,7 +203,7 @@ public Task IndentationSettings(Indentation indentation, byte size) return Verify(output).UseParameters(indentation, size); } - [Fact] + [Test] public Task NoIndentation() { // Arrange @@ -216,7 +218,7 @@ public Task NoIndentation() return Verify(output); } - [Fact] + [Test] public Task NoNamespace() { // Arrange @@ -231,7 +233,7 @@ public Task NoNamespace() return Verify(output); } - [Fact] + [Test] public Task NullProperty() { // Arrange @@ -246,7 +248,7 @@ public Task NullProperty() return Verify(output); } - [Fact] + [Test] public Task LoggerName() { // Arrange @@ -261,7 +263,7 @@ public Task LoggerName() return Verify(output); } - [Fact] + [Test] public Task LoggerNameStructureValue() { // Arrange @@ -276,7 +278,7 @@ public Task LoggerNameStructureValue() return Verify(output); } - [Fact] + [Test] public Task DefaultFormatProvider() { // Arrange @@ -291,9 +293,9 @@ public Task DefaultFormatProvider() return Verify(output); } - [Theory] - [InlineData(true)] - [InlineData(false)] + [Test] + [Arguments(true)] + [Arguments(false)] public Task Log4JCompatibility(bool useStaticInstance) { // Arrange @@ -311,7 +313,7 @@ public Task Log4JCompatibility(bool useStaticInstance) return Verify(output).IgnoreParameters().DisableRequireUniquePrefix(); } - [Fact] + [Test] public Task ExplicitFormatProvider() { // Arrange @@ -327,7 +329,7 @@ public Task ExplicitFormatProvider() return Verify(output); } - [Fact] + [Test] public Task TwoProperties() { // Arrange @@ -345,7 +347,7 @@ public Task TwoProperties() return Verify(output); } - [Fact] + [Test] public Task TwoPropertiesOneNull() { // Arrange @@ -363,7 +365,7 @@ public Task TwoPropertiesOneNull() return Verify(output); } - [Fact] + [Test] public Task FilterProperty() { // Arrange @@ -381,7 +383,7 @@ public Task FilterProperty() return Verify(output); } - [Fact] + [Test, NotInParallel] public Task FilterPropertyThrowing() { // Arrange @@ -407,7 +409,7 @@ public Task FilterPropertyThrowing() return Verify(output); } - [Fact] + [Test] public Task TwoEvents() { // Arrange @@ -423,7 +425,7 @@ public Task TwoEvents() return Verify(output); } - [Fact] + [Test] public Task Exception() { // Arrange @@ -438,7 +440,7 @@ public Task Exception() return Verify(output); } - [Fact] + [Test] public Task ExceptionFormatter() { // Arrange @@ -453,7 +455,7 @@ public Task ExceptionFormatter() return Verify(output); } - [Fact] + [Test] public Task ExceptionFormatterReturningNull() { // Arrange @@ -468,7 +470,7 @@ public Task ExceptionFormatterReturningNull() return Verify(output); } - [Fact] + [Test, NotInParallel] public Task ExceptionFormatterThrowing() { // Arrange @@ -484,9 +486,9 @@ public Task ExceptionFormatterThrowing() return Verify(output); } - [Theory] - [InlineData(null)] - [InlineData(1)] + [Test] + [Arguments(null)] + [Arguments(1)] public Task ThreadIdProperty(int? threadId) { // Arrange @@ -501,12 +503,12 @@ public Task ThreadIdProperty(int? threadId) return Verify(output).UseParameters(threadId); } - [Theory] - [InlineData(null)] - [InlineData("")] - [InlineData("TheUser")] - [InlineData(@"TheDomain\TheUser")] - [InlineData(@"TheDomain\TheUser\Name")] + [Test] + [Arguments(null)] + [Arguments("")] + [Arguments("TheUser")] + [Arguments(@"TheDomain\TheUser")] + [Arguments(@"TheDomain\TheUser\Name")] public Task DomainAndUserNameProperty(string? environmentUserName) { // Arrange @@ -521,7 +523,7 @@ public Task DomainAndUserNameProperty(string? environmentUserName) return Verify(output).UseParameters(environmentUserName); } - [Fact] + [Test] public Task DomainAndUserNamePropertyStructureValue() { // Arrange @@ -536,9 +538,9 @@ public Task DomainAndUserNamePropertyStructureValue() return Verify(output); } - [Theory] - [InlineData(null)] - [InlineData("TheMachineName")] + [Test] + [Arguments(null)] + [Arguments("TheMachineName")] public Task MachineNameProperty(string? machineName) { // Arrange @@ -553,7 +555,7 @@ public Task MachineNameProperty(string? machineName) return Verify(output).UseParameters(machineName); } - [Fact] + [Test] public Task MachineNamePropertyStructureValue() { // Arrange @@ -568,7 +570,7 @@ public Task MachineNamePropertyStructureValue() return Verify(output); } - [Fact] + [Test] public Task Caller() { // Arrange @@ -584,7 +586,7 @@ public Task Caller() return Verify(output); } - [Fact] + [Test] public Task CallerNonScalar() { // Arrange @@ -600,7 +602,7 @@ public Task CallerNonScalar() return Verify(output); } - [Fact] + [Test] public Task CallerWithFile() { // Arrange @@ -616,7 +618,7 @@ public Task CallerWithFile() return Verify(output); } - [Fact] + [Test] public Task CallerLog4J() { // Arrange @@ -632,7 +634,7 @@ public Task CallerLog4J() return Verify(output); } - [Fact] + [Test] public Task SequenceProperty() { // Arrange @@ -648,7 +650,7 @@ public Task SequenceProperty() return Verify(output); } - [Fact] + [Test] public Task DictionaryProperty() { // Arrange @@ -669,7 +671,7 @@ public Task DictionaryProperty() return Verify(output); } - [Fact] + [Test] public Task StructureProperty() { // Arrange @@ -690,7 +692,7 @@ public Task StructureProperty() return Verify(output); } - [Fact] + [Test] public Task CustomLogEventPropertyValue() { // Arrange diff --git a/tests/PublicApi.cs b/tests/PublicApi.cs index 9609093..cc9e9e4 100644 --- a/tests/PublicApi.cs +++ b/tests/PublicApi.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; @@ -5,37 +6,33 @@ using System.Xml.Linq; using System.Xml.XPath; using PublicApiGenerator; -using VerifyXunit; -using Xunit; +using VerifyTUnit; namespace Serilog.Formatting.Log4Net.Tests; public class PublicApi { - [Theory] - [ClassData(typeof(TargetFrameworksTheoryData))] + [Test] + [MethodDataSource(nameof(GetTargetFrameworks))] public Task ApprovePublicApi(string targetFramework) { var testAssembly = typeof(PublicApi).Assembly; var configuration = testAssembly.GetCustomAttribute()?.Configuration ?? throw new InvalidDataException($"{nameof(AssemblyConfigurationAttribute)} not found in {testAssembly.Location}"); var assemblyPath = Path.Combine(GetSrcDirectoryPath(), "bin", configuration, targetFramework, "Serilog.Formatting.Log4Net.dll"); - var assembly = Assembly.LoadFile(assemblyPath); + var assembly = System.Reflection.Assembly.LoadFile(assemblyPath); var publicApi = assembly.GeneratePublicApi(); return Verifier.Verify(publicApi, "cs").UseFileName($"PublicApi.{targetFramework}"); } private static string GetSrcDirectoryPath([CallerFilePath] string path = "") => Path.Combine(Path.GetDirectoryName(path)!, "..", "src"); - private class TargetFrameworksTheoryData : TheoryData + public static IEnumerable GetTargetFrameworks() { - public TargetFrameworksTheoryData() - { - var csprojPath = Path.Combine(GetSrcDirectoryPath(), "Serilog.Formatting.Log4Net.csproj"); - var project = XDocument.Load(csprojPath); - var targetFrameworks = project.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks") - ?? throw new InvalidDataException($"TargetFrameworks element not found in {csprojPath}"); - AddRange(targetFrameworks.Value.Split(';')); - } + var csprojPath = Path.Combine(GetSrcDirectoryPath(), "Serilog.Formatting.Log4Net.csproj"); + var project = XDocument.Load(csprojPath); + var targetFrameworks = project.XPathSelectElement("/Project/PropertyGroup/TargetFrameworks") + ?? throw new InvalidDataException($"TargetFrameworks element not found in {csprojPath}"); + return targetFrameworks.Value.Split(';'); } } \ No newline at end of file diff --git a/tests/Serilog.Formatting.Log4Net.Tests.csproj b/tests/Serilog.Formatting.Log4Net.Tests.csproj index 0339e7d..e94e7c5 100644 --- a/tests/Serilog.Formatting.Log4Net.Tests.csproj +++ b/tests/Serilog.Formatting.Log4Net.Tests.csproj @@ -1,6 +1,7 @@ + exe net8.0 enable @@ -9,15 +10,14 @@ - + - - - + + @@ -39,7 +39,7 @@ - +