Skip to content

[Bug]: The method marked with [Before (TestSession)] never runs #4541

@AfricanCh

Description

@AfricanCh

Description

After upgrading TUnit from 0.19.2 to the latest version, this issue occurred where the method marked as [Before (TestSession)] never ran. Before all testing, I used this method to start a postgres container for the testing environment, but it never executed, causing the test to fail. After the breakpoint display test starts, the method will be skipped directly and breakpoints located within the method will not be triggered

Expected Behavior

The method marked as [Before (TestSession)] runs before the start of the test session

Actual Behavior

Marked as [Before (TestSession)], it has never been run from the beginning to the end of the test

Steps to Reproduce

Here is a simple test example I used:

public sealed class AlbaBootstrap : IntegrationTestBase, IAsyncInitializer, IAsyncDisposable
{
    public IAlbaHost Host { get; private set; } = null!;

    public async Task InitializeAsync() 
    {
        Host = await AlbaHost.For<WebApp.Program>();
    }

    public async ValueTask DisposeAsync()
    {
        await Host.DisposeAsync();
    }
}
public abstract class AlbaTestBase(AlbaBootstrap albaBootstrap)
{
    protected IAlbaHost Host => albaBootstrap.Host;
}

[ClassDataSource<AlbaBootstrap>(Shared = SharedType.PerTestSession)]
public class MyTestClass(AlbaBootstrap albaBootstrap) : AlbaTestBase(albaBootstrap)
{
    [Test]
    public async Task happy_path()
    {
        await Host.Scenario(_ =>
        {
            _.Get.Url("/fake/okay");
            _.StatusCodeShouldBeOk();
        });
    }
}

The following is the code for the base class IntegrationTestBase, where the method marked with [Before (TestSession)] is located:

public abstract class IntegrationTestBase
{
    private static RabbitMqContainer? rabbitMqContainer;
    private static PostgreSqlContainer? pgSqlContainer;
    protected static string? RabbitMqConnString { get; private set; }
    protected static string? PgSqlConnString { get; private set; }

    [Before(TestSession)]
    public static async Task SessionSetup()
    {
        // db setup
        rabbitMqContainer = new RabbitMqBuilder("rabbitmq:latest").Build();
        pgSqlContainer = new PostgreSqlBuilder("postgres:latest")
            .WithUsername("test")
            .WithPassword("test")
            .WithDatabase("testDB")
            .WithCleanUp(true)
            .Build();
        await rabbitMqContainer.StartAsync();
        await pgSqlContainer.StartAsync();
        PgSqlConnString = pgSqlContainer.GetConnectionString();
        RabbitMqConnString = rabbitMqContainer.GetConnectionString();
    }

    [After(TestSession)]
    public static async Task SessionCleanup()
    {
        if (rabbitMqContainer is not null)
            await rabbitMqContainer.DisposeAsync();

        if (pgSqlContainer is not null)
            await pgSqlContainer.DisposeAsync();
    }
}

TUnit Version

1.12.15

.NET Version

.Net 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

Additional Context

none of them

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions