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
1 change: 1 addition & 0 deletions TUnit.Core/TestDetails.Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public partial class TestDetails
bool ITestDetailsMetadata.HasAttribute<T>() => HasAttribute<T>();
IEnumerable<T> ITestDetailsMetadata.GetAttributes<T>() => GetAttributes<T>();
IReadOnlyList<Attribute> ITestDetailsMetadata.GetAllAttributes() => GetAllAttributes();

}
21 changes: 6 additions & 15 deletions TUnit.Core/TestDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace TUnit.Core;
/// </summary>
public partial class TestDetails : ITestIdentity, ITestClass, ITestMethod, ITestConfiguration, ITestLocation, ITestDetailsMetadata
{
private readonly IReadOnlyList<Attribute> _allAttributes;

// Zero-allocation interface properties for organized API access
public ITestIdentity Identity => this;
public ITestClass Class => this;
Expand Down Expand Up @@ -40,19 +42,9 @@ public partial class TestDetails : ITestIdentity, ITestClass, ITestMethod, ITest

public required IReadOnlyDictionary<Type, IReadOnlyList<Attribute>> AttributesByType { get; init; }

private readonly Lazy<IReadOnlyList<Attribute>> _cachedAllAttributes;

public TestDetails()
public TestDetails(IReadOnlyList<Attribute> allAttributes)
{
_cachedAllAttributes = new Lazy<IReadOnlyList<Attribute>>(() =>
{
var allAttrs = new List<Attribute>();
foreach (var attrList in AttributesByType?.Values ?? [])
{
allAttrs.AddRange(attrList);
}
return allAttrs;
});
_allAttributes = allAttributes;
}

/// <summary>
Expand All @@ -75,10 +67,9 @@ public IEnumerable<T> GetAttributes<T>() where T : Attribute

/// <summary>
/// Gets all attributes as a flattened collection.
/// Cached after first access for performance.
/// </summary>
/// <returns>All attributes associated with this test.</returns>
public IReadOnlyList<Attribute> GetAllAttributes() => _cachedAllAttributes.Value;
public IReadOnlyList<Attribute> GetAllAttributes() => _allAttributes;

/// <summary>
/// Resolved generic type arguments for the test method.
Expand All @@ -96,4 +87,4 @@ public IEnumerable<T> GetAttributes<T>() where T : Attribute
/// <summary>
/// Generic version of TestDetails for compatibility with tests
/// </summary>
public class TestDetails<T> : TestDetails where T : class;
public class TestDetails<T>(IReadOnlyList<Attribute> allAttributes) : TestDetails(allAttributes) where T : class;
7 changes: 4 additions & 3 deletions TUnit.Engine/Building/TestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ private async ValueTask<TestContext> CreateTestContextAsync(string testId, TestM
attributes = [..attributes, (Attribute)testBuilderContext.DataSourceAttribute];
}

var testDetails = new TestDetails
var testDetails = new TestDetails(attributes)
{
TestId = testId,
TestName = metadata.TestName,
Expand Down Expand Up @@ -1074,7 +1074,8 @@ private async Task<AbstractExecutableTest> CreateFailedTestForDataGenerationErro

private async Task<TestDetails> CreateFailedTestDetails(TestMetadata metadata, string testId)
{
return new TestDetails
var attributes = (await InitializeAttributesAsync(metadata.AttributeFactory.Invoke()));
return new TestDetails(attributes)
{
TestId = testId,
TestName = metadata.TestName,
Expand All @@ -1087,7 +1088,7 @@ private async Task<TestDetails> CreateFailedTestDetails(TestMetadata metadata, s
TestLineNumber = metadata.LineNumber,
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
AttributesByType = (await InitializeAttributesAsync(metadata.AttributeFactory.Invoke())).ToAttributeDictionary(),
AttributesByType = attributes.ToAttributeDictionary(),
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
};
}
Expand Down
8 changes: 4 additions & 4 deletions TUnit.Engine/Building/TestBuilderPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private async Task<AbstractExecutableTest[]> GenerateDynamicTests(TestMetadata m
var attributes = metadata.AttributeFactory();

// Create TestDetails for dynamic tests
var testDetails = new TestDetails
var testDetails = new TestDetails(attributes)
{
TestId = testId,
TestName = metadata.TestName,
Expand Down Expand Up @@ -377,7 +377,7 @@ private async IAsyncEnumerable<AbstractExecutableTest> BuildTestsFromSingleMetad
: baseDisplayName;

// Create TestDetails for dynamic tests
var testDetails = new TestDetails
var testDetails = new TestDetails(attributes)
{
TestId = testId,
TestName = resolvedMetadata.TestName,
Expand Down Expand Up @@ -457,7 +457,7 @@ private AbstractExecutableTest CreateFailedTestForDataGenerationError(TestMetada
var testId = TestIdentifierService.GenerateFailedTestId(metadata);
var displayName = $"{metadata.TestClassType.Name}.{metadata.TestName}";

var testDetails = new TestDetails
var testDetails = new TestDetails([])
{
TestId = testId,
TestName = metadata.TestName,
Expand Down Expand Up @@ -509,7 +509,7 @@ private AbstractExecutableTest CreateFailedTestForGenericResolutionError(TestMet
var testId = TestIdentifierService.GenerateFailedTestId(metadata);
var displayName = $"{metadata.TestName} [GENERIC RESOLUTION ERROR]";

var testDetails = new TestDetails
var testDetails = new TestDetails([])
{
TestId = testId,
TestName = metadata.TestName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ namespace
}
public class TestDetails : ., ., ., ., ., .
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
public . Attributes { get; }
public required .<, .<>> AttributesByType { get; init; }
public .<string> Categories { get; }
Expand Down Expand Up @@ -1457,7 +1457,7 @@ namespace
public class TestDetails<T> : .TestDetails
where T : class
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
}
public class TestDiscoveryContext : .Context
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ namespace
}
public class TestDetails : ., ., ., ., ., .
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
public . Attributes { get; }
public required .<, .<>> AttributesByType { get; init; }
public .<string> Categories { get; }
Expand Down Expand Up @@ -1457,7 +1457,7 @@ namespace
public class TestDetails<T> : .TestDetails
where T : class
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
}
public class TestDiscoveryContext : .Context
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ namespace
}
public class TestDetails : ., ., ., ., ., .
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
public . Attributes { get; }
public required .<, .<>> AttributesByType { get; init; }
public .<string> Categories { get; }
Expand Down Expand Up @@ -1457,7 +1457,7 @@ namespace
public class TestDetails<T> : .TestDetails
where T : class
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
}
public class TestDiscoveryContext : .Context
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ namespace
}
public class TestDetails : ., ., ., ., ., .
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
public . Attributes { get; }
public required .<, .<>> AttributesByType { get; init; }
public .<string> Categories { get; }
Expand Down Expand Up @@ -1411,7 +1411,7 @@ namespace
public class TestDetails<T> : .TestDetails
where T : class
{
public TestDetails() { }
public TestDetails(.<> allAttributes) { }
}
public class TestDiscoveryContext : .Context
{
Expand Down
Loading