Skip to content

Class data rows wrapping a tuple in TestDataRow cannot bind to a tuple constructor parameter #7680

Open

Description

@thomhurst

Summary

A class-level data source row that wraps a tuple in TestDataRow<T> cannot bind to a tuple constructor parameter: the tuple is spread into its elements, so the test fails while creating the instance. The same row works for a test method parameter. Users would expect a class row and a method row to bind the same way.

Reproduction

[MethodDataSource(nameof(Rows))]
public class Example(Tuple<int> value)
{
    public static IEnumerable<object?[]> Rows() => [[new TestDataRow<Tuple<int>>(Tuple.Create(1))]];

    [Test]
    public void Test() { }
}

Expected and actual

Expected: the constructor receives Tuple.Create(1) and the test passes.

Actual, source-generated mode: InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Tuple1[System.Int32]'`.

Actual, reflection mode: ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.Tuple1[System.Int32]'`.

Observed on a generic class with an optional second parameter while working on #7599, at 263fe79, on net10.0. The non-generic form above shows the same code path but has not been run separately.

Cause

Class rows are unwrapped with DataUnwrapper.UnwrapWithMetadata (src/TUnit.Engine/Building/TestBuilder.cs:244 and :1090), which spreads tuples without looking at parameter types. Method rows use UnwrapWithTypesAndMetadata (TestBuilder.cs:339), which keeps a tuple whole when it fits the parameter.

Please make class rows use the same type-aware unwrapping as method rows.

Activity

  1. added
    bugSomething isn't working
    P2Medium: edge case, mode divergence, flaky test, or workaround exists
    on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions