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
+8-107Lines changed: 8 additions & 107 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ ms.date: 11/12/2024
8
8
9
9
# The `TestContext` class
10
10
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.
11
+
The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> class gives useful information and tools to help manage test execution. It lets you access details about the test run and adjust the test environment. This class is part of the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace.
12
12
13
13
## Accessing the `TestContext` object
14
14
@@ -18,64 +18,11 @@ The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> object is av
18
18
- As a property of a test class. In this context, the properties related to the test run are available.
19
19
- As a constructor parameter of a test class (starting with v3.6). This way is recommended over using the property, because it gives access to the object in the constructor. While the property is only available after the constructor has run. This way also helps to ensure immutability of the object and allows the compiler to enforce that the object is not null.
@@ -102,7 +49,9 @@ In MSTest 3.7 and later, the `TestContext` class also provides new properties he
102
49
103
50
### Data-driven tests
104
51
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).
52
+
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.
53
+
54
+
When targeting .NET framework, the `TestContext` 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
55
107
56
Consider the following CSV file `TestData.csv`:
108
57
@@ -115,40 +64,7 @@ Number,Name
115
64
116
65
You can use the `DataSource` attribute to read the data from the CSV file:
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.
@@ -163,21 +79,6 @@ string value = TestContext.Properties["MyKey"]?.ToString();
163
79
164
80
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.
You can also use `TestContext.Write` or `TestContext.WriteLine` methods to write custom messages directly to the test output. This is especially useful for debugging purposes, as it provides real-time logging information within your test execution context.
0 commit comments