Table of Contents

Class TestContext

Namespace
Xunit
Assembly
xunit.v3.core.dll

Represents information about the current state of the test engine. It may be available at various points during the execution pipeline, so consumers must always take care to ensure that they check for null values from the various properties.

public sealed class TestContext : ITestContext, IDisposable
Inheritance
TestContext
Implements
Inherited Members
Extension Methods
Xunit.TestContextExtensions.GetFixture``1(Xunit.ITestContext)

Properties

Attachments

Gets the attachments for the current test, if the engine is currently in the process of running a test; will return null outside of the context of a test.

CancellationToken

Gets the cancellation token that is used to indicate that the test run should be aborted. Async tests should pass this along to any async functions that support cancellation tokens, to help speed up the cancellation process.

Current

Gets the current test context. If called outside of the test discovery or execution path, will return a test context that is in the Unknown stage. The current test context is a "snapshot in time" for when this property is called, so do not cache the instance across a single method boundary (or else you run the risk of having an out-of-date context).

KeyValueStorage

Stores key/value pairs that are available across all stages of the pipeline. Can be used to communicate between extensions at different execution stages, in both directions, as a single storage container is used for the entire pipeline.

PipelineStage

Gets the current test pipeline stage.

Test

Gets the current test, if the engine is currently in the process of running a test; will return null outside of the context of a test.

TestAssembly

Gets the current test assembly, if the engine is currently in the process of running or discovering tests in assembly; will return null out of this context (this typically means the test framework itself is being created and initialized).

TestAssemblyStatus

Gets the current test engine status for the test assembly.

TestCase

Gets the current test case, if the engine is currently in the process of running a test case; will return null outside of the context of a test case.

TestCaseStatus

Gets the current test engine status for the test case. Will only be available when TestCase is not null.

TestClass

Gets the current test method, if the engine is currently in the process of running a test class; will return null outside of the context of a test class. Note that not all test framework implementations require that tests be based on classes, so this value may be null even if TestCase is not null.

TestClassInstance

Gets the instance of the test class; will return null outside of the context of a test. Static test methods do not create test class instances, so this will always be null for static test methods.

TestClassStatus

Gets the current test engine status for the test class. Will only be available when TestClass is not null.

TestCollection

Gets the current test collection, if the engine is currently in the process of running a test collection; will return null outside of the context of a test collection.

TestCollectionStatus

Gets the current test engine status for the test collection. Will only be available when TestCollection is not null.

TestMethod

Gets the current test method, if the engine is currently in the process of running a test method; will return null outside of the context of a test method. Note that not all test framework implementations require that tests be based on methods, so this value may be null even if TestCase is not null.

TestMethodStatus

Gets the current test engine status for the test method. Will only be available when TestMethod is not null.

TestOutputHelper

Gets the output helper, which can be used to add output to the test. Will only be available when Test is not null. Note that the value may still be null when Test is not null, if the test framework implementation does not provide output helper support.

TestState

Gets the current state of the test. Will only be available after the test has finished running.

TestStatus

Gets the current test engine status for the test. Will only be available when Test is not null.

Warnings

Gets the set of warnings associated with the current test. Will only be available when Test is not null; will also return null if there have been no warnings issued.

Methods

AddAttachment(string, byte[], bool, string)

Adds an attachment that is a binary value (represented by a byte array and media type).

AddAttachment(string, byte[], string)

Adds an attachment that is a binary value (represented by a byte array and media type).

AddAttachment(string, string)

Adds an attachment that is a string value.

AddAttachment(string, string, bool)

Adds an attachment that is a string value.

AddWarning(string)

Adds a warning to the test result.

CancelCurrentTest()

Attempt to cancel the currently executing test, if one is executing. This will signal the CancellationToken for cancellation.

Dispose()
GetFixture(Type)

Gets a fixture that was attached to the test class. Will return null if there is no exact match for the requested fixture type, or if there is no test class (that is, if TestClass returns null).

SendDiagnosticMessage(string)

Sends a diagnostic message. Will only be visible if the end user has enabled diagnostic messages. See https://xunit.net/docs/configuration-files for configuration information.

SendDiagnosticMessage(string, object?)

Sends a formatted diagnostic message. Will only be visible if the end user has enabled diagnostic messages. See https://xunit.net/docs/configuration-files for configuration information.

SendDiagnosticMessage(string, object?, object?)

Sends a formatted diagnostic message. Will only be visible if the end user has enabled diagnostic messages. See https://xunit.net/docs/configuration-files for configuration information.

SendDiagnosticMessage(string, object?, object?, object?)

Sends a formatted diagnostic message. Will only be visible if the end user has enabled diagnostic messages. See https://xunit.net/docs/configuration-files for configuration information.

SendDiagnosticMessage(string, params object?[])

Sends a formatted diagnostic message. Will only be visible if the end user has enabled diagnostic messages. See https://xunit.net/docs/configuration-files for configuration information.

SetForInitialization(IMessageSink?, bool, bool)

Sets the test context for test framework initialization. This is the moment before any specific assembly is being discovered or run. This is typically used by custom runners just before they create the test framework via a call to GetTestFramework(Assembly).

SetForTest(ITest, TestEngineStatus, CancellationToken, TestResultState?, ITestOutputHelper?, object?)

Sets the test context for execution of a test. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.

SetForTestAssembly(ITestAssembly, TestEngineStatus, CancellationToken)

Sets the test context for discovery or execution of a test assembly. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.

SetForTestCase(ITestCase, TestEngineStatus, CancellationToken)

Sets the test context for execution of a test case. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.

SetForTestClass(ITestClass, TestEngineStatus, CancellationToken, FixtureMappingManager?)

Sets the test context for execution of a test class. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.

SetForTestCollection(ITestCollection, TestEngineStatus, CancellationToken)

Sets the test context for execution of a test collection. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.

SetForTestMethod(ITestMethod, TestEngineStatus, CancellationToken)

Sets the test context for execution of a test method. This assumes an existing test context already exists from which it can pull the diagnostic and internal diagnostic message sinks.