Overview of TUnit Argument Matchers
Returns, Throws) and verification (WasCalled) contexts. TUnit.Mocks automatically imports the Arg class via global using static, allowing matcher methods to be called directly without the Arg. prefix.website·Indexed 33 days ago
https://tunit.devA comprehensive, fluent assertion library for .NET. TUnit provides expressive assertions for boolean values, collections, dictionaries, DateTime, numeric types, strings, and async Tasks. It features support for custom assertions via source generators, member assertions for object properties, and an optional 'Should' syntax package for a FluentAssertions-style experience.
Returns, Throws) and verification (WasCalled) contexts. TUnit.Mocks automatically imports the Arg class via global using static, allowing matcher methods to be called directly without the Arg. prefix.Microsoft.Testing.Platform and focuses on performance through source generation and compile-time optimizations. Key features include flexible test data injection (new or shared instances) and lifecycle hooks that run before and after tests.TUnit supports two execution modes for test discovery and execution:
// Equality
IsEqualTo(), IsNotEqualTo()
// Comparison
IsGreaterThan(), IsLessThan(), IsBetween()
// Null/Default
IsNull(), IsNotNull(), IsDefault()
// Boolean
IsTrue(), IsFalse()
// Strings
Contains(), StartsWith(), Matches()
// Collections
Contains(), Count(), All(), Any()
// Exceptions
Throws<T>(), ThrowsNothing()
// Types
IsTypeOf<T>(), IsAssignableTo<T>()
// Async
CompletesWithin()TUnit.Mocks utilizes source generation at compile time rather than runtime proxy generation. This approach provides several technical advantages:
TestContext object via the static TestContext.Current property. This context provides metadata about the test (name, containing class, categories) and execution details. The Execution.Result property is available within AfterEachTest methods to conditionally execute logic based on the test outcome (e.g., taking a screenshot on failure).// Check for a custom property
if (TestContext.Current?.Metadata.TestDetails.CustomProperties.ContainsKey("SomeProperty") == true)
{
// Do something
}
// Conditional cleanup based on test failure
if (TestContext.Current?.Execution.Result?.State == TestState.Failed)
{
// Take a screenshot
}When migrating from MSTest to TUnit, keep these fundamental changes in mind:
async Task and require await.[TestClass] attribute is not required in TUnit.Assert.That() pattern.TestContext is passed as a method parameter, not as a class property.AspireFixture runs an OTLP receiver to correlate System Under Test (SUT) logs to the specific test that triggered them. When CreateHttpClient is used, the request includes W3C traceparent and baggage headers (including tunit.test.id). The SUT's OpenTelemetry SDK exports logs with the same TraceId, allowing TUnit to route logs to the correct test output even during concurrent execution.TUnit HTML reports automatically capture and display trace spans from any .NET library (requiring .NET 8+) that uses System.Diagnostics.Activity.
Because TUnit wraps each test body in a root System.Diagnostics.Activity, any child spans created by libraries like HttpClient, ASP.NET Core, or EF Core are automatically nested under the test's root span and rendered as a waterfall timeline in the report's test detail view. No additional configuration is required.
[Test]
public async Task GetUsers_ReturnsOk()
{
var client = Factory.CreateClient();
// The following call emits spans captured automatically by TUnit
var response = await client.GetAsync("/api/users");
await Assert.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK);
}TUnit ensures fair and reproducible performance benchmarks by adhering to three core principles: