Closed codybower3 closed 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.
Just ran a quick test and I can reproduce this on my own machine.
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.
@basdijkstra That makes sense. Thank you! :)
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.
Thanks @basdijkstra. This is helpful! Really enjoying RestAssured.Net :)
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!
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.