Skip to content

Commit 313893c

Browse files
committed
Address review comments
1 parent a19dced commit 313893c

File tree

5 files changed

+113
-107
lines changed

5 files changed

+113
-107
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
[TestClass]
4+
public class MyTestClass
5+
{
6+
[TestMethod]
7+
public void TestMethodWithResultFile()
8+
{
9+
// Simulate creating a log file for this test
10+
string logFilePath = Path.Combine(TestContext.TestRunDirectory, "TestLog.txt");
11+
File.WriteAllText(logFilePath, "This is a sample log entry for the test.");
12+
13+
// Add the log file to the test result
14+
TestContext.AddResultFile(logFilePath);
15+
16+
// Perform some assertions (example only)
17+
Assert.IsTrue(File.Exists(logFilePath), "The log file was not created.");
18+
Assert.IsTrue(new FileInfo(logFilePath).Length > 0, "The log file is empty.");
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
4+
namespace YourNamespace
5+
{
6+
[TestClass]
7+
public class CsvDataDrivenTest
8+
{
9+
public TestContext TestContext { get; set; }
10+
11+
[TestMethod]
12+
[DataSource(
13+
"Microsoft.VisualStudio.TestTools.DataSource.CSV",
14+
"|DataDirectory|\\TestData.csv",
15+
"TestData#csv",
16+
DataAccessMethod.Sequential)]
17+
public void TestWithCsvDataSource()
18+
{
19+
// Access data from the current row
20+
int number = Convert.ToInt32(TestContext.DataRow["Number"]);
21+
string name = TestContext.DataRow["Name"].ToString();
22+
23+
Console.WriteLine($"Number: {number}, Name: {name}");
24+
25+
// Example assertions or logic
26+
Assert.IsTrue(number > 0);
27+
Assert.IsFalse(string.IsNullOrEmpty(name));
28+
}
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
[TestClass]
4+
public class MyTestClass
5+
{
6+
public TestContext TestContext { get; set; }
7+
8+
[AssemblyInitialize]
9+
public static void AssemblyInitialize(TestContext context)
10+
{
11+
// Access TestContext properties and methods here. The properties related to the test run are not available.
12+
}
13+
14+
[ClassInitialize]
15+
public static void ClassInitialize(TestContext context)
16+
{
17+
// Access TestContext properties and methods here. The properties related to the test run are not available.
18+
}
19+
20+
[TestMethod]
21+
public void MyTestMethod()
22+
{
23+
// Access TestContext properties and methods here
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
[TestClass]
4+
public class MyTestClass
5+
{
6+
private readonly TestContext _testContext;
7+
8+
public MyTestClass(TestContext testContext)
9+
{
10+
_testContext = testContext;
11+
}
12+
13+
[AssemblyInitialize]
14+
public static void AssemblyInitialize(TestContext context)
15+
{
16+
// Access TestContext properties and methods here. The properties related to the test run are not available.
17+
}
18+
19+
[ClassInitialize]
20+
public static void ClassInitialize(TestContext context)
21+
{
22+
// Access TestContext properties and methods here. The properties related to the test run are not available.
23+
}
24+
25+
[TestMethod]
26+
public void MyTestMethod()
27+
{
28+
// Access TestContext properties and methods here
29+
}
30+
}

docs/core/testing/unit-testing-mstest-writing-tests-testcontext.md

Lines changed: 8 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.date: 11/12/2024
88

99
# The `TestContext` class
1010

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.
1212

1313
## Accessing the `TestContext` object
1414

@@ -18,64 +18,11 @@ The <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext> object is av
1818
- As a property of a test class. In this context, the properties related to the test run are available.
1919
- 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.
2020

21-
```csharp
22-
[TestClass]
23-
public class MyTestClass
24-
{
25-
public TestContext TestContext { get; set; }
26-
27-
[AssemblyInitialize]
28-
public static void AssemblyInitialize(TestContext context)
29-
{
30-
// Access TestContext properties and methods here. The properties related to the test run are not available.
31-
}
32-
33-
[ClassInitialize]
34-
public static void ClassInitialize(TestContext context)
35-
{
36-
// Access TestContext properties and methods here. The properties related to the test run are not available.
37-
}
38-
39-
[TestMethod]
40-
public void MyTestMethod()
41-
{
42-
// Access TestContext properties and methods here
43-
}
44-
}
45-
```
21+
:::code language="csharp" source="snippets/testcontext/csharp/TestContext.cs":::
4622

4723
Or with MSTest 3.6+:
4824

49-
```csharp
50-
[TestClass]
51-
public class MyTestClass
52-
{
53-
private readonly TestContext _testContext;
54-
55-
public MyTestClass(TestContext testContext)
56-
{
57-
_testContext = testContext;
58-
}
59-
60-
[AssemblyInitialize]
61-
public static void AssemblyInitialize(TestContext context)
62-
{
63-
// Access TestContext properties and methods here. The properties related to the test run are not available.
64-
}
65-
66-
[ClassInitialize]
67-
public static void ClassInitialize(TestContext context)
68-
{
69-
// Access TestContext properties and methods here. The properties related to the test run are not available.
70-
}
71-
72-
[TestMethod]
73-
public void MyTestMethod()
74-
{
75-
// Access TestContext properties and methods here
76-
}
77-
}
78-
```
25+
:::code language="csharp" source="snippets/testcontext/csharp/TestContextCtor.cs":::
7926

8027
## The `TestContext` members
8128

@@ -102,7 +49,9 @@ In MSTest 3.7 and later, the `TestContext` class also provides new properties he
10249

10350
### Data-driven tests
10451

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).
10655

10756
Consider the following CSV file `TestData.csv`:
10857

@@ -115,40 +64,7 @@ Number,Name
11564

11665
You can use the `DataSource` attribute to read the data from the CSV file:
11766

118-
```csharp
119-
using Microsoft.VisualStudio.TestTools.UnitTesting;
120-
using System;
121-
122-
namespace YourNamespace
123-
{
124-
[TestClass]
125-
public class CsvDataDrivenTest
126-
{
127-
public TestContext TestContext { get; set; }
128-
129-
[TestMethod]
130-
[DataSource(
131-
"Microsoft.VisualStudio.TestTools.DataSource.CSV",
132-
"|DataDirectory|\\TestData.csv",
133-
"TestData#csv",
134-
DataAccessMethod.Sequential)]
135-
public void TestWithCsvDataSource()
136-
{
137-
// Access data from the current row
138-
int number = Convert.ToInt32(TestContext.DataRow["Number"]);
139-
string name = TestContext.DataRow["Name"].ToString();
140-
141-
Console.WriteLine($"Number: {number}, Name: {name}");
142-
143-
// Example assertions or logic
144-
Assert.IsTrue(number > 0);
145-
Assert.IsFalse(string.IsNullOrEmpty(name));
146-
}
147-
}
148-
}
149-
```
150-
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.
67+
:::code language="csharp" source="snippets/testcontext/csharp/CsvDataSource.cs":::
15268

15369
### Store and retrieve runtime data
15470

@@ -163,21 +79,6 @@ string value = TestContext.Properties["MyKey"]?.ToString();
16379

16480
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.
16581

166-
```csharp
167-
[TestMethod]
168-
public void TestMethodWithResultFile()
169-
{
170-
// Simulate creating a log file for this test
171-
string logFilePath = Path.Combine(TestContext.TestRunDirectory, "TestLog.txt");
172-
File.WriteAllText(logFilePath, "This is a sample log entry for the test.");
173-
174-
// Add the log file to the test result
175-
TestContext.AddResultFile(logFilePath);
176-
177-
// Perform some assertions (example only)
178-
Assert.IsTrue(File.Exists(logFilePath), "The log file was not created.");
179-
Assert.IsTrue(new FileInfo(logFilePath).Length > 0, "The log file is empty.");
180-
}
181-
```
82+
:::code language="csharp" source="snippets/testcontext/csharp/AddResultFile.cs":::
18283

18384
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

Comments
 (0)