Skip to content

Add field support to DynamicDataAttribute #6203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 31, 2025
Merged

Add field support to DynamicDataAttribute #6203

merged 13 commits into from
Jul 31, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 28, 2025

This PR adds support for public static fields as data sources in DynamicDataAttribute, addressing a key performance improvement request for test data management.

Problem

Previously, DynamicDataAttribute only supported properties and methods as data sources. This meant that even for constant test data, new objects were created on each access due to property getters or method calls, despite being static:

// Property - creates new array on each access
public static IEnumerable<object[]> TestData => new[]
{
    new object[] { 25, 250 },
    new object[] { 42, 420 },
    new object[] { 164, 1640 }
};

// Method - also creates new array on each call  
public static IEnumerable<object[]> GetTestData() => new[]
{
    new object[] { 25, 250 },
    new object[] { 42, 420 },
    new object[] { 164, 1640 }
};

Solution

Added comprehensive support for static fields as data sources, allowing truly constant test data:

// Field - created once, reused efficiently
public static int[][] TestData = new[]
{
    new[] { 25, 250 },
    new[] { 42, 420 },
    new[] { 164, 1640 }
};

[TestMethod]
[DynamicData(nameof(TestData), DynamicDataSourceType.Field)]
public void TestWithExplicitField(int input, int expected) { /* ... */ }

[TestMethod]
[DynamicData(nameof(TestData))] // Auto-detect also works
public void TestWithAutoDetect(int input, int expected) { /* ... */ }

Key Features

  • Explicit field specification: DynamicDataSourceType.Field for direct field targeting
  • Auto-detection support: Fields are discovered automatically after properties and methods
  • External class support: Works with fields in other classes via declaring type parameter
  • Inheritance support: Fields from base classes are properly resolved
  • Full integration: Complete support in analyzer, source generation, and runtime
  • Comprehensive validation: Proper error messages for non-static fields and type mismatches

Implementation Details

  • Extended DynamicDataSourceType enum with Field = 3 option
  • Added field detection and data extraction methods in DynamicDataOperations
  • Updated auto-detect logic with priority: Property → Method → Field
  • Enhanced analyzer to validate field usage and provide appropriate diagnostics
  • Updated source generation to handle field symbols correctly
  • Added comprehensive test coverage for all field scenarios

Backward Compatibility

This change is fully backward compatible - all existing code continues to work exactly as before. The new field support is purely additive functionality.

Fixes #5321.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 37cvsblobprodcus359.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • 4zjvsblobprodcus390.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • 7devsblobprodcus323.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • ljcvsblobprodcus317.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • sqdvsblobprodcus333.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • st8vsblobprodcus339.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)
  • ytvvsblobprodcus310.vsblob.vsassets.io
    • Triggering command: dotnet build src/TestFramework/TestFramework/TestFramework.csproj (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Feature Request]: Allow DynamicDataAttribute to accept a public static class field Add field support to DynamicDataAttribute Jul 28, 2025
@Copilot Copilot AI requested a review from Evangelink July 28, 2025 12:48
Copilot finished work on behalf of Evangelink July 28, 2025 12:48
@Evangelink Evangelink marked this pull request as ready for review July 28, 2025 20:13
@Copilot Copilot AI requested a review from Evangelink July 28, 2025 20:22
Copilot finished work on behalf of Evangelink July 28, 2025 20:22
# Conflicts:
#	src/TestFramework/TestFramework/PublicAPI/PublicAPI.Unshipped.txt
@Evangelink Evangelink enabled auto-merge (squash) July 30, 2025 14:50
@Evangelink Evangelink merged commit 428ddf8 into main Jul 31, 2025
8 checks passed
@Evangelink Evangelink deleted the copilot/fix-5321 branch July 31, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Allow DynamicDataAttribute to accept a public static class field
3 participants