A modern, flexible and fast testing framework for .NET 8 and up. With Native AOT and Trimmed Single File application support included!
TUnit is designed to aid with all testing types:
- Unit
- Integration
- Acceptance
- and more!
See here: https://thomhurst.github.io/TUnit/
TUnit leverages source generators to locate and register your tests as opposed to reflection. You'll have a slight bump in build time, but a speedier runtime.
TUnit also builds upon the newer Microsoft.Testing.Platform, whereas most other frameworks you'll have used will use VSTest. The new platform was reconstructed from the ground up to address pain points, be more extensible, and be faster.
One of the most powerful parts of TUnit is the information you have available to you because of the source generation and the events you can subscribe to. Because tests are constructed at the point of discovery, and not at runtime, you know all your arguments, properties, etc. upfront.
You can then register to be notified about various events such as test registered (scheduled to run in this test session at some point in the future), test started, test finished, etc.
Say we injected an external object into our tests: By knowing how many tests are registered, we could count them up, and then on a test end event, we could decrease the count. When hitting 0, we know our object isn't going to be used by any other tests, so we can dispose of it. We know when we can handle the lifecycle, and this prevents it from living till the end of the test session where it could be hanging on to precious resources.
TUnit tries to help you write your tests correctly with analyzers. If something isn't quite right, an analyzer should tell you what's wrong.
TUnit is built on top of the newer Microsoft.Testing.Platform, as opposed to the older VSTest platform. Because the infrastructure behind the scenes is new and different, you may need to enable some settings. This should just be a one time thing.
Visual Studio is supported. The "Use testing platform server mode" option must be selected in Tools > Manage Preview Features.
Rider is supported. The Enable Testing Platform support option must be selected in Settings > Build, Execution, Deployment > Unit Testing > VSTest.
Visual Studio Code is supported.
- Install the extension Name: C# Dev Kit
- Go to the C# Dev Kit extension's settings
- Enable Dotnet > Test Window > Use Testing Platform Protocol
dotnet
CLI - Fully supported. Tests should be runnable with dotnet test
, dotnet run
, dotnet exec
or executing an executable directly. See the docs for more information!
To be used when you want to define re-useable components, such as a test library, but it wouldn't be run as its own test suite.
For test suites. This contains the test execution logic and test adapter. Only install this on actual test projects you intend to run, not class libraries.
This is independent from the framework and can be used wherever - Even in other test frameworks. It is just an assertion library used to assert data is as you expect. It uses an asychronous syntax which may be different to other assertion libraries you may have used.
This is a helper package to combine the above 3 packages. If you just want a standard test app where you can write, run and assert tests, just install this!
This provides you base classes, similarly to Microsoft.Playwright.NUnit or Microsoft.Playwright.MSTest, to automatically create and dispose of Playwright objects in tests, to make it easier for you to write tests without worrying about lifecycles or disposing. The base classes are named the same as the other libraries: PageTest
, ContextTest
, BrowserTest
, and PlaywrightTest
.
- Native AOT / Trimmed Single File application support
- Source generated tests
- Property injection
- Full async support
- Parallel by default, with mechanisms to:
- Run specific tests completely on their own
- Run specific tests not in parallel with other specific tests
- Limit the parallel limit on a per-test, class or assembly level
- Tests can depend on other tests to form chains, useful for if one test depends on state from another action. While not recommended for unit tests, this can be useful in integration testing where state matters
- Easy to read assertions - though you're also free to use whichever assertion library you like
- Injectable test data via classes, methods, compile-time args, or matrices
- Hooks before and after:
- TestDiscover
- TestSession
- Assembly
- Class
- Test
- Designed to avoid common pitfalls such as leaky test states
- Dependency injection support (See here)
- Ability to view and interrogate metadata and results from various assembly/class/test context objects
dotnet add package TUnit --prerelease
private static readonly TimeOnly Midnight = TimeOnly.FromTimeSpan(TimeSpan.Zero);
private static readonly TimeOnly Noon = TimeOnly.FromTimeSpan(TimeSpan.FromHours(12));
[Test]
public async Task IsMorning()
{
var time = GetTime();
await Assert.That(time).IsAfterOrEqualTo(Midnight)
.And.IsBefore(Noon);
}
or with more complex test orchestration needs
[Before(Class)]
public static async Task ClearDatabase(ClassHookContext context) { ... }
[After(Class)]
public static async Task AssertDatabaseIsAsExpected(ClassHookContext context) { ... }
[Before(Test)]
public async Task CreatePlaywrightBrowser(TestContext context) { ... }
[After(Test)]
public async Task DisposePlaywrightBrowser(TestContext context) { ... }
[Retry(3)]
[Test, DisplayName("Register an account")]
[MethodData(nameof(GetAuthDetails))]
public async Task Register(string username, string password) { ... }
[Repeat(5)]
[Test, DependsOn(nameof(Register))]
[MethodData(nameof(GetAuthDetails))]
public async Task Login(string username, string password) { ... }
[Test, DependsOn(nameof(Login), [typeof(string), typeof(string)])]
[MethodData(nameof(GetAuthDetails))]
public async Task DeleteAccount(string username, string password) { ... }
[Category("Downloads")]
[Timeout(300_000)]
[Test, NotInParallel(Order = 1)]
public async Task DownloadFile1() { ... }
[Category("Downloads")]
[Timeout(300_000)]
[Test, NotInParallel(Order = 2)]
public async Task DownloadFile2() { ... }
[Repeat(10)]
[Test]
[Arguments(1)]
[Arguments(2)]
[Arguments(3)]
[DisplayName("Go to the page numbered $page")]
public async Task GoToPage(int page) { ... }
[Category("Cookies")]
[Test, Skip("Not yet built!")]
public async Task CheckCookies() { ... }
[Test, Explicit, WindowsOnlyTest, RetryHttpServiceUnavailable(5)]
[Property("Some Key", "Some Value")]
public async Task Ping() { ... }
[Test]
[ParallelLimit<LoadTestParallelLimit>]
[Repeat(1000)]
public async Task LoadHomepage() { ... }
public static IEnumerable<(string Username, string Password)> GetAuthDetails()
{
yield return ("user1", "password1");
yield return ("user2", "password2");
yield return ("user3", "password3");
}
public class WindowsOnlyTestAttribute : SkipAttribute
{
public WindowsOnlyTestAttribute() : base("Windows only test")
{
}
public override Task<bool> ShouldSkip(TestContext testContext)
{
return Task.FromResult(!OperatingSystem.IsWindows());
}
}
public class RetryHttpServiceUnavailableAttribute : RetryAttribute
{
public RetryHttpServiceUnavailableAttribute(int times) : base(times)
{
}
public override Task<bool> ShouldRetry(TestInformation testInformation, Exception exception, int currentRetryCount)
{
return Task.FromResult(exception is HttpRequestException { StatusCode: HttpStatusCode.ServiceUnavailable });
}
}
public class LoadTestParallelLimit : IParallelLimit
{
public int Limit => 50;
}
TUnit is inspired by NUnit and xUnit - two of the most popular testing frameworks for .NET.
It aims to build upon the useful features of both while trying to address any pain points that they may have.
You'll notice that version 1.0 isn't out yet. While this framework is mostly feature complete, I'm waiting for a few things:
- Full Rider support for all features
- Full VS support for all features
- Open to feedback on existing features
- Open to ideas on new features
As such, the API may change. I'll try to limit this but it's a possibility.
BenchmarkDotNet v0.14.0, macOS Sonoma 14.7 (23H124) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
Build_TUnit | 1,042.4 ms | 26.04 ms | 75.97 ms |
Build_NUnit | 859.4 ms | 16.17 ms | 14.34 ms |
Build_xUnit | 907.8 ms | 18.03 ms | 47.17 ms |
Build_MSTest | 1,472.4 ms | 80.52 ms | 234.88 ms |
BenchmarkDotNet v0.14.0, Ubuntu 22.04.5 LTS (Jammy Jellyfish)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
Build_TUnit | 1.829 s | 0.0257 s | 0.0228 s |
Build_NUnit | 1.522 s | 0.0278 s | 0.0260 s |
Build_xUnit | 1.515 s | 0.0174 s | 0.0136 s |
Build_MSTest | 1.589 s | 0.0272 s | 0.0255 s |
BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2849) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
Build_TUnit | 2.098 s | 0.0413 s | 0.0424 s |
Build_NUnit | 1.540 s | 0.0244 s | 0.0216 s |
Build_xUnit | 1.530 s | 0.0215 s | 0.0191 s |
Build_MSTest | 1.574 s | 0.0303 s | 0.0284 s |
Scenario: A single test that completes instantly (including spawning a new process and initialising the test framework)
BenchmarkDotNet v0.14.0, macOS Sonoma 14.7 (23H124) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev | Median |
---|---|---|---|---|
TUnit_AOT | 93.12 ms | 1.850 ms | 3.337 ms | 92.39 ms |
TUnit | 602.24 ms | 47.823 ms | 137.212 ms | 533.66 ms |
NUnit | 809.62 ms | 19.635 ms | 56.337 ms | 798.22 ms |
xUnit | 755.79 ms | 10.228 ms | 9.567 ms | 754.16 ms |
MSTest | 677.61 ms | 11.644 ms | 10.892 ms | 679.00 ms |
BenchmarkDotNet v0.14.0, Ubuntu 22.04.5 LTS (Jammy Jellyfish)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
TUnit_AOT | 32.71 ms | 0.650 ms | 0.952 ms |
TUnit | 854.84 ms | 16.967 ms | 20.836 ms |
NUnit | 1,311.93 ms | 17.039 ms | 15.105 ms |
xUnit | 1,307.33 ms | 26.022 ms | 28.923 ms |
MSTest | 1,218.37 ms | 14.125 ms | 13.213 ms |
BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2849) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
TUnit_AOT | 77.97 ms | 0.170 ms | 0.151 ms |
TUnit | 834.68 ms | 16.307 ms | 25.387 ms |
NUnit | 1,309.65 ms | 21.410 ms | 20.027 ms |
xUnit | 1,267.72 ms | 10.715 ms | 10.023 ms |
MSTest | 1,165.20 ms | 22.945 ms | 25.503 ms |
Scenario: A test that takes 50ms to execute, repeated 100 times (including spawning a new process and initialising the test framework)
BenchmarkDotNet v0.14.0, macOS Sonoma 14.7 (23H124) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), Arm64 RyuJIT AdvSIMD
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
TUnit_AOT | 246.5 ms | 10.13 ms | 29.70 ms |
TUnit | 732.1 ms | 30.09 ms | 88.24 ms |
NUnit | 14,271.0 ms | 280.97 ms | 541.34 ms |
xUnit | 14,448.9 ms | 269.54 ms | 450.35 ms |
MSTest | 14,396.1 ms | 271.13 ms | 553.85 ms |
BenchmarkDotNet v0.14.0, Ubuntu 22.04.5 LTS (Jammy Jellyfish)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
TUnit_AOT | 99.48 ms | 2.112 ms | 6.228 ms |
TUnit | 896.59 ms | 17.843 ms | 28.300 ms |
NUnit | 6,505.15 ms | 27.090 ms | 25.340 ms |
xUnit | 6,530.15 ms | 39.224 ms | 36.690 ms |
MSTest | 6,487.74 ms | 22.029 ms | 19.528 ms |
BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2849) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
.NET 9.0 : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX2
Job=.NET 9.0 Runtime=.NET 9.0
Method | Mean | Error | StdDev |
---|---|---|---|
TUnit_AOT | 128.0 ms | 2.77 ms | 8.17 ms |
TUnit | 908.7 ms | 17.90 ms | 25.68 ms |
NUnit | 7,514.0 ms | 13.83 ms | 12.94 ms |
xUnit | 7,510.9 ms | 7.81 ms | 6.92 ms |
MSTest | 7,482.7 ms | 25.60 ms | 23.94 ms |