|
| 1 | +--- |
| 2 | +title: MSTest TestContext |
| 3 | +description: Learn about the TestContext class of MSTest. |
| 4 | +author: Evangelink |
| 5 | +ms.author: amauryleve |
| 6 | +ms.date: 11/12/2024 |
| 7 | +--- |
| 8 | + |
| 9 | +# The `TestContext` class |
| 10 | + |
| 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 | + |
| 13 | +## Accessing the `TestContext` object |
| 14 | + |
| 15 | +The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> object is available in the following contexts: |
| 16 | + |
| 17 | +- As a parameter to `[AssemblyInitialize]` and `[ClassInitialize]` methods. In this context, the properties related to the test run are not available. |
| 18 | +- As a property of a test class. In this context, the properties related to the test run are available. |
| 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. |
| 20 | + |
| 21 | +:::code language="csharp" source="snippets/testcontext/csharp/TestContext.cs"::: |
| 22 | + |
| 23 | +Or with MSTest 3.6+: |
| 24 | + |
| 25 | +:::code language="csharp" source="snippets/testcontext/csharp/TestContextCtor.cs"::: |
| 26 | + |
| 27 | +## The `TestContext` members |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +### Test run information |
| 32 | + |
| 33 | +The `TestContext` provides information about the test run, such as: |
| 34 | + |
| 35 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestName> – the name of the currently executing test. |
| 36 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.CurrentTestOutcome> - the result of the current test. |
| 37 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.FullyQualifiedTestClassName> - the full name of the test class. |
| 38 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestRunDirectory> - the directory where the test run is executed. |
| 39 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DeploymentDirectory> - the directory where the deployment items are located. |
| 40 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.ResultsDirectory> - the directory where the test results are stored. Typically a subdirectory of the `TestRunDirectory`. |
| 41 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestRunResultsDirectory> - the directory where the test results are stored. Typically a subdirectory of the `ResultsDirectory`. |
| 42 | +- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.TestResultsDirectory> - the directory where the test results are stored. Typically a subdirectory of the `ResultsDirectory`. |
| 43 | + |
| 44 | +In MSTest 3.7 and later, the `TestContext` class also provides new properties helpful for `TestInitialize` and `TestCleanup` methods: |
| 45 | + |
| 46 | +- `TestContext.TestData` - the data that will be provided to the parameterized test method or `null` if the test is not parameterized. |
| 47 | +- `TestContext.TestDisplayName` - the display name of the test method. |
| 48 | +- `TestContext.TestException` - the exception thrown by either the test method or test initialize, or `null` if the test method did not throw an exception. |
| 49 | + |
| 50 | +### Data-driven tests |
| 51 | + |
| 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](xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute)-based tests). |
| 55 | + |
| 56 | +Consider the following CSV file `TestData.csv`: |
| 57 | + |
| 58 | +```csv |
| 59 | +Number,Name |
| 60 | +1,TestValue1 |
| 61 | +2,TestValue2 |
| 62 | +3,TestValue3 |
| 63 | +``` |
| 64 | + |
| 65 | +You can use the `DataSource` attribute to read the data from the CSV file: |
| 66 | + |
| 67 | +:::code language="csharp" source="snippets/testcontext/csharp/CsvDataSource.cs"::: |
| 68 | + |
| 69 | +### Store and retrieve runtime data |
| 70 | + |
| 71 | +You can use <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Properties?displayProperty=nameWithType> to store custom key-value pairs that can be accessed across different methods in the same test session. |
| 72 | + |
| 73 | +```csharp |
| 74 | +TestContext.Properties["MyKey"] = "MyValue"; |
| 75 | +string value = TestContext.Properties["MyKey"]?.ToString(); |
| 76 | +``` |
| 77 | + |
| 78 | +### Associate data to a test |
| 79 | + |
| 80 | +The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.AddResultFile(System.String)?displayProperty=nameWithType> 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. |
| 81 | + |
| 82 | +:::code language="csharp" source="snippets/testcontext/csharp/AddResultFile.cs"::: |
| 83 | + |
| 84 | +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