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/mstest-analyzers/design-rules.md
+62-17Lines changed: 62 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,20 +8,65 @@ ms.date: 01/03/2024
8
8
9
9
# MSTest design rules
10
10
11
-
Design rules will help you create and maintain test suites that adhere to proper design and good practices.
12
-
13
-
Identifier | Name | Description
14
-
-----------|------|------------
15
-
[MSTEST0004](mstest0004.md) | PublicTypeShouldBeTestClassAnalyzer | It's considered a good practice to have only test classes marked public in a test project.
16
-
[MSTEST0006](mstest0006.md) | AvoidExpectedExceptionAttributeAnalyzer | Prefer `Assert.ThrowsExactly` or `Assert.ThrowsExactlyAsync` over `[ExpectedException]` as it ensures that only the expected call throws the expected exception. The assert APIs also provide more flexibility and allow you to assert extra properties of the exception.
17
-
[MSTEST0015](mstest0015.md) | TestMethodShouldNotBeIgnored | Test methods should not be ignored (marked with `[Ignore]`).
18
-
[MSTEST0016](mstest0016.md) | TestClassShouldHaveTestMethod | Test class should have at least one test method or be 'static' with method(s) marked by `[AssemblyInitialization]` and/or `[AssemblyCleanup]`.
19
-
[MSTEST0019](mstest0019.md) | PreferTestInitializeOverConstructorAnalyzer | Prefer TestInitialize methods over constructors
20
-
[MSTEST0020](mstest0020.md) | PreferConstructorOverTestInitializeAnalyzer | Prefer constructors over TestInitialize methods
21
-
[MSTEST0021](mstest0021.md) | PreferDisposeOverTestCleanupAnalyzer | Prefer Dispose over TestCleanup methods
22
-
[MSTEST0022](mstest0022.md) | PreferTestCleanupOverDisposeAnalyzer | Prefer TestCleanup over Dispose methods
23
-
[MSTEST0025](mstest0025.md) | PreferAssertFailOverAlwaysFalseConditionsAnalyzer | Use 'Assert.Fail' instead of an always-failing assert
24
-
[MSTEST0029](mstest0029.md) | PublicMethodShouldBeTestMethod | A `public` method of a class marked with `[TestClass]` should be a test method (marked with `[TestMethod]`). The rule ignores methods that are marked with `[TestInitialize]`, or `[TestCleanup]` attributes.
25
-
[MSTEST0036](mstest0036.md) | DoNotUseShadowingAnalyzer | Shadowing test members could cause testing issues (such as NRE).
26
-
[MSTEST0044](mstest0044.md) | PreferTestMethodOverDataTestMethodAnalyzer | A method or type uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> or inherits from it.
27
-
[MSTEST0045](mstest0045.md) | UseCooperativeCancellationForTimeoutAnalyzer | A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute> without setting the `CooperativeCancellation` property to `true`.
11
+
Design rules help you create and maintain test suites that adhere to proper design and good practices. These rules focus on test structure, best practices, and common patterns that lead to maintainable test code.
12
+
13
+
## Rules in this category
14
+
15
+
| Rule ID | Title | Severity | Fix Available |
16
+
|---------|-------|----------|---------------|
17
+
|[MSTEST0004](mstest0004.md)| Public types should be test classes. | Info | Yes |
18
+
|[MSTEST0006](mstest0006.md)| Avoid ExpectedException attribute. | Info | Yes |
19
+
|[MSTEST0015](mstest0015.md)| Test method should not be ignored. | None (opt-in) | No |
20
+
|[MSTEST0016](mstest0016.md)| Test class should have test method. | Info | No |
Related documentation: [Write tests with MSTest](../unit-testing-mstest-writing-tests.md), [Attributes](../unit-testing-mstest-writing-tests-attributes.md)
116
+
117
+
### Async/await patterns
118
+
119
+
Rules for writing asynchronous test code correctly:
120
+
121
+
-[MSTEST0013](mstest0013.md) - AssemblyCleanup should be valid (includes async rules)
122
+
-[MSTEST0027](mstest0027.md) - Suppress async suffix for test methods
123
+
-[MSTEST0028](mstest0028.md) - Suppress async suffix for test fixture methods
124
+
-[MSTEST0039](mstest0039.md) - Use newer Assert.Throws methods (async variants)
125
+
-[MSTEST0040](mstest0040.md) - Avoid using asserts in async void context
126
+
-[MSTEST0045](mstest0045.md) - Use cooperative cancellation for timeout
-[MSTEST0062](mstest0062.md) - Avoid out/ref test method parameters
142
+
143
+
Related documentation: [Attributes used for data-driven testing](../unit-testing-mstest-writing-tests-attributes.md#attributes-used-for-data-driven-testing)
144
+
145
+
### Lifecycle & initialization
146
+
147
+
Rules for test initialization, cleanup, and lifecycle management:
148
+
149
+
-[MSTEST0008](mstest0008.md) - TestInitialize should be valid
150
+
-[MSTEST0009](mstest0009.md) - TestCleanup should be valid
151
+
-[MSTEST0010](mstest0010.md) - ClassInitialize should be valid
152
+
-[MSTEST0011](mstest0011.md) - ClassCleanup should be valid
153
+
-[MSTEST0012](mstest0012.md) - AssemblyInitialize should be valid
154
+
-[MSTEST0013](mstest0013.md) - AssemblyCleanup should be valid
155
+
-[MSTEST0019](mstest0019.md) - Prefer TestInitialize over constructors
156
+
-[MSTEST0020](mstest0020.md) - Prefer constructors over TestInitialize
157
+
-[MSTEST0021](mstest0021.md) - Prefer Dispose over TestCleanup
158
+
-[MSTEST0022](mstest0022.md) - Prefer TestCleanup over Dispose
159
+
-[MSTEST0034](mstest0034.md) - Use ClassCleanupBehavior.EndOfClass
160
+
-[MSTEST0050](mstest0050.md) - Global test fixture should be valid
161
+
162
+
Related documentation: [Lifecycle](../unit-testing-mstest-writing-tests-lifecycle.md)
163
+
164
+
### Assertions
165
+
166
+
Rules for using assertion methods correctly and effectively:
\* Escalated to Error in `Recommended` and `All` modes.
281
+
282
+
## MSTESTEXP
92
283
93
284
Several APIs of MSTest are decorated with the <xref:System.Diagnostics.CodeAnalysis.ExperimentalAttribute>. This attribute indicates that the API is experimental and may be removed or changed in future versions of MSTest. The attribute is used to identify APIs that aren't yet stable and may not be suitable for production use.
0 commit comments