Table of Contents

Class SpanAssertions

Namespace
TracingAssertions.TUnit
Assembly
TracingAssertions.TUnit.dll

TUnit-native fluent assertions over Activity spans (typically captured by SpanCapture): operation name, tag existence and value, status, and single-hop parent/child and same-trace relationships.

public static class SpanAssertions
Inheritance
SpanAssertions
Inherited Members

Remarks

Source methods carry the [GenerateAssertion] attribute; TUnit's source generator emits the fluent Assert.That(activity).<Method>() entry point at consumer build time. The generated chain is AOT-clean (no runtime reflection in the assertion path). Tag values are compared by their invariant ToString() form, matching how spans carry heterogeneously-typed tag values.

Methods

HasEvent(Activity, string)

Asserts that span carries an ActivityEvent named name (ordinal).

[GenerateAssertion]
public static AssertionResult HasEvent(this Activity span, string name)

Parameters

span Activity

The captured span.

name string

The event name expected to be present.

Returns

AssertionResult

A passing assertion when an event with that name is present; otherwise a failing assertion listing the event names the span carries.

Exceptions

ArgumentNullException

span or name is null.

HasExceptionEvent(Activity)

Asserts that span carries the OpenTelemetry exception event (an ActivityEvent named "exception", as recorded by AddException(Exception, in TagList, DateTimeOffset) or an OpenTelemetry instrumentation).

[GenerateAssertion]
public static AssertionResult HasExceptionEvent(this Activity span)

Parameters

span Activity

The captured span.

Returns

AssertionResult

A passing assertion when an exception event is present; otherwise a failing assertion listing the event names the span carries.

Exceptions

ArgumentNullException

span is null.

HasKind(Activity, ActivityKind)

Asserts that span has Kind equal to kind (for example Server for an inbound request span or Client for an outbound call).

[GenerateAssertion]
public static AssertionResult HasKind(this Activity span, ActivityKind kind)

Parameters

span Activity

The captured span.

kind ActivityKind

The expected activity kind.

Returns

AssertionResult

A passing assertion when the kind matches; otherwise a failing assertion naming the expected and observed kind.

Exceptions

ArgumentNullException

span is null.

HasOperationName(Activity, string)

Asserts that span has OperationName equal to operationName (ordinal).

[GenerateAssertion]
public static AssertionResult HasOperationName(this Activity span, string operationName)

Parameters

span Activity

The captured span, as the receiver of the fluent assertion.

operationName string

The operation name the span is expected to carry.

Returns

AssertionResult

A passing assertion when the operation name matches; otherwise a failing assertion naming the expected and observed operation names.

Exceptions

ArgumentNullException

span or operationName is null.

HasStatus(Activity, ActivityStatusCode)

Asserts that span has Status equal to status.

[GenerateAssertion]
public static AssertionResult HasStatus(this Activity span, ActivityStatusCode status)

Parameters

span Activity

The captured span.

status ActivityStatusCode

The expected status code.

Returns

AssertionResult

A passing assertion when the status matches; otherwise a failing assertion naming the expected and observed status.

Exceptions

ArgumentNullException

span is null.

HasTag(Activity, string)

Asserts that span carries a tag key with a non-null value.

[GenerateAssertion]
public static AssertionResult HasTag(this Activity span, string key)

Parameters

span Activity

The captured span.

key string

The tag key expected to be present.

Returns

AssertionResult

A passing assertion when the tag is present; otherwise a failing assertion.

Exceptions

ArgumentNullException

span or key is null.

HasTagValue(Activity, string, object)

Asserts that span carries a tag key whose value matches value (compared by invariant ToString()).

[GenerateAssertion]
public static AssertionResult HasTagValue(this Activity span, string key, object value)

Parameters

span Activity

The captured span.

key string

The tag key to inspect.

value object

The expected tag value.

Returns

AssertionResult

A passing assertion when the tag value matches; otherwise a failing assertion naming the expected and observed values, or reporting the tag as absent.

Exceptions

ArgumentNullException

span, key, or value is null.

IsChildOf(Activity, Activity)

Asserts that span is a direct child of parent: its ParentSpanId equals parent's SpanId and it shares the same TraceId.

[GenerateAssertion]
public static AssertionResult IsChildOf(this Activity span, Activity parent)

Parameters

span Activity

The captured span.

parent Activity

The expected parent span.

Returns

AssertionResult

A passing assertion when the parent/trace relationship holds; otherwise a failing assertion naming the expected and observed parent span and trace.

Exceptions

ArgumentNullException

span or parent is null.

IsRoot(Activity)

Asserts that span is a root span: it has no parent, so its ParentSpanId is the default (all-zero) span id. A span created under a propagated remote parent carries that parent's span id and is not a root.

[GenerateAssertion]
public static AssertionResult IsRoot(this Activity span)

Parameters

span Activity

The captured span.

Returns

AssertionResult

A passing assertion when the span is a root; otherwise a failing assertion naming the observed parent span.

Exceptions

ArgumentNullException

span is null.

SharesTraceWith(Activity, Activity)

Asserts that span shares the same TraceId as other (context propagation across a boundary).

[GenerateAssertion]
public static AssertionResult SharesTraceWith(this Activity span, Activity other)

Parameters

span Activity

The captured span.

other Activity

The span expected to be in the same trace.

Returns

AssertionResult

A passing assertion when the traces match; otherwise a failing assertion naming the expected and observed trace.

Exceptions

ArgumentNullException

span or other is null.