TUnit.Assertions.Analyzers
0.1.454
Prefix Reserved
TUnit.Assertions handles analyzing natively now.
See the version list below for details.
dotnet add package TUnit.Assertions.Analyzers --version 0.1.454
NuGet\Install-Package TUnit.Assertions.Analyzers -Version 0.1.454
<PackageReference Include="TUnit.Assertions.Analyzers" Version="0.1.454" />
paket add TUnit.Assertions.Analyzers --version 0.1.454
#r "nuget: TUnit.Assertions.Analyzers, 0.1.454"
// Install TUnit.Assertions.Analyzers as a Cake Addin #addin nuget:?package=TUnit.Assertions.Analyzers&version=0.1.454 // Install TUnit.Assertions.Analyzers as a Cake Tool #tool nuget:?package=TUnit.Assertions.Analyzers&version=0.1.454
TUnit
T(est)Unit!
Documentation
See here: https://thomhurst.github.io/TUnit/
IDE
TUnit is built on top of newer Microsoft.Testing.Platform libraries, as opposed to older legacy VSTest libraries. As of July 2024, IDEs do not fully support this testing platform yet.
Visual Studio Preview versions can run the new tests by enabling the new testing platform server mode, within Visual Studio preview/experimental features. You will have to opt in to this manually.
For Rider, it is not yet supported. I believe they are working on it so we just have to wait for now.
dotnet
CLI - Fully supported. Tests should be runnable with both dotnet test
or dotnet run
. dotnet run
should give you a better experience and make it simpler to pass in test flags!
Features
- Source generated tests
- Full async support
- Parallel by default, with mechanisms to switch it off for certain tests
- Test ordering (if running not in parallel)
- Tests can depend on other tests to form chains
- Easy to read assertions
- Injectable test data functionality
- Hooks before and after: Assembly, Class, Test
- Designed to avoid common pitfalls such as leaky test states
- Ability to view and interrogate metadata and results from various assembly/class/test context objects
Installation
dotnet add package TUnit --prerelease
Example test
[Test]
public async Task Test1()
{
var value = "Hello world!";
await Assert.That(value)
.Is.Not.Null
.And.Does.StartWith("H")
.And.Has.Count().EqualTo(12)
.And.Is.EqualTo("hello world!", StringComparison.InvariantCultureIgnoreCase);
}
or with more complex test orchestration needs
[BeforeAllTestsInClass]
public static async Task ClearDatabase(ClassHookContext context) { ... }
[AfterAllTestsInClass]
public static async Task AssertDatabaseIsAsExpected(ClassHookContext context) { ... }
[BeforeEachTest]
public async Task CreatePlaywrightBrowser(TestContext context) { ... }
[AfterEachTest]
public async Task DisposePlaywrightBrowser(TestContext context) { ... }
[Retry(3)]
[Test, DisplayName("Register an account")]
[EnumerableMethodData(nameof(GetAuthDetails))]
public async Task Register(string username, string password) { ... }
[DataSourceDrivenTest, DependsOn(nameof(Register))]
[EnumerableMethodData(nameof(GetAuthDetails))]
public async Task Login(string username, string password) { ... }
[DataSourceDrivenTest, DependsOn(nameof(Login), [typeof(string), typeof(string)])]
[EnumerableMethodData(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)]
[DataDrivenTest]
[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]
[Property("Some Key", "Some Value")]
public async Task Ping() { ... }
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 });
}
}
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. You may have experienced these, or you may have not even known and experienced flakiness or bugs due to it.
NUnit by default creates 1 test class for all the tests within it, and runs them all against the same instance. Therefore if a test stores or accesses any state, it could be a hangover from another test, meaning leaky test states and potentially dodgy data.
xUnit doesn't offer a way out-of-the-box to retrieve the test state within a hook. For example, running UI tests, in a tear down method, you might want to take a screenshot but only if the test failed. The hook is written generically to work with every test, but we don't have anything available to us to query for the test status.
xUnit performs tear downs through interfaces such as IDisposable. TUnit also allows this, but a problem occurs if you want to use test class inheritance. Say you have a class that wants to take a screenshot, and then a base class that disposes the browser. You have to declare another Dispose()
method, and hide the visibility of the base one with the new
keyword, which is generally frowned up. You then also have to remember to call base.Dispose()
- and if you don't, you may have bugs and/or unreleased resources. And then to top it off, you have to manage exceptions yourself to ensure they still run. In TUnit, all cleanup methods will run, even if previous code has encountered exceptions.
xUnit assertions are fairly basic and have the problem of it being unclear which argument goes in which position:
var one = 2;
Assert.Equal(1, one)
Assert.Equal(one, 1)
NUnit assertions largely influenced the way that TUnit assertions work. However, NUnit assertions do not have compile time checks. I could check if a string is negative (NUnitAssert.That("String", Is.Negative);
) or if a boolean throws an exception (NUnitAssert.That(true, Throws.ArgumentException);
). These assertions don't make sense. There are analyzers to help catch these - But they will compile if these analyzers aren't run.
TUnit assertions are built with the type system in mind. Specific assertions are built via extensions to the relevant types, and not in a generic sense that could apply to anything. That means when you're using intellisense to see what methods you have available, you should only see assertions that are relevant for your type. This makes it harder to make mistakes, and decreases your feedback loop time.
Extras
TUnit offers a few extra bits that NUnit and xUnit do not (that I'm aware of):
- Tests are source generated - And not relied upon using reflection to discover them
- Hooks are available at the assembly, class and test level, each with respective objects available to be referenced in your hook methods. An
AssemblyHookContext
object will have details on the current assembly and a collection of all the tests its discovered. If you're in a tear down, each test object will have details of its result. AClassHookContext
is the same, but with details of the class and its tests instead. And aTestContext
will just be the details for a single test. - Dependent tests - Tests can depend on another and delay their execution until their dependencies have finished - Without turning off parallelism or having to manually work out the best way to configure this.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.1)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
0.1.984 | 606 | 10/7/2024 | |
0.1.980 | 84 | 10/7/2024 | |
0.1.971 | 81 | 10/7/2024 | |
0.1.966 | 97 | 10/7/2024 | |
0.1.962 | 86 | 10/6/2024 | |
0.1.961 | 85 | 10/6/2024 | |
0.1.958 | 82 | 10/6/2024 | |
0.1.954 | 91 | 10/6/2024 | |
0.1.949 | 84 | 10/6/2024 | |
0.1.942 | 90 | 10/5/2024 | |
0.1.939 | 85 | 10/5/2024 | |
0.1.937 | 87 | 10/5/2024 | |
0.1.934 | 83 | 10/5/2024 | |
0.1.931 | 89 | 10/5/2024 | |
0.1.928 | 86 | 10/4/2024 | |
0.1.923 | 90 | 10/4/2024 | |
0.1.916 | 85 | 10/3/2024 | |
0.1.911 | 98 | 10/3/2024 | |
0.1.897 | 116 | 10/2/2024 | |
0.1.894 | 88 | 10/2/2024 | |
0.1.891 | 91 | 10/2/2024 | |
0.1.885 | 91 | 10/1/2024 | |
0.1.877 | 94 | 9/29/2024 | |
0.1.872 | 101 | 9/29/2024 | |
0.1.869 | 93 | 9/29/2024 | |
0.1.864 | 92 | 9/28/2024 | |
0.1.863 | 95 | 9/28/2024 | |
0.1.857 | 90 | 9/28/2024 | |
0.1.842 | 95 | 9/26/2024 | |
0.1.827 | 93 | 9/25/2024 | |
0.1.818 | 92 | 9/24/2024 | |
0.1.817 | 88 | 9/24/2024 | |
0.1.816 | 89 | 9/24/2024 | |
0.1.812 | 101 | 9/23/2024 | |
0.1.807 | 90 | 9/23/2024 | |
0.1.804 | 110 | 9/22/2024 | |
0.1.801 | 99 | 9/22/2024 | |
0.1.797 | 90 | 9/22/2024 | |
0.1.793 | 110 | 9/21/2024 | |
0.1.791 | 88 | 9/21/2024 | |
0.1.790 | 87 | 9/21/2024 | |
0.1.783 | 100 | 9/20/2024 | |
0.1.773 | 95 | 9/20/2024 | |
0.1.767 | 95 | 9/19/2024 | |
0.1.763 | 94 | 9/19/2024 | |
0.1.762 | 98 | 9/19/2024 | |
0.1.759 | 98 | 9/19/2024 | |
0.1.752 | 93 | 9/18/2024 | |
0.1.741 | 96 | 9/17/2024 | |
0.1.738 | 118 | 9/16/2024 | |
0.1.732 | 115 | 9/16/2024 | |
0.1.720 | 109 | 9/15/2024 | |
0.1.716 | 115 | 9/15/2024 | |
0.1.714 | 109 | 9/15/2024 | |
0.1.711 | 103 | 9/15/2024 | |
0.1.703 | 323 | 9/14/2024 | |
0.1.697 | 201 | 9/14/2024 | |
0.1.691 | 326 | 9/13/2024 | |
0.1.686 | 171 | 9/13/2024 | |
0.1.672 | 390 | 9/12/2024 | |
0.1.667 | 207 | 9/11/2024 | |
0.1.664 | 142 | 9/11/2024 | |
0.1.653 | 260 | 9/11/2024 | |
0.1.639 | 330 | 9/10/2024 | |
0.1.634 | 178 | 9/10/2024 | |
0.1.623 | 310 | 9/7/2024 | |
0.1.600 | 575 | 9/6/2024 | |
0.1.582 | 337 | 9/5/2024 | |
0.1.578 | 135 | 9/5/2024 | |
0.1.575 | 125 | 9/4/2024 | |
0.1.518 | 363 | 9/2/2024 | |
0.1.512 | 1,139 | 8/30/2024 | |
0.1.508 | 131 | 8/29/2024 | |
0.1.506 | 202 | 8/28/2024 | |
0.1.505 | 145 | 8/28/2024 | |
0.1.504 | 132 | 8/28/2024 | |
0.1.503 | 176 | 8/27/2024 | |
0.1.502 | 136 | 8/27/2024 | |
0.1.497 | 131 | 8/27/2024 | |
0.1.495 | 133 | 8/27/2024 | |
0.1.486 | 138 | 8/9/2024 | |
0.1.481 | 288 | 8/8/2024 | |
0.1.479 | 142 | 8/8/2024 | |
0.1.474 | 137 | 8/7/2024 | |
0.1.471 | 169 | 8/7/2024 | |
0.1.470 | 114 | 8/7/2024 | |
0.1.467 | 132 | 8/7/2024 | |
0.1.455 | 122 | 7/31/2024 | |
0.1.454 | 98 | 7/30/2024 |