Skip to content

thomhurst/TUnit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ The Modern Testing Framework for .NET

TUnit is a next-generation testing framework for C# that outpaces traditional frameworks with source-generated tests, parallel execution by default, and Native AOT support. Built on the modern Microsoft.Testing.Platform, TUnit delivers faster test runs, better developer experience, and unmatched flexibility.

thomhurst%2FTUnit | Trendshift

Codacy BadgeGitHub Repo stars GitHub Issues or Pull Requests GitHub Sponsors nuget NuGet Downloads GitHub Workflow Status (with event) GitHub last commit (branch) License

โšก Why Choose TUnit?

Feature Traditional Frameworks TUnit
Test Discovery โŒ Runtime reflection โœ… Compile-time generation
Execution Speed โŒ Sequential by default โœ… Parallel by default
Modern .NET โš ๏ธ Limited AOT support โœ… Full Native AOT & trimming
Test Dependencies โŒ Not supported โœ… [DependsOn] chains
Resource Management โŒ Manual lifecycle โœ… Intelligent cleanup

โšก Parallel by Default - Tests run concurrently with intelligent dependency management

๐ŸŽฏ Compile-Time Discovery - Know your test structure before runtime

๐Ÿ”ง Modern .NET Ready - Native AOT, trimming, and latest .NET features

๐ŸŽญ Extensible - Customize data sources, attributes, and test behavior


๐Ÿš€ New to TUnit? Start with our Getting Started Guide

๐Ÿ”„ Migrating? See our Migration Guides

๐ŸŽฏ Advanced Features? Explore Data-Driven Testing, Test Dependencies, and Parallelism Control


๐Ÿ Quick Start

Using the Project Template (Recommended)

dotnet new install TUnit.Templates
dotnet new TUnit -n "MyTestProject"

Manual Installation

dotnet add package TUnit --prerelease

๐Ÿ“– ๐Ÿ“š Complete Documentation & Guides - Everything you need to master TUnit

โœจ Key Features

๐Ÿš€ Performance & Modern Platform

  • ๐Ÿ”ฅ Source-generated tests (no reflection)
  • โšก Parallel execution by default
  • ๐Ÿš€ Native AOT & trimming support
  • ๐Ÿ“ˆ Optimized for performance

๐ŸŽฏ Advanced Test Control

  • ๐Ÿ”— Test dependencies with [DependsOn]
  • ๐ŸŽ›๏ธ Parallel limits & custom scheduling
  • ๐Ÿ›ก๏ธ Built-in analyzers & compile-time checks
  • ๐ŸŽญ Custom attributes & extensible conditions

๐Ÿ“Š Rich Data & Assertions

  • ๐Ÿ“‹ Multiple data sources ([Arguments], [Matrix], [ClassData])
  • โœ… Fluent async assertions
  • ๐Ÿ”„ Smart retry logic & conditional execution
  • ๐Ÿ“ Rich test metadata & context

๐Ÿ”ง Developer Experience

  • ๐Ÿ’‰ Full dependency injection support
  • ๐Ÿช Comprehensive lifecycle hooks
  • ๐ŸŽฏ IDE integration (VS, Rider, VS Code)
  • ๐Ÿ“š Extensive documentation & examples

๐Ÿ“ Simple Test Example

[Test]
public async Task User_Creation_Should_Set_Timestamp()
{
    // Arrange
    var userService = new UserService();

    // Act
    var user = await userService.CreateUserAsync("[email protected]");

    // Assert - TUnit's fluent assertions
    await Assert.That(user.CreatedAt)
        .IsEqualTo(DateTime.Now)
        .Within(TimeSpan.FromMinutes(1));

    await Assert.That(user.Email)
        .IsEqualTo("[email protected]");
}

๐ŸŽฏ Data-Driven Testing

