Class ProtoExecutionContext

Namespace
ProtoTest.Core
Assembly
ProtoTest.Core.dll

Provides the public per-test façade for metadata, services, state, clients, attachments, and observations.

public sealed class ProtoExecutionContext : IAsyncDisposable
Inheritance
ProtoExecutionContext
Implements
Inherited Members
Extension Methods

Constructors

ProtoExecutionContext(string, IServiceScope, ProtoTestId, MethodInfo)

public ProtoExecutionContext(string testName, IServiceScope scope, ProtoTestId id, MethodInfo testMethod)

Parameters

testName string
scope IServiceScope
id ProtoTestId
testMethod MethodInfo

ProtoExecutionContext(string, IServiceScope, string, MethodInfo)

public ProtoExecutionContext(string testName, IServiceScope scope, string testId, MethodInfo testMethod)

Parameters

testName string
scope IServiceScope
testId string
testMethod MethodInfo

Properties

Attachments

Gets a snapshot of the artifacts registered for this test.

public IReadOnlyList<ProtoTestAttachment> Attachments { get; }

Property Value

IReadOnlyList<ProtoTestAttachment>

Configuration

Retrieves the global configuration registered in the ProtoHost.

public IConfiguration Configuration { get; }

Property Value

IConfiguration

Id

Numeric execution ID generated by the owning host. The string form preserves leading zeroes.

public ProtoTestId Id { get; }

Property Value

ProtoTestId

RecordedObservations

Gets a snapshot of observations recorded for this test.

public IReadOnlyCollection<ProtoObservation> RecordedObservations { get; }

Property Value

IReadOnlyCollection<ProtoObservation>

Resources

Gets a snapshot of the resources owned by this test, in registration order.

public IReadOnlyList<ProtoResourceSnapshot> Resources { get; }

Property Value

IReadOnlyList<ProtoResourceSnapshot>

Services

public IServiceProvider Services { get; }

Property Value

IServiceProvider

TestId

public string TestId { get; }

Property Value

string

TestMethod

public MethodInfo TestMethod { get; }

Property Value

MethodInfo

TestName

public string TestName { get; }

Property Value

string

TestNumber

public long TestNumber { get; }

Property Value

long

Trace

Records automatic execution diagnostics for ProtoTest integrations.

public IProtoTraceWriter Trace { get; }

Property Value

IProtoTraceWriter

Methods

AddAttachment(ProtoTestAttachment)

public ProtoTestAttachment AddAttachment(ProtoTestAttachment attachment)

Parameters

attachment ProtoTestAttachment

Returns

ProtoTestAttachment

AddAttachment(string, ReadOnlyMemory<byte>, string, string?)

public ProtoTestAttachment AddAttachment(string name, ReadOnlyMemory<byte> content, string mediaType = "application/octet-stream", string? description = null)

Parameters

name string
content ReadOnlyMemory<byte>
mediaType string
description string

Returns

ProtoTestAttachment

AddAttachment(string, string, string, string?)

public ProtoTestAttachment AddAttachment(string name, string content, string mediaType = "text/plain", string? description = null)

Parameters

name string
content string
mediaType string
description string

Returns

ProtoTestAttachment

AddAttachmentFile(string, string?, string, string?)

public ProtoTestAttachment AddAttachmentFile(string filePath, string? name = null, string mediaType = "application/octet-stream", string? description = null)

Parameters

filePath string
name string
mediaType string
description string

Returns

ProtoTestAttachment

AddFinding(string, ProtoReportStatus, string?, string?, string?, IReadOnlyList<string>?, IReadOnlyDictionary<string, object>?)

Records a finding: something worth reporting that is deliberately not a test failure. Findings reach the run's reports and run gates, and are traced so they appear in ProtoTrace.

public ProtoReportItem AddFinding(string message, ProtoReportStatus status = ProtoReportStatus.Warning, string? identifier = null, string? category = null, string? targetName = null, IReadOnlyList<string>? tags = null, IReadOnlyDictionary<string, object>? metadata = null)

Parameters

message string
status ProtoReportStatus
identifier string
category string
targetName string
tags IReadOnlyList<string>
metadata IReadOnlyDictionary<string, object>

Returns

ProtoReportItem

Client<TClient>(string)

Retrieves a required named client.

public TClient Client<TClient>(string name = "Default") where TClient : class

Parameters

name string

Returns

TClient

Type Parameters

TClient

DisposeAsync()

Releases owned resources, disposes owned clients and the service scope. Disposal is idempotent and attempts every resource.

public ValueTask DisposeAsync()

Returns

ValueTask

RecordObservation(ProtoObservation)

Stores and dispatches an observation to every collector that accepts it.

public void RecordObservation(ProtoObservation observation)

Parameters

observation ProtoObservation

RecordObservation(string, string, string, object?, IReadOnlyDictionary<string, object>?)

Convenience overload for recording an observation.

public void RecordObservation(string targetName, string kind, string identifier, object? data = null, IReadOnlyDictionary<string, object>? metadata = null)

Parameters

targetName string
kind string
identifier string
data object
metadata IReadOnlyDictionary<string, object>

RegisterClient<TClient>(TClient, string, bool)

Registers a named client for this test. The client is owned by the test and released in reverse registration order when the test completes; pass disposeWithContext as false for shared clients whose lifetime is managed elsewhere.

public void RegisterClient<TClient>(TClient client, string name = "Default", bool disposeWithContext = true) where TClient : class

Parameters

client TClient
name string
disposeWithContext bool

Type Parameters

TClient

RegisterResource(IProtoResource)

Registers a resource owned by this test. Owned resources are released in reverse registration order during teardown, after hooks and attributes and before clients are disposed. Resources registered here reach the run's report; framework-managed clients do not, unless their release fails.

public void RegisterResource(IProtoResource resource)

Parameters

resource IProtoResource

RegisterResource(string, string, string, Func<ProtoResourceReleaseContext, ValueTask>)

Registers a resource described by a release callback.

public void RegisterResource(string id, string kind, string description, Func<ProtoResourceReleaseContext, ValueTask> release)

Parameters

id string
kind string
description string
release Func<ProtoResourceReleaseContext, ValueTask>

RegisterResource<T>(T)

Registers a resource and returns it, so ownership can be expressed inline.

public T RegisterResource<T>(T resource) where T : class, IProtoResource

Parameters

resource T

Returns

T

Type Parameters

T

ReleaseResourceAsync(string)

Releases one owned resource before teardown. Returns false when the resource is unknown or was already released.

public ValueTask<bool> ReleaseResourceAsync(string id)

Parameters

id string

Returns

ValueTask<bool>

Resolve<T>()

Retrieves required contextual state.

public T Resolve<T>() where T : class, IProtoContext

Returns

T

Type Parameters

T

Service<T>()

public T Service<T>() where T : notnull

Returns

T

Type Parameters

T

SetContext<T>(T)

Registers or replaces contextual state for this test.

public void SetContext<T>(T context) where T : class, IProtoContext

Parameters

context T

Type Parameters

T

TryClient<TClient>(string)

Retrieves a named client when it is present.

public TClient? TryClient<TClient>(string name = "Default") where TClient : class

Parameters

name string

Returns

TClient

Type Parameters

TClient

TryResolve<T>()

Retrieves contextual state when it is present.

public T? TryResolve<T>() where T : class, IProtoContext

Returns

T

Type Parameters

T

TryService<T>()

public T? TryService<T>()

Returns

T

Type Parameters

T