Class TestContext
Provide the context information of the current test. This is an adapter for the internal ExecutionContext class, hiding the internals from the user test.
Inherited Members
Namespace: NUnit.Framework
Assembly: nunit.framework.dll
Syntax
public class TestContext
Constructors
View SourceTestContext(TestExecutionContext)
Construct a TestContext for an ExecutionContext
Declaration
public TestContext(TestExecutionContext testExecutionContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TestExecutionContext | testExecutionContext | The ExecutionContext to adapt |
Fields
View SourceError
Gets a TextWriter that will send output directly to Console.Error
Declaration
public static TextWriter Error
Field Value
| Type | Description |
|---|---|
| TextWriter |
Parameters
TestParameters object holds parameters for the test run, if any are specified
Declaration
public static readonly TestParameters Parameters
Field Value
| Type | Description |
|---|---|
| TestParameters |
Progress
Gets a TextWriter for use in displaying immediate progress messages
Declaration
public static readonly TextWriter Progress
Field Value
| Type | Description |
|---|---|
| TextWriter |
Properties
View SourceAssertCount
Gets the number of assertions executed up to this point in the test.
Declaration
public int AssertCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
CancellationToken
Gets the CancellationToken for the test case.
Declaration
public CancellationToken CancellationToken { get; }
Property Value
| Type | Description |
|---|---|
| CancellationToken |
CurrentContext
Get the current test context. This is created as needed. The user may save the context for use within a test, but it should not be used outside the test for which it is created.
Declaration
public static TestContext CurrentContext { get; }
Property Value
| Type | Description |
|---|---|
| TestContext |
CurrentRepeatCount
Get the number of times the current Test has been repeated when using the RetryAttribute or RepeatAttribute.
Declaration
public int CurrentRepeatCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Out
Gets a TextWriter that will send output to the current test result.
Declaration
public static TextWriter Out { get; }
Property Value
| Type | Description |
|---|---|
| TextWriter |
Random
Gets the random generator.
Declaration
public Randomizer Random { get; }
Property Value
| Type | Description |
|---|---|
| Randomizer | The random generator. |
Result
Gets a Representation of the TestResult for the current test.
Declaration
public TestContext.ResultAdapter Result { get; }
Property Value
| Type | Description |
|---|---|
| TestContext.ResultAdapter |
Test
Get a representation of the current test.
Declaration
public TestContext.TestAdapter Test { get; }
Property Value
| Type | Description |
|---|---|
| TestContext.TestAdapter |
TestDirectory
Gets the directory containing the current test assembly.
Declaration
public string TestDirectory { get; }
Property Value
| Type | Description |
|---|---|
| string |
WorkDirectory
Gets the directory to be used for outputting files created by this test run.
Declaration
public string WorkDirectory { get; }
Property Value
| Type | Description |
|---|---|
| string |
WorkerId
Gets the unique name of the Worker that is executing this test.
Declaration
public string? WorkerId { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
View SourceAddFormatter(ValueFormatterFactory)
This method adds a new ValueFormatterFactory to the chain of responsibility used for formatting values in messages. The scope of the change is the current TestContext.
Declaration
public static void AddFormatter(ValueFormatterFactory formatterFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueFormatterFactory | formatterFactory | The factory delegate |
AddFormatter<TSupported>(ValueFormatter)
This method provides a simplified way to add a ValueFormatter delegate to the chain of responsibility, creating the factory delegate internally. It is useful when the Type of the object is the only criterion for selection of the formatter, since it can be used without getting involved with a compound function.
Declaration
public static void AddFormatter<TSupported>(ValueFormatter formatter)
Parameters
| Type | Name | Description |
|---|---|---|
| ValueFormatter | formatter | The ValueFormatter delegate |
Type Parameters
| Name | Description |
|---|---|
| TSupported | The type supported by this formatter |
AddTestAttachment(string, string?)
Attach a file to the current test result
Declaration
public static void AddTestAttachment(string filePath, string? description = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filePath | Relative or absolute file path to attachment |
| string | description | Optional description of attachment |
Write(bool)
Write the string representation of a boolean value to the current result
Declaration
public static void Write(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value |
Write(char)
Write a char to the current result
Declaration
public static void Write(char value)
Parameters
| Type | Name | Description |
|---|---|---|
| char | value |
Write(char[]?)
Write a char array to the current result
Declaration
public static void Write(char[]? value)
Parameters
| Type | Name | Description |
|---|---|---|
| char[] | value |
Write(decimal)
Write the string representation of a decimal value to the current result
Declaration
public static void Write(decimal value)
Parameters
| Type | Name | Description |
|---|---|---|
| decimal | value |
Write(double)
Write the string representation of a double to the current result
Declaration
public static void Write(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value |
Write(int)
Write the string representation of an Int32 value to the current result
Declaration
public static void Write(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value |
Write(long)
Write the string representation of an Int64 value to the current result
Declaration
public static void Write(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value |
Write(object?)
Write the string representation of an object to the current result
Declaration
public static void Write(object? value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | value |
Write(float)
Write the string representation of a Single value to the current result
Declaration
public static void Write(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value |
Write(string?)
Write a string to the current result
Declaration
public static void Write(string? value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value |
Write(string, object?)
Write a formatted string to the current result
Declaration
public static void Write(string format, object? arg1)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 |
Write(string, object?, object?)
Write a formatted string to the current result
Declaration
public static void Write(string format, object? arg1, object? arg2)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 | |
| object | arg2 |
Write(string, object?, object?, object?)
Write a formatted string to the current result
Declaration
public static void Write(string format, object? arg1, object? arg2, object? arg3)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 | |
| object | arg2 | |
| object | arg3 |
Write(string, params object?[])
Write a formatted string to the current result
Declaration
public static void Write(string format, params object?[] args)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object[] | args |
Write(uint)
Write the string representation of a UInt32 value to the current result
Declaration
[CLSCompliant(false)]
public static void Write(uint value)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | value |
Write(ulong)
Write the string representation of a UInt64 value to the current result
Declaration
[CLSCompliant(false)]
public static void Write(ulong value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | value |
WriteLine()
Write a line terminator to the current result
Declaration
public static void WriteLine()
WriteLine(bool)
Write the string representation of a boolean value to the current result followed by a line terminator
Declaration
public static void WriteLine(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value |
WriteLine(char)
Write a char to the current result followed by a line terminator
Declaration
public static void WriteLine(char value)
Parameters
| Type | Name | Description |
|---|---|---|
| char | value |
WriteLine(char[]?)
Write a char array to the current result followed by a line terminator
Declaration
public static void WriteLine(char[]? value)
Parameters
| Type | Name | Description |
|---|---|---|
| char[] | value |
WriteLine(decimal)
Write the string representation of a decimal value to the current result followed by a line terminator
Declaration
public static void WriteLine(decimal value)
Parameters
| Type | Name | Description |
|---|---|---|
| decimal | value |
WriteLine(double)
Write the string representation of a double to the current result followed by a line terminator
Declaration
public static void WriteLine(double value)
Parameters
| Type | Name | Description |
|---|---|---|
| double | value |
WriteLine(int)
Write the string representation of an Int32 value to the current result followed by a line terminator
Declaration
public static void WriteLine(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value |
WriteLine(long)
Write the string representation of an Int64 value to the current result followed by a line terminator
Declaration
public static void WriteLine(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value |
WriteLine(object?)
Write the string representation of an object to the current result followed by a line terminator
Declaration
public static void WriteLine(object? value)
Parameters
| Type | Name | Description |
|---|---|---|
| object | value |
WriteLine(float)
Write the string representation of a Single value to the current result followed by a line terminator
Declaration
public static void WriteLine(float value)
Parameters
| Type | Name | Description |
|---|---|---|
| float | value |
WriteLine(string?)
Write a string to the current result followed by a line terminator
Declaration
public static void WriteLine(string? value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value |
WriteLine(string, object?)
Write a formatted string to the current result followed by a line terminator
Declaration
public static void WriteLine(string format, object? arg1)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 |
WriteLine(string, object?, object?)
Write a formatted string to the current result followed by a line terminator
Declaration
public static void WriteLine(string format, object? arg1, object? arg2)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 | |
| object | arg2 |
WriteLine(string, object?, object?, object?)
Write a formatted string to the current result followed by a line terminator
Declaration
public static void WriteLine(string format, object? arg1, object? arg2, object? arg3)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object | arg1 | |
| object | arg2 | |
| object | arg3 |
WriteLine(string, params object?[])
Write a formatted string to the current result followed by a line terminator
Declaration
public static void WriteLine(string format, params object?[] args)
Parameters
| Type | Name | Description |
|---|---|---|
| string | format | |
| object[] | args |
WriteLine(uint)
Write the string representation of a UInt32 value to the current result followed by a line terminator
Declaration
[CLSCompliant(false)]
public static void WriteLine(uint value)
Parameters
| Type | Name | Description |
|---|---|---|
| uint | value |
WriteLine(ulong)
Write the string representation of a UInt64 value to the current result followed by a line terminator
Declaration
[CLSCompliant(false)]
public static void WriteLine(ulong value)
Parameters
| Type | Name | Description |
|---|---|---|
| ulong | value |