thomhurst / TUnit

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

Questions about TUnitLogger #590

Closed StefH closed 1 hour ago

StefH commented 2 hours ago

1 TUnitLogger Context?

I want to redirect my own logger to the TUnitLogger instance.

This is the class:

public sealed class TUnitWireMockLogger : IWireMockLogger
{
    private readonly TUnitLogger _tUnitLogger;

    /// <summary>
    /// Create a new instance on the <see cref="TUnitWireMockLogger"/>.
    /// </summary>
    /// <param name="tUnitLogger">Represents a class which can be used to provide test output.</param>
    public TUnitWireMockLogger(TUnitLogger tUnitLogger)
    {
        _tUnitLogger = Guard.NotNull(tUnitLogger);
    }

    // . . .

Which is used like:

public class TUnitTests
{
    private IWireMockLogger _logger = null!;

    [Before(Test)]
    public void BeforeTest(TestContext context)
    {
        _logger = new TUnitWireMockLogger(context.GetDefaultLogger());
    }

    [Test]
    public async Task Test_TUnitWireMockLogger()
    {
        // . . .
    } 
}

Question: Is this there a way to inject the TestContext in the [Test] instead of [Before(Test)]?

2 conflicts in SelfRegisteredExtensions?

I've created a new project WireMock.Net.TUnit.csproj which includes the TUnit NuGet. And I've a test-project test\WireMock.Net.TUnitTests.csproj which also includes the TUnit NuGet.

When building I get the warning:

D:\a\WireMock.Net\WireMock.Net\test\WireMock.Net.TUnitTests\obj\Release\net8.0\TestPlatformEntryPoint.cs(13,9): warning CS0436: The type 'SelfRegisteredExtensions' in 'D:\a\WireMock.Net\WireMock.Net\test\WireMock.Net.TUnitTests\obj\Release\net8.0\AutoRegisteredExtensions.cs' conflicts with the imported type 'SelfRegisteredExtensions' in 'WireMock.Net.TUnit, Version=1.6.3.0, Culture=neutral, PublicKeyToken=c8d65537854e1f03'. Using the type defined in 'D:\a\WireMock.Net\WireMock.Net\test\WireMock.Net.TUnitTests\obj\Release\net8.0\AutoRegisteredExtensions.cs'. [D:\a\WireMock.Net\WireMock.Net\test\WireMock.Net.TUnitTests\WireMock.Net.TUnitTests.csproj]

Is this defined behavior?

Note that xUnit has a NuGet xUnit.abstractions which includes the ITestOutputHelper which is similar to TUnitLogger.

thomhurst commented 2 hours ago

Is this there a way to inject the TestContext in the [Test] instead of [Before(Test)]?

You can grab it statically: TestContext.Current

2 conflicts in SelfRegisteredExtensions?

Yeah this happens if you use the TUnit package in a library project, and reference that in your test project. The plan is for library projects to reference TUnit.Core only - Similar to xUnit.abstractions - But it's not ready yet. I'm going to try to get that working in the next week or so hopefully.

thomhurst commented 1 hour ago

Does that solve everything for you for now @StefH ?

StefH commented 1 hour ago

Yes. Thanks for the explanation. I'll close this now.

Maybe tag me here once the refactoring related to TUnit.Core is done.