[Test]
[Arguments("[email protected]", "ValidPassword123")]
[Arguments("[email protected]", "AnotherPassword456")]
[Arguments("[email protected]", "AdminPass789")]
public async Task User_Login_Should_Succeed(string email, string password)
{
    var result = await authService.LoginAsync(email, password);
    await Assert.That(result.IsSuccess).IsTrue();
}

// Matrix testing - tests all combinations
[Test]
[MatrixDataSource]
public async Task Database_Operations_Work(
    [Matrix("Create", "Update", "Delete")] string operation,
    [Matrix("User", "Product", "Order")] string entity)
{
    await Assert.That(await ExecuteOperation(operation, entity))
        .IsTrue();
}

๐Ÿ”— Advanced Test Orchestration

[Before(Class)]
public static async Task SetupDatabase(ClassHookContext context)
{
    await DatabaseHelper.InitializeAsync();
}

[Test, DisplayName("Register a new account")]
[MethodDataSource(nameof(GetTestUsers))]
public async Task Register_User(string username, string password)
{
    // Test implementation
}

[Test, DependsOn(nameof(Register_User))]
[Retry(3)] // Retry on failure
public async Task Login_With_Registered_User(string username, string password)
{
    // This test runs after Register_User completes
}

[Test]
[ParallelLimit<LoadTestParallelLimit>] // Custom parallel control
[Repeat(100)] // Run 100 times
public async Task Load_Test_Homepage()
{
    // Performance testing
}

// Custom attributes
[Test, WindowsOnly, RetryOnHttpError(5)]
public async Task Windows_Specific_Feature()
{
    // Platform-specific test with custom retry logic
}

public class LoadTestParallelLimit : IParallelLimit
{
    public int Limit => 10; // Limit to 10 concurrent executions
}

๐Ÿ”ง Smart Test Control

// Custom conditional execution
public class WindowsOnlyAttribute : SkipAttribute
{
    public WindowsOnlyAttribute() : base("Windows only test") { }

    public override Task<bool> ShouldSkip(TestContext testContext)
        => Task.FromResult(!OperatingSystem.IsWindows());
}

// Custom retry logic
public class RetryOnHttpErrorAttribute : RetryAttribute
{
    public RetryOnHttpErrorAttribute(int times) : base(times) { }

    public override Task<bool> ShouldRetry(TestInformation testInformation,
        Exception exception, int currentRetryCount)
        => Task.FromResult(exception is HttpRequestException { StatusCode: HttpStatusCode.ServiceUnavailable });
}

๐ŸŽฏ Perfect For Every Testing Scenario

๐Ÿงช Unit Testing

[Test]
[Arguments(1, 2, 3)]
[Arguments(5, 10, 15)]
public async Task Calculate_Sum(int a, int b, int expected)
{
    await Assert.That(Calculator.Add(a, b))
        .IsEqualTo(expected);
}

Fast, isolated, and reliable

๐Ÿ”— Integration Testing

[Test, DependsOn(nameof(CreateUser))]
public async Task Login_After_Registration()
{
    // Runs after CreateUser completes
    var result = await authService.Login(user);
    await Assert.That(result.IsSuccess).IsTrue();
}

Stateful workflows made simple

โšก Load Testing

[Test]
[ParallelLimit<LoadTestLimit>]
[Repeat(1000)]
public async Task API_Handles_Concurrent_Requests()
{
    await Assert.That(await httpClient.GetAsync("/api/health"))
        .HasStatusCode(HttpStatusCode.OK);
}

Built-in performance testing

๐Ÿš€ What Makes TUnit Different?

Compile-Time Intelligence

Tests are discovered at build time, not runtime - enabling faster discovery, better IDE integration, and precise resource lifecycle management.

Parallel-First Architecture

Built for concurrency from day one with [DependsOn] for test chains, [ParallelLimit] for resource control, and intelligent scheduling.

Extensible by Design

The DataSourceGenerator<T> pattern and custom attribute system let you extend TUnit's capabilities without modifying core framework code.

๐Ÿ† Community & Ecosystem

๐ŸŒŸ Join thousands of developers modernizing their testing

Downloads Contributors Discussions

๐Ÿค Active Community

๐Ÿ› ๏ธ IDE Support

TUnit works seamlessly across all major .NET development environments:

Visual Studio (2022 17.13+)

โœ… Fully supported - No additional configuration needed for latest versions

โš™๏ธ Earlier versions: Enable "Use testing platform server mode" in Tools > Manage Preview Features

JetBrains Rider

โœ… Fully supported

โš™๏ธ Setup: Enable "Testing Platform support" in Settings > Build, Execution, Deployment > Unit Testing > VSTest

Visual Studio Code

โœ… Fully supported

โš™๏ธ Setup: Install C# Dev Kit and enable "Use Testing Platform Protocol"

Command Line

โœ… Full CLI support - Works with dotnet test, dotnet run, and direct executable execution

๐Ÿ“ฆ Package Options

Package Use Case
TUnit โญ Start here - Complete testing framework (includes Core + Engine + Assertions)
TUnit.Core ๐Ÿ“š Test libraries and shared components (no execution engine)
TUnit.Engine ๐Ÿš€ Test execution engine and adapter (for test projects)
TUnit.Assertions โœ… Standalone assertions (works with any test framework)
TUnit.Playwright ๐ŸŽญ Playwright integration with automatic lifecycle management

๐ŸŽฏ Migration from Other Frameworks

Coming from NUnit or xUnit? TUnit maintains familiar syntax while adding modern capabilities:

// Enhanced with TUnit's advanced features
[Test]
[Arguments("value1")]
[Arguments("value2")]
[Retry(3)]
[ParallelLimit<CustomLimit>]
public async Task Modern_TUnit_Test(string value) { }

๐Ÿ“– Need help migrating? Check our detailed Migration Guides with step-by-step instructions for xUnit, NUnit, and MSTest.

๐Ÿ’ก Current Status

The API is mostly stable, but may have some changes based on feedback or issues before v1.0 release.


๐Ÿš€ Ready to Experience the Future of .NET Testing?

โšก Start in 30 Seconds

# Create a new test project with examples
dotnet new install TUnit.Templates && dotnet new TUnit -n "MyAwesomeTests"

# Or add to existing project
dotnet add package TUnit --prerelease

๐ŸŽฏ Why Wait? Join the Movement

๐Ÿ“ˆ Performance

Optimized execution Parallel by default Zero reflection overhead

๐Ÿ”ฎ Future-Ready

Native AOT support Latest .NET features Source generation

๐Ÿ› ๏ธ Developer Experience

Compile-time checks Rich IDE integration Intelligent debugging

๐ŸŽญ Flexibility

Test dependencies Custom attributes Extensible architecture


๐Ÿ“– Learn More: tunit.dev | ๐Ÿ’ฌ Get Help: GitHub Discussions | โญ Show Support: Star on GitHub

TUnit is actively developed and production-ready. Join our growing community of developers who've made the switch!

Performance Benchmark

Scenario: Building the test project

macos-latest


BenchmarkDotNet v0.15.2, macOS Sonoma 14.7.6 (23H626) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev Median
Build_TUnit 1,195.2 ms 41.66 ms 118.18 ms 1,168.6 ms
Build_NUnit 949.9 ms 35.76 ms 103.74 ms 915.1 ms
Build_xUnit 912.8 ms 39.98 ms 116.63 ms 873.3 ms
Build_MSTest 907.8 ms 18.13 ms 48.09 ms 902.7 ms

ubuntu-latest


BenchmarkDotNet v0.15.2, Linux Ubuntu 24.04.2 LTS (Noble Numbat)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
Build_TUnit 1.944 s 0.0380 s 0.0591 s
Build_NUnit 1.511 s 0.0166 s 0.0155 s
Build_xUnit 1.505 s 0.0288 s 0.0332 s
Build_MSTest 1.473 s 0.0275 s 0.0294 s

windows-latest


BenchmarkDotNet v0.15.2, Windows 10 (10.0.20348.3695) (Hyper-V)
AMD EPYC 7763 2.44GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
Build_TUnit 1.895 s 0.0332 s 0.0536 s
Build_NUnit 1.490 s 0.0176 s 0.0156 s
Build_xUnit 1.479 s 0.0255 s 0.0226 s
Build_MSTest 1.504 s 0.0190 s 0.0178 s

Scenario: A single test that completes instantly (including spawning a new process and initialising the test framework)

macos-latest


BenchmarkDotNet v0.15.2, macOS Sonoma 14.7.6 (23H626) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 73.13 ms 0.676 ms 0.564 ms
TUnit 476.00 ms 6.706 ms 5.945 ms
NUnit 709.57 ms 11.568 ms 15.443 ms
xUnit 722.15 ms 8.798 ms 8.230 ms
MSTest 629.19 ms 11.363 ms 8.871 ms

ubuntu-latest


BenchmarkDotNet v0.15.2, Linux Ubuntu 24.04.2 LTS (Noble Numbat)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 26.80 ms 0.749 ms 2.198 ms
TUnit 806.94 ms 15.996 ms 22.423 ms
NUnit 1,265.35 ms 9.973 ms 9.328 ms
xUnit 1,319.47 ms 8.417 ms 7.873 ms
MSTest 1,117.79 ms 9.172 ms 8.580 ms

windows-latest


BenchmarkDotNet v0.15.2, Windows 10 (10.0.20348.3695) (Hyper-V)
AMD EPYC 7763 2.44GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 78.96 ms 1.461 ms 1.367 ms
TUnit 906.14 ms 17.843 ms 23.201 ms
NUnit 1,364.59 ms 14.652 ms 13.706 ms
xUnit 1,394.09 ms 20.137 ms 18.836 ms
MSTest 1,197.66 ms 12.341 ms 11.544 ms

Scenario: A test that takes 50ms to execute, repeated 100 times (including spawning a new process and initialising the test framework)

macos-latest


BenchmarkDotNet v0.15.2, macOS Sonoma 14.7.6 (23H626) [Darwin 23.6.0]
Apple M1 (Virtual), 1 CPU, 3 logical and 3 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 245.1 ms 12.51 ms 36.89 ms
TUnit 687.9 ms 23.73 ms 69.97 ms
NUnit 14,147.6 ms 279.59 ms 353.59 ms
xUnit 14,191.1 ms 281.52 ms 606.00 ms
MSTest 14,196.6 ms 280.49 ms 644.46 ms

ubuntu-latest


BenchmarkDotNet v0.15.2, Linux Ubuntu 24.04.2 LTS (Noble Numbat)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev Median
TUnit_AOT 74.00 ms 1.447 ms 2.418 ms 72.68 ms
TUnit 878.49 ms 17.376 ms 19.314 ms 875.09 ms
NUnit 6,258.70 ms 13.241 ms 12.385 ms 6,258.58 ms
xUnit 6,385.30 ms 11.289 ms 9.427 ms 6,386.97 ms
MSTest 6,215.62 ms 5.275 ms 4.405 ms 6,215.27 ms

windows-latest


BenchmarkDotNet v0.15.2, Windows 10 (10.0.20348.3695) (Hyper-V)
AMD EPYC 7763 2.44GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.301
  [Host]   : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.6 (9.0.625.26613), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev Median
TUnit_AOT 111.9 ms 2.21 ms 3.25 ms 109.4 ms
TUnit 952.1 ms 19.01 ms 25.37 ms 952.8 ms
NUnit 7,524.8 ms 47.39 ms 44.33 ms 7,506.6 ms
xUnit 7,568.8 ms 32.18 ms 30.10 ms 7,563.7 ms
MSTest 7,441.2 ms 20.01 ms 18.72 ms 7,442.0 ms

About

A modern, fast and flexible .NET testing framework

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Contributors 44

Languages