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
-
Source generator tests pass - The source generator correctly generates the test metadata with ConcreteInstantiations and includes the MethodDataSource in the DataSources array.
-
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]
-
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
Description
When a generic method (not class) has both
[GenerateGenericTest]AND[MethodDataSource]attributes, the tests are not discovered at runtime.Reproduction
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-testsInvestigation findings
Source generator tests pass - The source generator correctly generates the test metadata with
ConcreteInstantiationsand includes theMethodDataSourcein theDataSourcesarray.Other generic tests work - The following combinations work correctly:
[GenerateGenericTest](no data source)[GenerateGenericTest](no data source)[MethodDataSource]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-GenerateGenericTestWithConcreteTypesfunctionTUnit.Engine/Building/TestBuilder.cs-BuildTestsFromMetadataAsynchandling ofGenericTestMetadataWorkaround
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