-
-
Notifications
You must be signed in to change notification settings - Fork 108
Description
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
ConcreteInstantiationsand includes theMethodDataSourcein theDataSourcesarray. -
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]
- Generic class with
-
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 ofGenericTestMetadata
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