-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add documentation about the TestContext #43420
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this. It would be ideal if you can add the longer code snippets to a project so it's automatically verified to compile.
|
||
The `TestContext` class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the `Microsoft.VisualStudio.TestTools.UnitTesting` namespace and is available when using the MSTest Framework. | ||
|
||
You can learn about [accessing the `TestContext` object](./unit-testing-mstest-writing-tests-testcontext.md#accessing-the-testcontext-object) or [the `TestContext` members](./unit-testing-mstest-writing-tests-testcontext.md#the-testcontext-members). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can learn about [accessing the `TestContext` object](./unit-testing-mstest-writing-tests-testcontext.md#accessing-the-testcontext-object) or [the `TestContext` members](./unit-testing-mstest-writing-tests-testcontext.md#the-testcontext-members). | |
For more information, see [Accessing the `TestContext` object](./unit-testing-mstest-writing-tests-testcontext.md#accessing-the-testcontext-object) or [The `TestContext` members](./unit-testing-mstest-writing-tests-testcontext.md#the-testcontext-members). |
@@ -47,6 +47,12 @@ MSTest assertions are divided into the following classes: | |||
- [The `StringAssert` class](./unit-testing-mstest-writing-tests-assertions.md#the-stringassert-class) | |||
- [The `CollectionAssert` class](./unit-testing-mstest-writing-tests-assertions.md#the-collectionassert-class) | |||
|
|||
## The `TestContext` class | |||
|
|||
The `TestContext` class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the `Microsoft.VisualStudio.TestTools.UnitTesting` namespace and is available when using the MSTest Framework. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `TestContext` class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the `Microsoft.VisualStudio.TestTools.UnitTesting` namespace and is available when using the MSTest Framework. | |
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace and is available when using the MSTest Framework. |
|
||
# The `TestContext` class | ||
|
||
The `TestContext` class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the `Microsoft.VisualStudio.TestTools.UnitTesting` namespace and is available when using the MSTest Framework. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `TestContext` class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the `Microsoft.VisualStudio.TestTools.UnitTesting` namespace and is available when using the MSTest Framework. | |
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> class provides contextual information and support for test execution, making it easier to retrieve information about the test run and manipulate aspects of the environment. It's defined in the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace and is available when using the MSTest Framework. |
|
||
## Accessing the `TestContext` object | ||
|
||
The `TestContext` object is available in the following contexts: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `TestContext` object is available in the following contexts: | |
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> object is available in the following contexts: |
} | ||
``` | ||
|
||
or with MSTest 3.6+ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or with MSTest 3.6+ | |
Or with MSTest 3.6+: |
|
||
- `TestData` – the data that will be provided to the parameterized test method or `null` if the test is not parameterized. | ||
- `TestDisplayName` - the display name of the test method. | ||
- `TestException` - the exception thrown by either the test method or test initialize or `null` if the test method did not throw an exception. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `TestException` - the exception thrown by either the test method or test initialize or `null` if the test method did not throw an exception. | |
- `TestException` - the exception thrown by either the test method or test initialize, or `null` if the test method did not throw an exception. |
|
||
`TestContext` is essential for data-driven testing in MSTest. It enables you to retrieve and set data for each iteration in a data-driven test, using properties like `DataRow` and `DataConnection` (for datasource based tests). | ||
|
||
Assuming the following CSV file `TestData.csv`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming the following CSV file `TestData.csv`: | |
Consider the following CSV file `TestData.csv`: |
|
||
In MSTest 3.7 and later, the property `TestData` can be used to access the data for the current test during `TestInitialize` and `TestCleanup` methods. | ||
|
||
### Storing and Retrieving Runtime Data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Storing and Retrieving Runtime Data | |
### Store and retrieve runtime data |
string value = TestContext.Properties["MyKey"]?.ToString(); | ||
``` | ||
|
||
### Associating data to a test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Associating data to a test | |
### Associate data to a test |
|
||
### Associating data to a test | ||
|
||
The `TestContext.AddResultFile` method allows you to add a file to the test results, making it available for review in the test output. This can be useful if you generate files during your test (e.g., log files, screenshots, or data files) that you want to attach to the test results. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `TestContext.AddResultFile` method allows you to add a file to the test results, making it available for review in the test output. This can be useful if you generate files during your test (e.g., log files, screenshots, or data files) that you want to attach to the test results. | |
The `TestContext.AddResultFile` method allows you to add a file to the test results, making it available for review in the test output. This can be useful if you generate files during your test (for example, log files, screenshots, or data files) that you want to attach to the test results. |
Fixes microsoft/testfx#4040
Internal previews
TestContext
class