Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TUnit.Engine/Services/TestArgumentRegistrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public TestArgumentRegistrationService(ObjectLifecycleService objectLifecycleSer
/// </summary>
public async ValueTask RegisterTestArgumentsAsync(TestContext testContext)
{
TestContext.Current = testContext;

var classArguments = testContext.Metadata.TestDetails.TestClassArguments;
var methodArguments = testContext.Metadata.TestDetails.TestMethodArguments;

Expand Down
46 changes: 46 additions & 0 deletions TUnit.UnitTests/PropertyDataSourceInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,49 @@ public string GetDeepData()
}
}

// Regression test for Issue #3991: TestContext.Current is null during property injection
public class TestContextAvailabilityDuringPropertyInjectionTests
{
[TestContextDataSource]
public required TestContextCapture? ContextCapture { get; set; }

[Test]
public async Task PropertyInjection_CanAccessTestContext_DuringDiscoveryPhase()
{
// This test verifies that TestContext.Current is available during property injection
// in the discovery/registration phase (Issue #3991)
await Assert.That(ContextCapture).IsNotNull();
await Assert.That(ContextCapture!.TestContextWasAvailable).IsTrue();
await Assert.That(ContextCapture.CapturedTestContextId).IsNotNull();
}
}

// Data source that accesses TestContext.Current during initialization
// This reproduces the bug from Issue #3991
public class TestContextDataSourceAttribute : AsyncDataSourceGeneratorAttribute<TestContextCapture>
{
protected override async IAsyncEnumerable<Func<Task<TestContextCapture>>> GenerateDataSourcesAsync(DataGeneratorMetadata dataGeneratorMetadata)
{
yield return async () =>
{
// This is where the bug occurs: TestContext.Current is null during discovery
var testContext = TestContext.Current;

return new TestContextCapture
{
TestContextWasAvailable = testContext != null,
CapturedTestContextId = testContext?.Id,
CapturedCancellationToken = testContext?.CancellationToken ?? default
};
};
await Task.CompletedTask;
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The await Task.CompletedTask; statement is unreachable code after a yield return statement in an async iterator method. This line will never execute and should be removed. The method naturally completes after all yield return statements have been executed.

Suggested change
await Task.CompletedTask;

Copilot uses AI. Check for mistakes.
}
}

public class TestContextCapture
{
public bool TestContextWasAvailable { get; set; }
public string? CapturedTestContextId { get; set; }
public CancellationToken CapturedCancellationToken { get; set; }
}

Loading