[Bug]: The method marked with [Before (TestSession)] never runs
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?
- [x] I've confirmed this issue occurs when running via
dotnet testordotnet run, not just in my IDE
I created a test to reproduce this issue and it passes on the current main branch:
public abstract class AbstractBaseWithTestSessionHook
{
public static bool TestSessionHookWasExecuted { get; private set; }
[Before(TestSession)]
public static Task BeforeTestSession()
{
TestSessionHookWasExecuted = true;
return Task.CompletedTask;
}
}
public class ConcreteTestClass : AbstractBaseWithTestSessionHook
{
[Test]
public async Task Verify_TestSession_Hook_On_Abstract_Base_Class_Executed()
{
await Assert.That(TestSessionHookWasExecuted).IsTrue();
}
}
The hook on the abstract base class executes correctly.
Could you help me understand what's different in your setup? A few questions:
- Is your
IntegrationTestBasein the same project as your tests, or a separate project/assembly? - Does
IntegrationTestBasehave any generic type parameters? - Could you share a minimal reproduction that demonstrates the issue?
Also, could you try with the latest version from main to see if the issue persists? It's possible this was fixed recently.
Thank you for taking the time to reply amidst your busy schedule.
IntegrationTestBase is an independent project that is referenced by all of my tests as a base class.
IntegrationTestBase does not have generic parameters, in fact, what I posted in the above question is the complete code of my IntegrationTestBase.
I am more than happy to provide a simple example. I have created a new repo for him for you to view. After closing this issue, I will delete this repo: link Thank you again for your help.
Give the latest version a try
Hello, I am using the latest version 1.12.53, but [Before (TestSession)] still doesn't seem to work
link This is a simple example of reproducing the problem, which is not much different from the previous example except for updating the TUnit version, but the method marked with [Before (TestSession)] in the base class still does not execute
@thomhurst