Class ProtoExecutionContext
Provides the public per-test facade 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)
Creates the execution context for a test from an already parsed id.
public ProtoExecutionContext(string testName, IServiceScope scope, ProtoTestId id, MethodInfo testMethod)
Parameters
testNamestringThe test's display name in the trace.
scopeIServiceScopeThe service scope the test resolves its services from.
idProtoTestIdThe test's numeric id.
testMethodMethodInfoThe test method the context belongs to.
ProtoExecutionContext(string, IServiceScope, string, MethodInfo)
Creates the execution context for a test, parsing testId with
Parse(string).
public ProtoExecutionContext(string testName, IServiceScope scope, string testId, MethodInfo testMethod)
Parameters
testNamestringThe test's display name in the trace.
scopeIServiceScopeThe service scope the test resolves its services from.
testIdstringThe test's numeric id, 1 to 18 decimal digits.
testMethodMethodInfoThe test method the context belongs to.
Properties
Attachments
Gets a snapshot of the artifacts registered for this test.
public IReadOnlyList<ProtoTestAttachment> Attachments { get; }
Property Value
CancellationToken
Gets the token the test lifecycle observes, so a test that honours cancellation ends at the same point the runner stopped it. Setup I/O that can observe it passes it to the provider.
public CancellationToken CancellationToken { get; }
Property Value
Clock
Gets this test's clock. Time-dependent code reads it, and the test advances it instead of sleeping; the application under test receives it as its TimeProvider.
public ProtoClock Clock { get; }
Property Value
Configuration
Retrieves the global configuration registered in the ProtoHost.
public IConfiguration Configuration { get; }
Property Value
Id
Numeric execution ID generated by the owning host. The string form preserves leading zeroes.
public ProtoTestId Id { get; }
Property Value
RecordedObservations
Gets a snapshot of observations recorded for this test.
public IReadOnlyCollection<ProtoObservation> RecordedObservations { get; }
Property Value
Resources
Gets a snapshot of the resources owned by this test, in registration order.
public IReadOnlyList<ProtoResourceSnapshot> Resources { get; }
Property Value
Services
Gets the service scope this test resolves from, so the scope closes with the test.
public IServiceProvider Services { get; }
Property Value
TestId
Gets Id as the string the trace and reports key on.
public string TestId { get; }
Property Value
TestMethod
Gets the test method this context belongs to.
public MethodInfo TestMethod { get; }
Property Value
TestName
Gets the test's display name as shown in the trace.
public string TestName { get; }
Property Value
TestNumber
Gets Id as the number a test id preserves; 0 is not a test.
public long TestNumber { get; }
Property Value
Trace
Records automatic execution diagnostics for ProtoTest integrations.
public IProtoTraceWriter Trace { get; }
Property Value
Methods
AddAttachment(ProtoTestAttachment)
Registers a prepared artifact and records it on the trace.
public ProtoTestAttachment AddAttachment(ProtoTestAttachment attachment)
Parameters
attachmentProtoTestAttachment
Returns
AddAttachment(string, ReadOnlyMemory<byte>, string, string?)
Registers a binary artifact and records it on the trace.
public ProtoTestAttachment AddAttachment(string name, ReadOnlyMemory<byte> content, string mediaType = "application/octet-stream", string? description = null)
Parameters
namestringcontentReadOnlyMemory<byte>mediaTypestringdescriptionstring
Returns
AddAttachment(string, string, string, string?)
Registers a text artifact and records it on the trace.
public ProtoTestAttachment AddAttachment(string name, string content, string mediaType = "text/plain", string? description = null)
Parameters
Returns
AddAttachmentFile(string, string?, string, string?)
Registers a file as an artifact; the file is captured with the test's artifacts.
public ProtoTestAttachment AddAttachmentFile(string filePath, string? name = null, string mediaType = "application/octet-stream", string? description = null)
Parameters
Returns
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
messagestringstatusProtoReportStatusidentifierstringcategorystringtargetNamestringtagsIReadOnlyList<string>metadataIReadOnlyDictionary<string, object>
Returns
Client<TClient>(string)
Retrieves a required named client.
public TClient Client<TClient>(string name = "Default") where TClient : class
Parameters
namestring
Returns
- TClient
Type Parameters
TClient
DisposeAsync()
Releases owned resources, disposes owned clients and the service scope. A second call waits for the first and does not release a resource again. An early release still running is awaited, with no timeout, before the service scope is disposed. A release that already failed is not retried; that failure is still included in the aggregated cleanup outcome.
public ValueTask DisposeAsync()
Returns
Exceptions
- InvalidOperationException
A release callback disposed its own context.
RecordObservation(ProtoObservation)
Stores and dispatches an observation to every collector that accepts it.
public void RecordObservation(ProtoObservation observation)
Parameters
observationProtoObservation
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
targetNamestringkindstringidentifierstringdataobjectmetadataIReadOnlyDictionary<string, object>
RegisterClient<TClient>(TClient, string, ProtoClientOwnership)
Registers a named client for this test. A Context client is released once when the test completes, however many names point at it; a Caller client is shared and its lifetime is managed elsewhere. The same instance under the same type and name (the name is case-insensitive) is a no-op when the ownership matches. Another name, or the same instance registered as another type, is an alias and does not add a release.
public void RegisterClient<TClient>(TClient client, string name = "Default", ProtoClientOwnership ownership = ProtoClientOwnership.Context) where TClient : class
Parameters
clientTClientnamestringownershipProtoClientOwnership
Type Parameters
TClient
Exceptions
- InvalidOperationException
A different instance is already registered under the type and name, or this instance is already registered with a different ProtoClientOwnership.
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
resourceIProtoResource
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
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
resourceT
Returns
- T
Type Parameters
T
ReleaseResourceAsync(string)
Releases one owned resource before teardown. Returns false when the id is unknown or a release for it has already started. A release this call runs that fails is recorded as ReleaseFailed and then thrown; dispose does not retry it, but that failure still counts in the test's cleanup outcome.
public ValueTask<bool> ReleaseResourceAsync(string id)
Parameters
idstring
Returns
Exceptions
- InvalidOperationException
The release callback for
idcalled this method for the same resource.
ReplaceClient<TClient>(TClient, string, ProtoClientOwnership)
Replaces a named client registered for this test and traces the replacement. The instance that lost its last name keeps the one release from when it was first registered. An instance another name still references is not released again, and a replacement that is already registered does not gain a second release. Replacing a name with the instance already registered there is a no-op when the ownership matches.
public void ReplaceClient<TClient>(TClient client, string name = "Default", ProtoClientOwnership ownership = ProtoClientOwnership.Context) where TClient : class
Parameters
clientTClientnamestringownershipProtoClientOwnership
Type Parameters
TClient
Exceptions
- InvalidOperationException
Nothing is registered under
name, orclientis already registered with a different ProtoClientOwnership. A replacement always follows a registration; it never creates one.
Resolve<T>()
Retrieves required contextual state.
public T Resolve<T>() where T : class, IProtoContext
Returns
- T
Type Parameters
T
Resolve<T>(string)
Retrieves required contextual state registered under a key.
public T Resolve<T>(string key) where T : class, IProtoContext
Parameters
keystring
Returns
- T
Type Parameters
T
Service<T>()
Resolves a required service from this test's scope.
public T Service<T>() where T : notnull
Returns
- T
Type Parameters
T
SetContext<T>(string, T)
Registers or replaces contextual state under a key, so two protocols can store the same state type without overwriting each other's instance.
public void SetContext<T>(string key, T context) where T : class, IProtoContext
Parameters
keystringcontextT
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
contextT
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
namestring
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
TryResolve<T>(string)
Retrieves contextual state registered under a key when it is present.
public T? TryResolve<T>(string key) where T : class, IProtoContext
Parameters
keystring
Returns
- T
Type Parameters
T
TryService<T>()
Resolves a service from this test's scope when it is registered.
public T? TryService<T>()
Returns
- T
Type Parameters
T
UniqueName(string, int)
Creates a name for a record that must outlive the test process, such as a tenant, an operator,
an account or a customer. The name derives from TestId, so parallel tests in one
host never collide. Across runs or processes, give each a distinct
RunPrefix through ConfigureTestIds; the default prefix is
random per run. A rerun gets the same name only with an IProtoTestIdGenerator that
derives the id from the test. Pass sequence when one test creates several
records of the same kind; the default (0) omits it.
public string UniqueName(string name, int sequence = 0)
Parameters
Returns
Examples
var tenant = context.UniqueName("tenant"); // "tenant-0042317"
var second = context.UniqueName("member", 2); // "member-0042317-2"