thomhurst / TUnit

A modern, fast and flexible .NET testing framework
MIT License
2.4k stars 38 forks source link

[question] How to properly log? #1249

Closed theodelrieu closed 3 days ago

theodelrieu commented 3 days ago

Hello,

I had to remove every instance of ITestOutputHelper when migrating from xUnit to TUnit, but I couldn't find anything in the doc about logging.

Looking at the code, there are some logging-related classes in TUnit.Engine.Logging. Are they supposed to be used by client code? Is Console.WriteLine the way to go?

thomhurst commented 3 days ago

Yep console writelines are automatically intercepted by the framework

theodelrieu commented 3 days ago

Thanks. It's still not clear to me what the difference between xUnit and TUnit is on that aspect. Are there missing/added features?

EDIT: We currently have a lot of Asp services that take an ILogger, so I'd like to get an ILogger in my tests like I'm able to do with xUnit, c.f. this article.

thomhurst commented 3 days ago

If you wanted an ILogger you'd have to create your own implementation, because TUnit doesn't have any dependencies on Microsoft.Extensions.Logging.

You can get a TUnitLogger by doing TestContext.Current!.GetDefaultLogger() - so your custom ILogger could forward calls on to the TUnit logger

theodelrieu commented 3 days ago

Thanks!