Skip to content

Generic methods with [GenerateGenericTest] + [MethodDataSource] are not discovered #4440

@thomhurst

Description

@thomhurst

Description

When a generic method (not class) has both [GenerateGenericTest] AND [MethodDataSource] attributes, the tests are not discovered at runtime.

Reproduction

public class NonGenericClassWithGenericMethodAndDataSource
{
    public static IEnumerable<string> GetStrings()
    {
        yield return "hello";
        yield return "world";
    }

    [Test]
    [GenerateGenericTest(typeof(int))]
    [GenerateGenericTest(typeof(double))]
    [MethodDataSource(nameof(GetStrings))]
    public async Task GenericMethod_With_DataSource<T>(string input)
    {
        await Assert.That(input).IsNotNull();
        await Assert.That(typeof(T).IsValueType).IsTrue();
    }
}

Expected: 4 tests discovered and executed:

  • GenericMethod_With_DataSource<int>("hello")
  • GenericMethod_With_DataSource<int>("world")
  • GenericMethod_With_DataSource<double>("hello")
  • GenericMethod_With_DataSource<double>("world")

Actual: No tests are discovered when running --list-tests

Investigation findings

  1. Source generator tests pass - The source generator correctly generates the test metadata with ConcreteInstantiations and includes the MethodDataSource in the DataSources array.

  2. Other generic tests work - The following combinations work correctly:

    • Generic class with [GenerateGenericTest] (no data source)
    • Generic method with [GenerateGenericTest] (no data source)
    • Generic class with [MethodDataSource]
  3. The specific failing case: Generic method + [GenerateGenericTest] + [MethodDataSource]

Technical details

The generated code appears correct but tests are not discovered. Key files involved:

  • TUnit.Core.SourceGenerator/Generators/TestMetadataGenerator.cs - GenerateGenericTestWithConcreteTypes function
  • TUnit.Engine/Building/TestBuilder.cs - BuildTestsFromMetadataAsync handling of GenericTestMetadata

Workaround

Use separate test methods - one generic method without data source, or split into non-generic method with data source.

Related

This was discovered while investigating #4431. The fix for generic classes is in PR #4439, but this issue remains for generic methods.

Labels

bug, source-generator

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions