TracingAssertions.TUnit
0.2.0
Prefix Reserved
dotnet add package TracingAssertions.TUnit --version 0.2.0
NuGet\Install-Package TracingAssertions.TUnit -Version 0.2.0
<PackageReference Include="TracingAssertions.TUnit" Version="0.2.0" />
<PackageVersion Include="TracingAssertions.TUnit" Version="0.2.0" />
<PackageReference Include="TracingAssertions.TUnit" />
paket add TracingAssertions.TUnit --version 0.2.0
#r "nuget: TracingAssertions.TUnit, 0.2.0"
#:package TracingAssertions.TUnit@0.2.0
#addin nuget:?package=TracingAssertions.TUnit&version=0.2.0
#tool nuget:?package=TracingAssertions.TUnit&version=0.2.0
TracingAssertions.TUnit
Scope: Test projects only. Not intended for production code.
TUnit-native OpenTelemetry distributed-tracing (Activity / span) assertions for .NET tests. Fluent entry points over TUnit's Assert.That(...) pipeline for asserting on captured spans. AOT-compatible, trimmable, no runtime reflection in the assertion path.
What ships
Span assertions on Assert.That(span) where span is a System.Diagnostics.Activity:
| Entry point | Behavior |
|---|---|
HasOperationName(name) |
Asserts the span's OperationName equals name (ordinal). |
HasTag(key) |
Asserts a tag key is present (non-null value). |
HasTagValue(key, value) |
Asserts the tag key matches value (compared by invariant ToString). |
HasStatus(status) |
Asserts the span's Status equals the given ActivityStatusCode. |
HasKind(kind) (v0.2.0+) |
Asserts the span's Kind equals the given ActivityKind (for example Server / Client). |
IsChildOf(parent) |
Asserts a single-hop parent/child relationship in the same trace. |
IsRoot() (v0.2.0+) |
Asserts the span has no parent. |
HasEvent(name) / HasExceptionEvent() (v0.2.0+) |
Asserts the span carries an ActivityEvent with that name / the OpenTelemetry exception event. |
SharesTraceWith(other) |
Asserts two spans share a TraceId. |
HasSpan(operationName) / HasNoSpan(operationName) (HasNoSpan v0.2.0+) (on SpanCapture) |
Asserts the capture does / does not contain a span with that operation name. |
HasSpanCount(int) (v0.2.0+) (on SpanCapture) |
Asserts the capture collected exactly that many spans. |
The framework-agnostic core (TracingAssertions) ships SpanCapture for collecting spans from one or more ActivitySources via a raw ActivityListener (no OpenTelemetry SDK or NuGet runtime dependency), plus the query helpers FindByOperationName, FindByOperationNameAndTag, and ChildrenOf. ForSource / ForSources take an optional ActivitySamplingResult (v0.2.0+) to capture under a chosen sampling level.
Install
dotnet add package TracingAssertions.TUnit
Requirements: TUnit 1.54.0 or later, .NET 10. The framework-agnostic TracingAssertions core comes transitively.
Quick start
using System.Diagnostics;
using TracingAssertions;
using var capture = SpanCapture.ForSource("MyCompany.MyService");
using (var span = MyActivitySource.StartActivity("order.created"))
{
await Assert.That(span).HasOperationName("order.created");
}
The full reference is in the GitHub README.
License
MIT. No runtime dependencies beyond the BCL.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- TracingAssertions (>= 0.2.0)
- TUnit.Assertions (>= 1.54.0)
- TUnit.Core (>= 1.54.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
View the rendered release notes: https://github.com/JohnVerheij/TracingAssertions.TUnit/releases/tag/v0.2.0
Minor release. Adds span and capture assertions for ASP.NET Core span testing, plus a sampling-control capture overload. Purely additive.
### Added
- **Span assertions:** `Assert.That(span).HasKind(ActivityKind)` (for example `Server` for an inbound request span), `IsRoot()` (no parent), `HasEvent(name)`, and `HasExceptionEvent()` (the OpenTelemetry `"exception"` event). The event assertions list the span's event names on failure.
- **Capture assertions:** `Assert.That(capture).HasNoSpan(operationName)` (the inverse of `HasSpan`, for asserting an operation was never traced) and `HasSpanCount(int)`.
- **`SpanCapture.ForSource(name, ActivitySamplingResult)`** and **`ForSources(ActivitySamplingResult, params string[])`** capture under a chosen sampling result. The existing parameterless forms still force `AllDataAndRecorded`. A lower result (for example `PropagationData`) lets a test exercise code that branches on `Activity.IsAllDataRequested`, with the documented caveat that an activity's effective sampling is the maximum across all active listeners, so this takes effect only when the capture is the sole listener.
### Changed
- Bumped `PackageValidationBaselineVersion` from `0.1.0` to `0.1.2` on both packages so ApiCompat strict-mode validates `0.2.0` against the most recently published baseline.