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
17 changes: 12 additions & 5 deletions TUnit.Core/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace TUnit.Core;
[DebuggerDisplay("{TestDetails.ClassType.Name}.{GetDisplayName(),nq}")]
public class TestContext : Context
{
private static readonly Dictionary<Guid, TestContext> _testContextsById = new(1000);
private readonly TestBuilderContext _testBuilderContext;
private string? _cachedDisplayName;

Expand All @@ -27,8 +28,12 @@ public TestContext(string testName, IServiceProvider serviceProvider, ClassHookC
CancellationToken = cancellationToken;
ServiceProvider = serviceProvider;
ClassContext = classContext;

_testContextsById[_testBuilderContext.Id] = this;
}

public Guid Id => _testBuilderContext.Id;

private static readonly AsyncLocal<TestContext?> TestContexts = new();

internal static readonly Dictionary<string, List<string>> InternalParametersDictionary = new();
Expand All @@ -47,6 +52,8 @@ internal set
}
}

public static TestContext? GetById(Guid id) => _testContextsById.GetValueOrDefault(id);

public static IReadOnlyDictionary<string, List<string>> Parameters => InternalParametersDictionary;

public static IConfiguration Configuration { get; internal set; } = null!;
Expand Down Expand Up @@ -96,21 +103,21 @@ public static string WorkingDirectory

// New: Support multiple parallel constraints
private readonly List<IParallelConstraint> _parallelConstraints = [];

/// <summary>
/// Gets the collection of parallel constraints applied to this test.
/// Multiple constraints can be combined (e.g., ParallelGroup + NotInParallel).
/// </summary>
public IReadOnlyList<IParallelConstraint> ParallelConstraints => _parallelConstraints;

/// <summary>
/// Gets or sets the primary parallel constraint for backward compatibility.
/// When setting, this replaces all existing constraints.
/// When getting, returns the first constraint or null if none exist.
/// </summary>
[Obsolete("Use ParallelConstraints collection instead. This property is maintained for backward compatibility.")]
public IParallelConstraint? ParallelConstraint
{
public IParallelConstraint? ParallelConstraint
{
get => _parallelConstraints.FirstOrDefault();
set
{
Expand All @@ -121,7 +128,7 @@ public IParallelConstraint? ParallelConstraint
}
}
}

/// <summary>
/// Adds a parallel constraint to this test context.
/// Multiple constraints can be combined to create complex parallelization rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ namespace
public ? DisplayNameFormatter { get; set; }
public .TestContextEvents Events { get; }
public . ExecutionPriority { get; set; }
public Id { get; }
public .CancellationTokenSource? LinkedCancellationTokens { get; set; }
public object Lock { get; }
public .<string, object?> ObjectBag { get; }
Expand Down Expand Up @@ -1277,6 +1278,7 @@ namespace
public void SetParallelLimiter(. parallelLimit) { }
public void WriteError(string message) { }
public void WriteLine(string message) { }
public static .TestContext? GetById( id) { }
}
public class TestContextEvents
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ namespace
public ? DisplayNameFormatter { get; set; }
public .TestContextEvents Events { get; }
public . ExecutionPriority { get; set; }
public Id { get; }
public .CancellationTokenSource? LinkedCancellationTokens { get; set; }
public object Lock { get; }
public .<string, object?> ObjectBag { get; }
Expand Down Expand Up @@ -1277,6 +1278,7 @@ namespace
public void SetParallelLimiter(. parallelLimit) { }
public void WriteError(string message) { }
public void WriteLine(string message) { }
public static .TestContext? GetById( id) { }
}
public class TestContextEvents
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,7 @@ namespace
public ? DisplayNameFormatter { get; set; }
public .TestContextEvents Events { get; }
public . ExecutionPriority { get; set; }
public Id { get; }
public .CancellationTokenSource? LinkedCancellationTokens { get; set; }
public object Lock { get; }
public .<string, object?> ObjectBag { get; }
Expand Down Expand Up @@ -1202,6 +1203,7 @@ namespace
public void SetParallelLimiter(. parallelLimit) { }
public void WriteError(string message) { }
public void WriteLine(string message) { }
public static .TestContext? GetById( id) { }
}
public class TestContextEvents
{
Expand Down
17 changes: 17 additions & 0 deletions TUnit.TestProject/TestContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TUnit.TestProject.Attributes;

namespace TUnit.TestProject;

[EngineTest(ExpectedResult.Pass)]
public class TestContextTests
{
[Test]
public async Task Test()
{
var id = TestContext.Current!.Id;

var context = TestContext.GetById(id);

await Assert.That(context).IsNotNull();
}
}
Loading