basdijkstra / rest-assured-net

C# DSL for writing readable tests for HTTP-based APIs
Apache License 2.0
145 stars 19 forks source link

logging not working with xunit #83

Closed codybower3 closed 1 year ago

codybower3 commented 1 year ago

Hi Bas. I haven't had a chance to look into this too much but it looks like logging isn’t working as it should. I suspect this might be unique to xunit. I'm using the latest xunit packages and RestAssured.Net v 2.8.1. RestAssuredDotNetLogIssue

basdijkstra commented 1 year ago

Thank you for reporting, @codybower3. I’ll have a look at this soon. I’ve not done a lot of testing with xUnit as I didn’t suspect it mattered that much, but I might just be wrong here.

Will report back later this week.

basdijkstra commented 1 year ago

Just ran a quick test and I can reproduce this on my own machine.

basdijkstra commented 1 year ago

Hi @codybower3, it seems like xUnit does not print console output by default (Log() simply calls Console.WriteLine()).

To solve this within RestAssured.Net, it looks like I need to create some custom logging functionality specific for xUnit, but that requires adding xUnit as a dependency to the project, and I don't want to do that.

I'll see if I can create a working example that does not require me to do this, and then add that to the documentation for example.

codybower3 commented 1 year ago

@basdijkstra That makes sense. Thank you! :)

basdijkstra commented 1 year ago

Hey @codybower3, here's one way to resolve this. I was going to say that you could put this in a before / after hook, but xUnit also doesn't have those. I suspect (but haven't tested) that you could put the inner class etc. in a base class for your tests to avoid having to do this for every test class you've got.

What this does is basically redirecting the calls to Console.WriteLine() that RestAssured.Net executes to the ITestOutputHelper from xUnit. A bit wordy, but it works.

public class IssueTest
{
    private readonly ITestOutputHelper output;

    public IssueTest(ITestOutputHelper output)
    {
        this.output = output;
    }

    public class ConsoleWriter : StringWriter
    {
        private ITestOutputHelper output;

        public ConsoleWriter(ITestOutputHelper output)
        {
            this.output = output;
        }

        public override void WriteLine(string? m)
        {
            output.WriteLine(m);
        }
    }

    [Fact]
    public void TestLogging()
    {
        Console.SetOut(new ConsoleWriter(output));

        Given()
            .Log(RequestLogLevel.All)
        .When()
            .Get("https://jsonplaceholder.typicode.com/users/1")
        .Then()
            .Log(ResponseLogLevel.All)
        .And()
            .StatusCode(200)
            .Body("$.name", NHamcrest.Is.EqualTo("Leanne Graham"));
    }
}

Hope that helps! If so I'll add this to the docs, too.

codybower3 commented 1 year ago

Thanks @basdijkstra. This is helpful! Really enjoying RestAssured.Net :)

basdijkstra commented 1 year ago

Happy to hear that. I’ll add this example to the docs and close this issue after that has been done. Feel free to reopen it or open another one if there’s something else you feel should be done.

Happy to hear you’re enjoying the library!

basdijkstra commented 1 year ago

Added to the docs: https://github.com/basdijkstra/rest-assured-net/wiki/Usage-Guide#logging-when-using-xunit