Skip to content

Commit cfc45e1

Browse files
authored
Merge pull request #8581 from MicrosoftDocs/main638018823463668660sync_temp
Repo sync for protected CLA branch
2 parents 43c63be + 53b26d8 commit cfc45e1

File tree

1 file changed

+178
-33
lines changed

1 file changed

+178
-33
lines changed

docs/test/using-microsoft-visualstudio-testtools-unittesting-members-in-unit-tests.md

Lines changed: 178 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Use MSTest in unit tests
33
description: Learn about the MSTest framework, which supports unit testing in Visual Studio. Use these classes and members to code unit tests.
44
ms.custom: SEO-VS-2020
5-
ms.date: 03/02/2018
5+
ms.date: 10/17/2022
66
ms.topic: reference
77
ms.author: mikejo
88
manager: jmartens
@@ -15,66 +15,211 @@ author: mikejo5000
1515

1616
[!INCLUDE [Visual Studio](~/includes/applies-to-version/vs-windows-only.md)]
1717

18-
The [MSTest](<xref:Microsoft.VisualStudio.TestTools.UnitTesting>) framework supports unit testing in Visual Studio. Use the classes and members in the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace when you are coding unit tests. You can also use them when you are refining a unit test that was generated from code.
18+
The [MSTest](https://github.com/microsoft/testfx) framework supports unit testing in Visual Studio. Use the classes and members in the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace when you're coding unit tests. You can also use them when you're refining a unit test that was generated from code.
1919

2020
## Framework members
2121

2222
To help provide a clearer overview of the unit testing framework, this section organizes the members of the <xref:Microsoft.VisualStudio.TestTools.UnitTesting> namespace into groups of related functionality.
2323

2424
> [!NOTE]
25-
> Attribute elements, whose names end with "Attribute", can be used either with or without "Attribute" on the end. For example, the following two code examples function identically:
25+
> Attribute elements, whose names end with "Attribute", can be used either with or without "Attribute" on the end and for parameterless constructors with or without parenthesis. For example, the following code examples function identically:
2626
>
2727
> `[TestClass()]`
2828
>
2929
> `[TestClassAttribute()]`
30+
>
31+
> `[TestClass]`
32+
>
33+
> `[TestClassAttribute]`
3034
31-
### Members used for data-driven testing
32-
33-
Use the following elements to set up data-driven unit tests. For more information, see [Create a data-driven unit test](../test/how-to-create-a-data-driven-unit-test.md) and [Use a configuration file to define a data source](../test/walkthrough-using-a-configuration-file-to-define-a-data-source.md).
34-
35-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod>
36-
37-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute>
38-
39-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceElement>
35+
### Attributes used to identify test classes and methods
4036

41-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceElementCollection>
37+
Every test class must have the `TestClass` attribute, and every test method must have the `TestMethod` attribute. For more information, see [Anatomy of a unit test](/previous-versions/ms182517(v=vs.110)).
4238

43-
## Attributes used to establish a calling order
39+
#### TestClassAttribute
4440

45-
A code element decorated with one of the following attributes is called at the moment you specify. For more information, see [Anatomy of a unit test](/previous-versions/ms182517(v=vs.110)).
41+
The [TestClass](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute>) attribute marks a class that contains tests and, optionally, initialize or cleanup methods.
4642

47-
### Attributes for assemblies
43+
This attribute can be extended to update or extend the behavior.
4844

49-
AssemblyInitialize and AssemblyCleanup are called right after your assembly is loaded and right before your assembly is unloaded.
45+
Example:
5046

51-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute>
47+
```csharp
48+
[TestClass]
49+
public class MyTestClass
50+
{
51+
}
52+
```
5253

53-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>
54+
#### TestMethodAttribute
5455

55-
### Attributes for classes
56+
The [TestMethod](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>) attribute is used inside a `TestClass` to define the actual test method to run.
5657

57-
ClassInitialize and ClassCleanup are called right after your class is loaded and right before your class is unloaded.
58+
The method should be an instance method defined as `public void` or `public Task` (optionally `async`) and be parameterless.
5859

59-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute>
60+
**Example**
6061

61-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute>
62+
```csharp
63+
[TestClass]
64+
public class MyTestClass
65+
{
66+
[TestMethod]
67+
public void TestMethod()
68+
{
69+
}
70+
}
71+
```
6272

63-
### Attributes for test methods
73+
```csharp
74+
[TestClass]
75+
public class MyTestClass
76+
{
77+
[TestMethod]
78+
public async Task TestMethod()
79+
{
80+
}
81+
}
82+
```
6483

65-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute>
84+
### Attributes used for data-driven testing
6685

67-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute>
86+
Use the following elements to set up data-driven unit tests. For more information, see [Create a data-driven unit test](../test/how-to-create-a-data-driven-unit-test.md) and [Use a configuration file to define a data source](../test/walkthrough-using-a-configuration-file-to-define-a-data-source.md).
6887

69-
## Attributes used to identify test classes and methods
88+
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute>
7089

71-
Every test class must have the `TestClass` attribute, and every test method must have the `TestMethod` attribute. For more information, see [Anatomy of a unit test](/previous-versions/ms182517(v=vs.110)).
90+
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataRowAttribute>
7291

73-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute>
92+
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DynamicDataAttribute>
7493

75-
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute>
94+
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataSourceAttribute>
7695

77-
## Assert classes and related exceptions
96+
### Attributes used to provide initialization and cleanups
97+
98+
A method decorated with one of the following attributes is called at the moment you specify. For more information, see [Anatomy of a unit test](/previous-versions/ms182517(v=vs.110)).
99+
100+
#### Assembly
101+
102+
[AssemblyInitialize](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitializeAttribute>) is called right after your assembly is loaded and [AssemblyCleanup](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyCleanupAttribute>) is called right before your assembly is unloaded.
103+
104+
The methods marked with these attributes should be defined as `static void` or `static Task`, in a `TestClass`, and appear only once. The initialize part requires one argument of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup no argument.
105+
106+
```csharp
107+
[TestClass]
108+
public class MyTestClass
109+
{
110+
[AssemblyInitialize]
111+
public static void AssemblyInitialize(TestContext testContext)
112+
{
113+
}
114+
115+
[AssemblyCleanup]
116+
public static void AssemblyCleanup()
117+
{
118+
}
119+
}
120+
```
121+
122+
```csharp
123+
[TestClass]
124+
public class MyOtherTestClass
125+
{
126+
[AssemblyInitialize]
127+
public static async Task AssemblyInitialize(TestContext testContext)
128+
{
129+
}
130+
131+
[AssemblyCleanup]
132+
public static async Task AssemblyCleanup()
133+
{
134+
}
135+
}
136+
```
137+
138+
#### Class
139+
140+
[ClassInitialize](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitializeAttribute>) is called right before your class is loaded (but after static constructor) and [ClassCleanup](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanupAttribute>) is called right after your class is unloaded.
141+
142+
It's possible to control the inheritance behavior: only for current class using `InheritanceBehavior.None` or for all derived classes using `InheritanceBehavior.BeforeEachDerivedClass`.
143+
144+
It's also possible to configure whether the class cleanup should be run at the end of the class or at the end of the assembly (current default).
145+
146+
The methods marked with these attributes should be defined as `static void` or `static Task`, in a `TestClass`, and appear only once. The initialize part requires one argument of type [TestContext](xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestContext) and the cleanup no argument.
147+
148+
```csharp
149+
[TestClass]
150+
public class MyTestClass
151+
{
152+
[ClassInitialize]
153+
public static void ClassInitialize(TestContext testContext)
154+
{
155+
}
156+
157+
[ClassCleanup]
158+
public static void ClassCleanup()
159+
{
160+
}
161+
}
162+
```
163+
164+
```csharp
165+
[TestClass]
166+
public class MyOtherTestClass
167+
{
168+
[ClassInitialize]
169+
public static async Task ClassInitialize(TestContext testContext)
170+
{
171+
}
172+
173+
[ClassCleanup]
174+
public static async Task ClassCleanup()
175+
{
176+
}
177+
}
178+
```
179+
180+
#### Test
181+
182+
[TestInitialize](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute>) is called right before your test is started and [TestCleanup](<xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute>) is called right after your test is finished.
183+
184+
The `TestInitialize` is similar to the class constructor but is usually more suitable for long or async initializations. The `TestInitialize` is always called after the constructor and called for each test (including each data row of data-driven tests).
185+
186+
The `TestCleanup` is similar to the class `Dispose` (or `DisposeAsync`) but is usually more suitable for long or async cleanups. The `TestCleanup` is always called just before the `DisposeAsync`/`Dispose` and called for each test (including each data row of data-driven tests).
187+
188+
The methods marked with these attributes should be defined as `void` or `Task`, in a `TestClass`, be parameterless, and appear one or multiple times.
189+
190+
```csharp
191+
[TestClass]
192+
public class MyTestClass
193+
{
194+
[TestInitialize]
195+
public void TestInitialize()
196+
{
197+
}
198+
199+
[TestCleanup]
200+
public void TestCleanup()
201+
{
202+
}
203+
}
204+
```
205+
206+
```csharp
207+
[TestClass]
208+
public class MyOtherTestClass
209+
{
210+
[TestInitialize]
211+
public async Task TestInitialize()
212+
{
213+
}
214+
215+
[TestCleanup]
216+
public async Task TestCleanup()
217+
{
218+
}
219+
}
220+
```
221+
222+
## Assertions and related exceptions
78223

79224
Unit tests can verify specific application behavior by their use of various kinds of assertions, exceptions, and attributes. For more information, see [Using the assert classes](../test/using-the-assert-classes.md).
80225

@@ -94,7 +239,7 @@ Unit tests can verify specific application behavior by their use of various kind
94239

95240
## The TestContext class
96241

97-
The following attributes and the values assigned to them appear in the Visual Studio Properties window for a particular test method. These attributes are not meant to be accessed through the code of the unit test. Instead, they affect the ways the unit test is used or run, either by you through the IDE of Visual Studio, or by the Visual Studio test engine. For example, some of these attributes appear as columns in the **Test Manager** window and **Test Results** window, which means that you can use them to group and sort tests and test results. One such attribute is <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute>, which you use to add arbitrary metadata to unit tests. For example, you could use it to store the name of a test pass that this test covers, by marking the unit test with `[TestProperty("TestPass", "Accessibility")]`. Or, you could use it to store an indicator of the kind of test it is with `[TestProperty("TestKind", "Localization")]`. The property you create by using this attribute, and the property value you assign, are both displayed in the Visual Studio **Properties** window under the heading **Test specific**.
242+
The following attributes and the values assigned to them appear in the Visual Studio Properties window for a particular test method. These attributes aren't meant to be accessed through the code of the unit test. Instead, they affect the ways the unit test is used or run, either by you through the IDE of Visual Studio, or by the Visual Studio test engine. For example, some of these attributes appear as columns in the **Test Manager** window and **Test Results** window, which means that you can use them to group and sort tests and test results. One such attribute is <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TestPropertyAttribute>, which you use to add arbitrary metadata to unit tests. For example, you could use it to store the name of a "test pass" that this test covers, by marking the unit test with `[TestProperty("TestPass", "Accessibility")]`. Or, you could use it to store an indicator of the kind of test It's with `[TestProperty("TestKind", "Localization")]`. The property you create by using this attribute, and the property value you assign, are both displayed in the Visual Studio **Properties** window under the heading **Test specific**.
98243

99244
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.OwnerAttribute>
100245

@@ -118,7 +263,7 @@ The following attributes and the values assigned to them appear in the Visual St
118263

119264
## Attributes used to generate reports
120265

121-
The attributes in this section relate the test method that they decorate to entities in the project hierarchy of a Team Foundation Server team project.
266+
The attributes in this section relate the test method that they decorate to entities in the project hierarchy of a `Team Foundation Server` team project.
122267

123268
- <xref:Microsoft.VisualStudio.TestTools.UnitTesting.CssIterationAttribute>
124269

0 commit comments

Comments
 (0)