You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/testing/unit-testing-mstest-writing-tests-testcontext.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,11 +8,11 @@ ms.date: 11/12/2024
8
8
9
9
# The `TestContext` class
10
10
11
-
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.
11
+
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.
12
12
13
13
## Accessing the `TestContext` object
14
14
15
-
The `TestContext` object is available in the following contexts:
15
+
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> object is available in the following contexts:
16
16
17
17
- As a parameter to `[AssemblyInitialize]` and `[ClassInitialize]` methods. In this context, the properties related to the test run are not available.
18
18
- As a property of a test class. In this context, the properties related to the test run are available.
@@ -44,7 +44,7 @@ public class MyTestClass
44
44
}
45
45
```
46
46
47
-
or with MSTest 3.6+
47
+
Or with MSTest 3.6+:
48
48
49
49
```csharp
50
50
[TestClass]
@@ -79,7 +79,7 @@ public class MyTestClass
79
79
80
80
## The `TestContext` members
81
81
82
-
The `TestContext` class provides properties about the test run along with methods to manipulate the test environment. In this section, we will cover the most commonly used properties and methods.
82
+
The `TestContext` class provides properties about the test run along with methods to manipulate the test environment. This section covers the most commonly used properties and methods.
83
83
84
84
### Test run information
85
85
@@ -98,13 +98,13 @@ In MSTest 3.7 and later, the `TestContext` class also provides new properties he
98
98
99
99
-`TestData` – the data that will be provided to the parameterized test method or `null` if the test is not parameterized.
100
100
-`TestDisplayName` - the display name of the test method.
101
-
-`TestException` - the exception thrown by either the test method or test initialize or `null` if the test method did not throw an exception.
101
+
-`TestException` - the exception thrown by either the test method or test initialize, or `null` if the test method did not throw an exception.
102
102
103
103
### Data-driven tests
104
104
105
105
`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).
106
106
107
-
Assuming the following CSV file `TestData.csv`:
107
+
Consider the following CSV file `TestData.csv`:
108
108
109
109
```csv
110
110
Number,Name
@@ -150,7 +150,7 @@ namespace YourNamespace
150
150
151
151
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.
152
152
153
-
### Storing and Retrieving Runtime Data
153
+
### Store and retrieve runtime data
154
154
155
155
You can use `TestContext.Properties` to store custom key-value pairs that can be accessed across different methods in the same test session.
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.
164
+
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.
Copy file name to clipboardExpand all lines: docs/core/testing/unit-testing-mstest-writing-tests.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,9 +49,9 @@ MSTest assertions are divided into the following classes:
49
49
50
50
## The `TestContext` class
51
51
52
-
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.
52
+
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.
53
53
54
-
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).
54
+
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).
0 commit comments