Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/core/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ For more information, see the following resources:

#### TUnit

[TUnit](https://thomhurst.github.io/TUnit/) is entirely built on top of Microsoft.Testing.Platform and doesn't support VSTest. For more information, refer to TUnit documentation.
[TUnit](https://tunit.dev/) is a testing framework for .NET that is built entirely on top of Microsoft.Testing.Platform and doesn't support VSTest. TUnit uses source generation for C# test discovery and is Native AOT compatible. Tests run in parallel by default.

For more information, see the following resources:

- [TUnit official documentation](https://tunit.dev/)
- [Unit testing with C#](unit-testing-csharp-with-tunit.md)
- [Unit testing with F#](unit-testing-fsharp-with-tunit.md)
- [Unit testing with Visual Basic](unit-testing-visual-basic-with-tunit.md)

#### xUnit.net

Expand Down
2 changes: 1 addition & 1 deletion docs/core/testing/microsoft-testing-platform-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The main driving factors for the evolution of the new testing platform are detai
* MSTest. In MSTest, the support of `Microsoft.Testing.Platform` is done via [MSTest runner](unit-testing-mstest-runner-intro.md).
* NUnit. In NUnit, the support of `Microsoft.Testing.Platform` is done via [NUnit runner](unit-testing-nunit-runner-intro.md).
* xUnit.net: In xUnit.net, the support of `Microsoft.Testing.Platform` is done via [xUnit.net runner](https://xunit.net/docs/getting-started/v3/microsoft-testing-platform).
* TUnit: entirely constructed on top of the `Microsoft.Testing.Platform`, for more information, see [TUnit documentation](https://tunit.dev/).
* TUnit: Built entirely on Microsoft.Testing.Platform and doesn't support VSTest. For more information, see [TUnit documentation](https://tunit.dev/) and [Getting started with TUnit](unit-testing-csharp-with-tunit.md).

## Run and debug tests

Expand Down
53 changes: 53 additions & 0 deletions docs/core/testing/order-unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,59 @@ To order tests explicitly, NUnit provides an [`OrderAttribute`](https://docs.nun

:::code language="csharp" source="snippets/order-unit-tests/csharp/NUnit.TestProject/ByOrder.cs":::

:::zone-end
:::zone pivot="tunit"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to update docs/zone-pivot-groups.yml for this to work, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated them


## Order by dependency

TUnit provides a `[DependsOn]` attribute to control test execution order through explicit dependencies. When a test depends on another, TUnit ensures the prerequisite test completes before executing the dependent test. This approach allows you to maintain test parallelism for independent tests while enforcing order where necessary.

```csharp
using TUnit.Core;

namespace OrderUnitTests.TUnit;

public class DependencyOrderedTests
{
[Test]
public async Task FirstTest()
{
// This test runs first
await Task.CompletedTask;
}

[Test]
[DependsOn(nameof(FirstTest))]
public async Task SecondTest()
{
// This test runs after FirstTest completes
await Task.CompletedTask;
}
}
```

### Behavior

* **Failure handling** - By default, if a dependency fails, the dependent test is skipped. You can override this behavior by setting `ProceedOnFailure = true` on the `DependsOnAttribute`.
* **Accessing dependent test context** - You can retrieve data from a prerequisite test's context using the `GetTests` method on the `TestContext` object.
* **Multiple dependencies** - You can apply multiple `[DependsOn]` attributes to a single test to create complex dependency chains.

### Handling method overloads

When depending on overloaded test methods, specify the parameter types explicitly:

```csharp
[Test]
[DependsOn(nameof(SetupTest), [typeof(string), typeof(int)])]
public async Task DependentTest()
{
// This test runs after SetupTest(string, int) completes
}
```

> [!NOTE]
> While `[DependsOn]` provides ordering capabilities, tests should ideally be self-contained, isolated, and side-effect free. Reserve dependency ordering for scenarios where independent tests are impractical, such as deployment pipelines or expensive multi-step workflows.

:::zone-end

## Next Steps
Expand Down
59 changes: 58 additions & 1 deletion docs/core/testing/selective-unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ dotnet test --filter <Expression>
| Test framework | Supported properties |
| -------------- | -------------------- |
| MSTest | `FullyQualifiedName`<br>`Name`<br>`ClassName`<br>`Priority`<br>`TestCategory` |
| NUnit | `FullyQualifiedName`<br>`Name`<br>`Priority`<br>`TestCategory` |
| xUnit | `FullyQualifiedName`<br>`DisplayName`<br>`Traits` |
| Nunit | `FullyQualifiedName`<br>`Name`<br>`Priority`<br>`TestCategory` |

* **Operators**

Expand Down Expand Up @@ -230,6 +230,63 @@ dotnet test --filter "(FullyQualifiedName~UnitTest1&TestCategory=CategoryA)|Prio

For more information, see [TestCase filter](https://github.com/Microsoft/vstest-docs/blob/main/docs/filter.md).

:::zone-end
:::zone pivot="tunit"

## TUnit examples

```csharp
using TUnit.Core;

namespace TUnitNamespace
{
public class UnitTest1
{
[Test, Property("Priority", "1"), Category("CategoryA")]
public async Task TestMethod1()
{
}

[Test, Property("Priority", "2")]
public async Task TestMethod2()
{
}
}
}
```

TUnit uses the `--treenode-filter` flag with a path-based syntax:

| Expression | Result |
|--|--|
| `dotnet test --treenode-filter "/*/*/*/*Method*"` | Runs tests whose method name contains `Method`. |
| `dotnet test --treenode-filter "/*/*/*/TestMethod1"` | Runs tests whose name is `TestMethod1`. |
| `dotnet test --treenode-filter "/*/TUnitNamespace/UnitTest1/*"` | Runs all tests in class `TUnitNamespace.UnitTest1`. |
| `dotnet test --treenode-filter "/**[Category=CategoryA]"` | Runs tests that are annotated with `[Category("CategoryA")]`. |
| `dotnet test --treenode-filter "/**[Priority=2]"` | Runs tests that have `[Property("Priority", "2")]`. |

In the code example, the `[Property]` and `[Category]` attributes can be used for filtering.

Examples using the conditional operators `|` and `&`:

To run tests that have `UnitTest1` in their class name **or** have a `Category` of `"CategoryA"`.

```dotnetcli
dotnet test --treenode-filter "(/*/*/UnitTest1/*)|/**[Category=CategoryA]"
```

To run tests that are in class `UnitTest1` **and** have a `Category` of `"CategoryA"`.

```dotnetcli
dotnet test --treenode-filter "(/*/*/UnitTest1/*)&/**[Category=CategoryA]"
```

To run tests that have either class `UnitTest1` **and** `Category` of `"CategoryA"` **or** have a `Property` with `"Priority"` of `"2"`.

```dotnetcli
dotnet test --treenode-filter "((/*/*/UnitTest1/*)&/**[Category=CategoryA])|/**[Priority=2]"
```

:::zone-end

## See also
Expand Down
78 changes: 75 additions & 3 deletions docs/core/testing/unit-testing-code-coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,25 @@ There are two types of code coverage tools:

In this section, the focus is on data collector tools.

.NET includes a built-in code coverage data collector, which is also available in Visual Studio. This data collector generates a binary *.coverage* file that can be used to generate reports in Visual Studio. The binary file isn't human-readable, and it must be converted to a human-readable format before it can be used to generate reports outside of Visual Studio.
### Code coverage approaches

.NET provides two different testing platforms, each with its own code coverage approach:

- **VSTest-based frameworks** (MSTest, NUnit, xUnit) - Can use either the built-in .NET code coverage collector or [Coverlet](https://github.com/coverlet-coverage/coverlet), an open-source alternative.
- **Microsoft.Testing.Platform-based frameworks** (TUnit, MSTest runner, NUnit runner, xUnit runner) - Use [Microsoft.Testing.Extensions.CodeCoverage](microsoft-testing-platform-extensions-code-coverage.md).

The built-in .NET code coverage data collector generates a binary *.coverage* file that can be used to generate reports in Visual Studio. The binary file isn't human-readable, and it must be converted to a human-readable format before it can be used to generate reports outside of Visual Studio.

> [!TIP]
> The `dotnet-coverage` tool is a cross-platform tool that can be used to convert the binary coverage test results file to a human-readable format. For more information, see [dotnet-coverage](../additional-tools/dotnet-coverage.md).

[Coverlet](https://github.com/coverlet-coverage/coverlet) is an open-source alternative to the built-in collector. It generates test results as human-readable Cobertura XML files, which can then be used to generate HTML reports. To use Coverlet for code coverage, an existing unit test project must have the appropriate package dependencies, or alternatively rely on [.NET global tooling](../tools/global-tools.md) and the corresponding [coverlet.console](https://www.nuget.org/packages/coverlet.console) NuGet package.
Coverlet generates test results as human-readable Cobertura XML files, which can then be used to generate HTML reports. To use Coverlet for code coverage, an existing unit test project must have the appropriate package dependencies, or alternatively rely on [.NET global tooling](../tools/global-tools.md) and the corresponding [coverlet.console](https://www.nuget.org/packages/coverlet.console) NuGet package.

## Code coverage with VSTest

VSTest is the traditional testing platform used by MSTest, NUnit, and xUnit. For VSTest-based projects, you can use either Coverlet or the built-in .NET code coverage collector.

## Integrate with .NET test
### Using Coverlet with VSTest

The xUnit test project template already integrates with [coverlet.collector](https://www.nuget.org/packages/coverlet.collector) by default.
From the command prompt, change directories to the *XUnit.Coverlet.Collector* project, and run the [`dotnet test`](../tools/dotnet-test.md) command:
Expand Down Expand Up @@ -295,6 +306,67 @@ After running this command, an HTML file represents the generated report.

:::image type="content" source="media/test-report.png" lightbox="media/test-report.png" alt-text="Unit test-generated report":::

## Code coverage with Microsoft.Testing.Platform

[Microsoft.Testing.Platform](microsoft-testing-platform-intro.md) is the modern testing platform for .NET. Frameworks built on this platform use [Microsoft.Testing.Extensions.CodeCoverage](microsoft-testing-platform-extensions-code-coverage.md) for code coverage.

The following frameworks support Microsoft.Testing.Platform:
- **MSTest** (via MSTest runner)
- **NUnit** (via NUnit runner)
- **xUnit** (via xUnit runner)
- **TUnit**

### Setting up code coverage with Microsoft.Testing.Platform

The setup process is consistent across all Microsoft.Testing.Platform-based frameworks. Add the code coverage package to your test project:

```dotnetcli
dotnet add package Microsoft.Testing.Extensions.CodeCoverage
```

### Configuring Microsoft.Testing.Platform mode

Some frameworks require Microsoft.Testing.Platform mode to be explicitly configured. Add this configuration to your `global.json` file in the solution root:

```json
{
"test": {
"runner": "Microsoft.Testing.Platform"
}
}
```

> [!NOTE]
> This configuration requires .NET 10 SDK or later. Frameworks like MSTest runner, NUnit runner, and xUnit runner support both VSTest and Microsoft.Testing.Platform, so this configuration is optional for those frameworks. TUnit only supports Microsoft.Testing.Platform, so this configuration is required.

### Running code coverage

Run tests with code coverage using the `--coverage` flag:

```dotnetcli
dotnet test --coverage
```

This generates a `.coverage` file in the `TestResults` directory. The `--coverage` flag is specific to Microsoft.Testing.Platform and provides a unified code coverage experience across all supported frameworks.

To generate reports in other formats, use the `--coverage-output-format` option:

```dotnetcli
dotnet test --coverage --coverage-output-format cobertura
```

Supported output formats include:
- `coverage` (binary format, default)
- `cobertura` (XML format)
- `xml` (XML format)

### Framework-specific documentation

For detailed information about using Microsoft.Testing.Platform with specific frameworks, see:
- [Unit testing with MSTest runner](unit-testing-mstest-runner-intro.md)
- [Unit testing with NUnit runner](unit-testing-nunit-runner-intro.md)
- [Unit testing C# with TUnit](unit-testing-csharp-with-tunit.md)

## See also

- [Visual Studio unit test code coverage](/visualstudio/test/using-code-coverage-to-determine-how-much-code-is-being-tested)
Expand Down
Loading