Skip to content

Bug: Too many calls to InitializeAsync #3961

@TheRumle

Description

@TheRumle

Info

  • IDE: JetBrains Rider 2025.2.4
  • TUnit version: 1.3.15

Recreation of the problem

I have a single test class that implements IAsyncInitializer resource. The initializer is called two times per usage class regardless of whether only one test class is run.

public sealed class BugResource : IAsyncInitializer, IAsyncDisposable
{
    public static int TearDowns = 0;
    public static int SetUps = 0;
    public  ValueTask DisposeAsync()
    {
        Interlocked.Increment(ref TearDowns);
        return ValueTask.CompletedTask;
    }

    public Task InitializeAsync()
    {
        Interlocked.Increment(ref SetUps);
        return Task.CompletedTask;
    }
}

public sealed class BugTestTwo
{
    [ClassDataSource<BugResource>]
    public required BugResource BugResource { get; init; }

    [Test]
    public async Task TestTwo)
    {
        await Assert.That(BugResource.SetUps).EqualTo(1);
    }
} 

public sealed class BugTestThree
{
    [ClassDataSource<BugResource>]
    public required BugResource BugResource { get; init; }

    [Test]
    public async Task TestThree()
    {
        await Assert.That(BugResource.SetUps).EqualTo(1);
    }
} 

public sealed class BugTestOne
{
    [ClassDataSource<BugResource>]
    public required BugResource BugResource { get; init; }

    [Test]
    public async Task TestOne()
    {
        await Assert.That(BugResource.SetUps).EqualTo(1);
    }
} 

That is, running any of BugTestN results in 2 calls to InitializeAsync. However, if I run a session with all three, then InitializeAsync is called 6 times.

What would solve my problem

The InitializeAsync should only be called once per test class, so in a session with all three test classes it should be called 3 times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions