Skip to content

Commit

Permalink
Update matrix docs (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Feb 5, 2025
1 parent 8195da8 commit 3f655e4
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions docs/docs/tutorial-basics/matrix-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ sidebar_position: 6

# Matrix Tests

Combinative tests can take multiple values for different arguments, and then generate every possible combination of all of those arguments.
The Matrix data source is a way to specify different arguments per parameter, and then generate every possible combination of all of those arguments.

Now bear in mind, that as your number of arguments and/or parameters increase, that the number of test cases will grow exponentially. This means you could very quickly get into the territory of generating thousands of test cases. So use it with caution.

For our arguments, we'll add a `[Matrix]` attribute. Instead of this being added to the test method, it's added to the parameters themselves.

And for the test method, we'll add a `[MatrixDataSource]` attribute which contains the logic to extract out all the data from those parameter Matrix attributes.

Here's an example:

```csharp
Expand All @@ -23,6 +25,7 @@ namespace MyTestProject;
public class MyTestClass
{
[Test]
[MatrixDataSource]
public async Task MyTest(
[Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] int value1,
[Matrix(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] int value2
Expand All @@ -40,4 +43,84 @@ public class MyTestClass
}
```

That will generate 100 test cases. 10 different values for value1, and 10 different values for value2. 10*10 is 100.
That will generate 100 test cases. 10 different values for value1, and 10 different values for value2. 10\*10 is 100.

## Matrix Range

You can also use the `[MatrixRange<T>]` for numerical types. It will generated a range between the minimum and maximum, with an optional step parameter to define how far to step between each value. By default, this is 1.

```csharp
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Assertions.Extensions.Is;
using TUnit.Core;

namespace MyTestProject;

public class MyTestClass
{
[Test]
[MatrixDataSource]
public async Task MyTest(
[MatrixRange<int>(1, 10)] int value1,
[MatrixRange<int>(1, 10)] int value2
)
{
var result = Add(value1, value2);

await Assert.That(result).IsPositive();
}

private int Add(int x, int y)
{
return x + y;
}
}
```

## Matrix Method

You can also specify a method that will return an `IEnumerable<T>` of values.

```csharp
using TUnit.Assertions;
using TUnit.Assertions.Extensions;
using TUnit.Assertions.Extensions.Is;
using TUnit.Core;

namespace MyTestProject;

public class MyTestClass
{
[Test]
[MatrixDataSource]
public async Task MyTest(
[MatrixRange<int>(1, 10)] int value1,
[MatrixMethod(nameof(Numbers))] int value2
)
{
var result = Add(value1, value2);

await Assert.That(result).IsPositive();
}

private int Add(int x, int y)
{
return x + y;
}

private IEnumerable<int> Numbers()
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield return 5;
yield return 6;
yield return 7;
yield return 8;
yield return 9;
yield return 10;
}
}
```

0 comments on commit 3f655e4

Please sign in to comment.