From 37bad618a35c489c9c2d48110053f51fc54ec881 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 18 Nov 2025 18:42:28 +0100 Subject: [PATCH] Add WireMock.Net.xUnit.v3 project (#1380) * Add WireMock.Net.xUnit.v3 project * . --- WireMock.Net Solution.sln | 11 ++- .../TestOutputHelperWireMockLogger.cs | 79 +++++++++++++++++++ .../WireMock.Net.xUnit.v3.csproj | 37 +++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/WireMock.Net.xUnit.v3/TestOutputHelperWireMockLogger.cs create mode 100644 src/WireMock.Net.xUnit.v3/WireMock.Net.xUnit.v3.csproj diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 89ca8789..edd11b30 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31521.260 +# Visual Studio Version 18 +VisualStudioVersion = 18.0.11205.157 d18.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8F890C6F-9ACC-438D-928A-AD61CDA862F2}" EndProject @@ -144,6 +144,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Extensions.Rou EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.ProtoBuf", "src\WireMock.Net.ProtoBuf\WireMock.Net.ProtoBuf.csproj", "{B47413AA-55D3-49A7-896A-17ADBFF72407}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.xUnit.v3", "src\WireMock.Net.xUnit.v3\WireMock.Net.xUnit.v3.csproj", "{4F46BD02-BEBC-4B2D-B857-4169AD1FB067}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -350,6 +352,10 @@ Global {B47413AA-55D3-49A7-896A-17ADBFF72407}.Debug|Any CPU.Build.0 = Debug|Any CPU {B47413AA-55D3-49A7-896A-17ADBFF72407}.Release|Any CPU.ActiveCfg = Release|Any CPU {B47413AA-55D3-49A7-896A-17ADBFF72407}.Release|Any CPU.Build.0 = Release|Any CPU + {4F46BD02-BEBC-4B2D-B857-4169AD1FB067}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F46BD02-BEBC-4B2D-B857-4169AD1FB067}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F46BD02-BEBC-4B2D-B857-4169AD1FB067}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F46BD02-BEBC-4B2D-B857-4169AD1FB067}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -407,6 +413,7 @@ Global {3FCBCA9C-9DB0-4A96-B47E-30470764CC9C} = {0BB8B634-407A-4610-A91F-11586990767A} {1E874C8F-08A2-493B-8421-619F9A6E9E77} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} {B47413AA-55D3-49A7-896A-17ADBFF72407} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} + {4F46BD02-BEBC-4B2D-B857-4169AD1FB067} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458} diff --git a/src/WireMock.Net.xUnit.v3/TestOutputHelperWireMockLogger.cs b/src/WireMock.Net.xUnit.v3/TestOutputHelperWireMockLogger.cs new file mode 100644 index 00000000..88c4993b --- /dev/null +++ b/src/WireMock.Net.xUnit.v3/TestOutputHelperWireMockLogger.cs @@ -0,0 +1,79 @@ +// Copyright © WireMock.Net + +using System; +using Newtonsoft.Json; +using Stef.Validation; +using WireMock.Admin.Requests; +using WireMock.Logging; +using Xunit; + +namespace WireMock.Net.Xunit; + +/// +/// When using xUnit, this class enables to log the output from WireMock.Net to the . +/// +public sealed class TestOutputHelperWireMockLogger : IWireMockLogger +{ + private readonly ITestOutputHelper _testOutputHelper; + + /// + /// Create a new instance on the . + /// + /// Represents a class which can be used to provide test output. + public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = Guard.NotNull(testOutputHelper); + } + + /// + public void Debug(string formatString, params object[] args) + { + _testOutputHelper.WriteLine(Format("Debug", formatString, args)); + } + + /// + public void Info(string formatString, params object[] args) + { + _testOutputHelper.WriteLine(Format("Info", formatString, args)); + } + + /// + public void Warn(string formatString, params object[] args) + { + _testOutputHelper.WriteLine(Format("Warning", formatString, args)); + } + + /// + public void Error(string formatString, params object[] args) + { + _testOutputHelper.WriteLine(Format("Error", formatString, args)); + } + + /// + public void Error(string message, Exception exception) + { + _testOutputHelper.WriteLine(Format("Error", $"{message} {{0}}", exception)); + + if (exception is AggregateException ae) + { + ae.Handle(ex => + { + _testOutputHelper.WriteLine(Format("Error", "Exception {0}", ex)); + return true; + }); + } + } + + /// + public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) + { + var message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); + _testOutputHelper.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message)); + } + + private static string Format(string level, string formatString, params object[] args) + { + var message = args.Length > 0 ? string.Format(formatString, args) : formatString; + return $"{DateTime.UtcNow} [{level}] : {message}"; + } +} \ No newline at end of file diff --git a/src/WireMock.Net.xUnit.v3/WireMock.Net.xUnit.v3.csproj b/src/WireMock.Net.xUnit.v3/WireMock.Net.xUnit.v3.csproj new file mode 100644 index 00000000..36db8e7a --- /dev/null +++ b/src/WireMock.Net.xUnit.v3/WireMock.Net.xUnit.v3.csproj @@ -0,0 +1,37 @@ + + + Some extensions for xUnit (ITestOutputHelper) + WireMock.Net.xUnit.v3 + Stef Heyenrath + net472;net8.0 + true + tdd;wiremock;test;unittest;xunit + {4F46BD02-BEBC-4B2D-B857-4169AD222267} + true + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + true + true + true + true + ../WireMock.Net/WireMock.Net.snk + true + + + + true + + + + + + + + + + + + + + + + \ No newline at end of file