Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Copy link
Contributor

@gewarren gewarren left a 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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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+
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `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`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Storing and Retrieving Runtime Data
### Store and retrieve runtime data

string value = TestContext.Properties["MyKey"]?.ToString();
```

### Associating data to a test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document TestContext property
3 participants