Skip to content
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

Add support for logging to an xUnit ITestOutputHelper #758

Closed
siewers opened this issue May 20, 2022 · 3 comments
Closed

Add support for logging to an xUnit ITestOutputHelper #758

siewers opened this issue May 20, 2022 · 3 comments
Labels

Comments

@siewers
Copy link
Contributor

siewers commented May 20, 2022

Is your feature request related to a problem? Please describe.
When using xUnit, it would be nice to be able to log the output from WireMock to the test output helper.

Describe the solution you'd like
Introduce a new WireMock.Net.XUnit project and create a TestOutputHelperWireMockLogger class, for logging to an ITestOutputHelper.

Describe alternatives you've considered
Keeping the implementation in my own code base.

Is your feature request supported by WireMock (java version)? Please provide details.
Not to my knowledge, no. XUnit is a .NET testing framework, so there is no direct Java equivalent.

Additional context
This is the implementation I'm currently using:

private sealed class TestOutputHelperWireMockLogger : IWireMockLogger
{
    private readonly ITestOutputHelper _testOutputHelper;

    public TestOutputHelperWireMockLogger(ITestOutputHelper testOutputHelper)
    {
        _testOutputHelper = 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 formatString, Exception exception)
    {
        _testOutputHelper.WriteLine(Format("Error", formatString, exception.Message));

        if (exception is AggregateException ae)
        {
            ae.Handle(ex =>
            {
                _testOutputHelper.WriteLine(Format("Error", "Exception {0}", ex.Message));
                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}";
    }
}
@StefH
Copy link
Collaborator

StefH commented May 20, 2022

#759

@StefH
Copy link
Collaborator

StefH commented May 20, 2022

@siewers
How to use this in the xunit test ?
Or is just referencing the new project enough?

@siewers
Copy link
Contributor Author

siewers commented May 20, 2022

It should be sufficient to reference the project.
Normally you already have a reference to xUnit, but since it can't write to the console, you need to use the ITestOutputHelper.

@StefH StefH closed this as completed May 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants