Skip to content

thomhurst/TUnit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TUnit

thomhurst%2FTUnit | Trendshift

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!

GitHub Repo stars GitHub Sponsors nuget NuGet Downloads GitHub Workflow Status (with event) GitHub last commit (branch) License

Documentation

See here: https://thomhurst.github.io/TUnit/

IDE

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

Visual Studio is supported on the Preview version currently.

  • Install the latest preview version
  • Open Visual Studio and go to Tools > Manage Preview Features
  • Enable "Use testing platform server mode"

Rider

Rider is supported. The Enable Testing Platform support option must be selected in Settings > Build, Execution, Deployment > Unit Testing > VSTest.

VS Code

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

CLI

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!

Features

  • 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

Installation

dotnet add package TUnit --prerelease

Example test

    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;
    }

Motivations

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.

Read more here

Prerelease

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.

Benchmark

Scenario: Building the test project

macos-latest


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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev Median
Build_TUnit 1,091.6 ms 52.49 ms 150.60 ms 1,043.7 ms
Build_NUnit 1,024.6 ms 37.19 ms 109.66 ms 1,059.7 ms
Build_xUnit 1,008.7 ms 62.37 ms 181.94 ms 939.6 ms
Build_MSTest 906.5 ms 22.95 ms 67.68 ms 888.4 ms

ubuntu-latest


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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
Build_TUnit 1.667 s 0.0330 s 0.0440 s
Build_NUnit 1.509 s 0.0292 s 0.0336 s
Build_xUnit 1.500 s 0.0238 s 0.0211 s
Build_MSTest 1.562 s 0.0145 s 0.0128 s

windows-latest


BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2700) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
Build_TUnit 1.768 s 0.0311 s 0.0456 s
Build_NUnit 1.594 s 0.0318 s 0.0485 s
Build_xUnit 1.621 s 0.0261 s 0.0231 s
Build_MSTest 1.656 s 0.0328 s 0.0390 s

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

macos-latest


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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev Median
TUnit_AOT 144.1 ms 5.50 ms 15.77 ms 134.7 ms
TUnit 541.8 ms 10.66 ms 14.59 ms 540.7 ms
NUnit 847.9 ms 16.00 ms 17.12 ms 847.7 ms
xUnit 765.1 ms 22.40 ms 64.63 ms 764.1 ms
MSTest 621.5 ms 6.43 ms 5.37 ms 622.4 ms

ubuntu-latest


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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 63.94 ms 0.970 ms 0.860 ms
TUnit 842.77 ms 16.777 ms 23.518 ms
NUnit 1,320.31 ms 20.782 ms 19.439 ms
xUnit 1,314.60 ms 25.541 ms 28.388 ms
MSTest 1,175.84 ms 20.078 ms 17.799 ms

windows-latest


BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2700) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 125.8 ms 2.42 ms 3.07 ms
TUnit 892.4 ms 17.64 ms 29.47 ms
NUnit 1,365.2 ms 9.83 ms 9.20 ms
xUnit 1,337.4 ms 9.24 ms 8.64 ms
MSTest 1,222.6 ms 13.85 ms 12.96 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.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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), Arm64 RyuJIT AdvSIMD

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 293.1 ms 13.27 ms 39.11 ms
TUnit 617.4 ms 19.24 ms 56.72 ms
NUnit 13,988.5 ms 273.31 ms 485.81 ms
xUnit 14,657.4 ms 289.97 ms 579.09 ms
MSTest 14,438.0 ms 287.78 ms 655.43 ms

ubuntu-latest


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-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 126.9 ms 2.53 ms 5.04 ms
TUnit 907.7 ms 18.11 ms 25.38 ms
NUnit 6,546.1 ms 31.81 ms 29.75 ms
xUnit 6,551.3 ms 21.44 ms 19.00 ms
MSTest 6,505.2 ms 21.23 ms 18.82 ms

windows-latest


BenchmarkDotNet v0.14.0, Windows 10 (10.0.20348.2700) (Hyper-V)
AMD EPYC 7763, 1 CPU, 4 logical and 2 physical cores
.NET SDK 9.0.100-rc.2.24474.11
  [Host]   : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.0 (9.0.24.47305), X64 RyuJIT AVX2

Job=.NET 9.0  Runtime=.NET 9.0  

Method Mean Error StdDev
TUnit_AOT 179.2 ms 3.54 ms 8.07 ms
TUnit 957.8 ms 18.50 ms 22.72 ms
NUnit 7,572.6 ms 12.39 ms 11.59 ms
xUnit 7,533.3 ms 14.60 ms 12.95 ms
MSTest 7,500.2 ms 19.14 ms 17.91 ms

About

A modern, fast and flexible .NET testing framework

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages