Skip to content

feat: allow class-level [MatrixDataSource] to cascade to methods with [Matrix] parameters #4765

@thomhurst

Description

@thomhurst

Problem

When a class has multiple test methods using `[Matrix]` parameters, `[MatrixDataSource]` must be repeated on every method:

```csharp
[Category("Search")]
public class ProductSearchTests
{
[Test]
[MatrixDataSource] // must repeat
public async Task Search_Returns_Results(
[Matrix("electronics", "clothing", "books")] string category,
[Matrix("price_asc", "price_desc", "name")] string sortBy) { }

[Test]
[MatrixDataSource] // must repeat again
public async Task Pagination_Works(
    [Matrix(1, 2, 3)] int page,
    [Matrix(2, 5)] int pageSize) { }

}
```

Placing `[MatrixDataSource]` on the class doesn't work - you get TUnit0049 errors on each method.

Proposed Behavior

`[MatrixDataSource]` on the class should cascade to any method that has `[Matrix]` parameters:

```csharp
[MatrixDataSource] // applies to all methods with [Matrix] params
public class ProductSearchTests
{
[Test]
public async Task Search_Returns_Results(
[Matrix("electronics", "clothing", "books")] string category,
[Matrix("price_asc", "price_desc", "name")] string sortBy) { }

[Test]
public async Task Pagination_Works(
    [Matrix(1, 2, 3)] int page,
    [Matrix(2, 5)] int pageSize) { }

[Test]
public async Task Non_Matrix_Test() { } // no [Matrix] params, unaffected

}
```

Methods without `[Matrix]` parameters would be unaffected. Methods with explicit `[MatrixDataSource]` would override the class-level attribute if needed.

Context

Discovered while building the CloudShop Aspire + TUnit example (#4761). The `ProductSearchTests` class had to repeat the attribute on every method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions