thomhurst / TUnit

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

ClassDataSource with multiple parameters doesn't call AsyncInitialize #1165

Closed theodelrieu closed 6 days ago

theodelrieu commented 6 days ago

Hello,

I've been banging my hand on a wall with this one, and I finally found a workaround.

I have two fixtures, one for creating/seeding a DB, the other to have different contexts. Following the documentation, I tried using ClassDataSource like this:

using TUnit.Core;
using TUnit.Core.Interfaces;

namespace DebugTests;

public class Fixture1 : IAsyncInitializer
{
    public async Task InitializeAsync()
    {
        Console.WriteLine("Fixture1 InitializeAsync");
        await Task.Delay(2);
    }
}

public class Fixture2 : IAsyncInitializer
{
    public async Task InitializeAsync()
    {
        Console.WriteLine("Fixture2 InitializeAsync");
        await Task.Delay(2);
    }
}

[ClassDataSource<Fixture1, Fixture2>(Shared = [SharedType.Globally, SharedType.Globally])]
public class Tests(Fixture1 fix1, Fixture2 fix2)
{
    [Test]
    public async Task Test1()
    {
        Console.WriteLine("Tests.Test1");
        await Task.Delay(2);
    }
}

This results in neither of the InitializeAsync logs showing up. Using a single parameter works as intended though.

I thus used this workaround:

public class Tests
{
    [ClassDataSource<Fixture1>(Shared = SharedType.Globally)]
    public required Fixture1 fix1 {get; init;}

    [ClassDataSource<Fixture2>(Shared = SharedType.Globally)]
    public required Fixture2 fix2 {get; init;}

    // [...]
}

I don't see any reason why the former shouldn't work. Have I misunderstood something?

PS: there are some discrepancies in the doc, e.g. BeforeEvery is not documented, nor is the fact that methods using those attributes must all be static.

thomhurst commented 6 days ago

Nope you haven't misunderstood anything, this is a bug, thanks for finding. I'll get a fix out for you later 😄

thomhurst commented 6 days ago

Should be fixed in 0.2.195