Skip to content

Class-level data-source analyzers check rows against the first constructor, not the [TestConstructor] one #7370

Closed

Description

@thomhurst

What happens

Class-level data-source analyzers check rows against the class's first constructor, not the one marked [TestConstructor] that the test instance is actually built with. A class whose rows fit the marked constructor gets a false TUnit0001 error (and the matrix and combined-data-source analyzers have the same gap).

[GenerateGenericTest(typeof(int))]
[MethodDataSource(nameof(Rows))]
public class C<T>
{
    public C(T value) { }

    [TestConstructor]
    public C(T value, string label) { }

    public static IEnumerable<(T, string)> Rows() => [((T)(object)5, "b")];

    [Test] public void Test() { }
}

Build: error TUnit0001: Attribute argument types 'T, string' don't match method parameter types 'T'. Both engine modes run the test correctly (after #7336) through the [TestConstructor] constructor.

Where

  • src/TUnit.Analyzers/TestDataAnalyzer.cs:131: namedTypeSymbol.InstanceConstructors.FirstOrDefault()?.Parameters
  • src/TUnit.Analyzers/MatrixAnalyzer.cs:30 and src/TUnit.Analyzers/CombinedDataSourceAnalyzer.cs:35: the same FirstOrDefault()
  • src/TUnit.Analyzers/Extensions/SymbolExtensions.cs:21: the same FirstOrDefault() in the shared parameter lookup; check its callers too

Expected

Analyzers validate class-level rows against the [TestConstructor] constructor when one is marked, otherwise the current first-constructor behaviour, matching the generator's instance factory (InstanceFactoryGenerator.GetPrimaryConstructor) and, since #7336, the class parameter metadata in both modes. The fixture OwnClassTupleMethodDataSourceWithTestConstructor in tests/TUnit.TestProject/Bugs/_7208 suppresses TUnit0001 for this reason; drop the suppression once fixed.

Found while working the review of #7336.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions