-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Improve analyzers docs #51209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Evangelink
wants to merge
4
commits into
main
Choose a base branch
from
dev/amauryleve/mstest-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+500
−81
Open
Improve analyzers docs #51209
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,20 +8,65 @@ ms.date: 01/03/2024 | |
|
|
||
| # MSTest design rules | ||
|
|
||
| Design rules will help you create and maintain test suites that adhere to proper design and good practices. | ||
|
|
||
| Identifier | Name | Description | ||
| -----------|------|------------ | ||
| [MSTEST0004](mstest0004.md) | PublicTypeShouldBeTestClassAnalyzer | It's considered a good practice to have only test classes marked public in a test project. | ||
| [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. | ||
| [MSTEST0015](mstest0015.md) | TestMethodShouldNotBeIgnored | Test methods should not be ignored (marked with `[Ignore]`). | ||
| [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]`. | ||
| [MSTEST0019](mstest0019.md) | PreferTestInitializeOverConstructorAnalyzer | Prefer TestInitialize methods over constructors | ||
| [MSTEST0020](mstest0020.md) | PreferConstructorOverTestInitializeAnalyzer | Prefer constructors over TestInitialize methods | ||
| [MSTEST0021](mstest0021.md) | PreferDisposeOverTestCleanupAnalyzer | Prefer Dispose over TestCleanup methods | ||
| [MSTEST0022](mstest0022.md) | PreferTestCleanupOverDisposeAnalyzer | Prefer TestCleanup over Dispose methods | ||
| [MSTEST0025](mstest0025.md) | PreferAssertFailOverAlwaysFalseConditionsAnalyzer | Use 'Assert.Fail' instead of an always-failing assert | ||
| [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. | ||
| [MSTEST0036](mstest0036.md) | DoNotUseShadowingAnalyzer | Shadowing test members could cause testing issues (such as NRE). | ||
| [MSTEST0044](mstest0044.md) | PreferTestMethodOverDataTestMethodAnalyzer | A method or type uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.DataTestMethodAttribute> or inherits from it. | ||
| [MSTEST0045](mstest0045.md) | UseCooperativeCancellationForTimeoutAnalyzer | A test method uses <xref:Microsoft.VisualStudio.TestTools.UnitTesting.TimeoutAttribute> without setting the `CooperativeCancellation` property to `true`. | ||
| 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. | ||
|
|
||
| ## Rules in this category | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In all of the articles, tables and lists are missing periods.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot please address review comment |
||
|
|
||
| | Rule ID | Title | Severity | Fix Available | | ||
| |---------|-------|----------|---------------| | ||
| | [MSTEST0004](mstest0004.md) | Public types should be test classes. | Info | Yes | | ||
| | [MSTEST0006](mstest0006.md) | Avoid ExpectedException attribute. | Info | Yes | | ||
| | [MSTEST0015](mstest0015.md) | Test method should not be ignored. | None (opt-in) | No | | ||
| | [MSTEST0016](mstest0016.md) | Test class should have test method. | Info | No | | ||
| | [MSTEST0019](mstest0019.md) | Prefer TestInitialize over constructors. | None (opt-in) | Yes | | ||
| | [MSTEST0020](mstest0020.md) | Prefer constructors over TestInitialize. | None (opt-in) | Yes | | ||
| | [MSTEST0021](mstest0021.md) | Prefer Dispose over TestCleanup. | None (opt-in) | Yes | | ||
| | [MSTEST0022](mstest0022.md) | Prefer TestCleanup over Dispose. | None (opt-in) | Yes | | ||
| | [MSTEST0025](mstest0025.md) | Prefer Assert.Fail over always-false conditions. | Info | Yes | | ||
| | [MSTEST0029](mstest0029.md) | Public method should be test method. | Info | Yes | | ||
| | [MSTEST0036](mstest0036.md) | Do not use shadowing. | Warning | No | | ||
| | [MSTEST0044](mstest0044.md) | Prefer TestMethod over DataTestMethod. | Info | Yes | | ||
| | [MSTEST0045](mstest0045.md) | Use cooperative cancellation for timeout. | Info | Yes | | ||
|
|
||
| ## Common scenarios | ||
|
|
||
| ### Test class structure | ||
|
|
||
| When creating test classes, these rules help ensure proper design: | ||
|
|
||
| - **[MSTEST0004](mstest0004.md)**: Keep helper classes internal, only test classes should be public. | ||
| - **[MSTEST0016](mstest0016.md)**: Ensure test classes contain at least one test method. | ||
| - **[MSTEST0029](mstest0029.md)**: Public methods in test classes should be test methods. | ||
|
|
||
| ### Initialization patterns | ||
|
|
||
| MSTest supports both constructors and TestInitialize methods. These mutually exclusive rules let you enforce a consistent pattern: | ||
|
|
||
| - **[MSTEST0019](mstest0019.md)**: Enforce TestInitialize for initialization (useful for async scenarios). | ||
| - **[MSTEST0020](mstest0020.md)**: Enforce constructors for initialization (better for readonly fields). | ||
|
|
||
| ### Cleanup patterns | ||
|
|
||
| Similarly, choose between Dispose and TestCleanup: | ||
|
|
||
| - **[MSTEST0021](mstest0021.md)**: Enforce Dispose pattern for cleanup. | ||
| - **[MSTEST0022](mstest0022.md)**: Enforce TestCleanup for cleanup. | ||
|
|
||
| ### Better assertions | ||
|
|
||
| - **[MSTEST0006](mstest0006.md)**: Use Assert.ThrowsExactly instead of [ExpectedException] for better precision. | ||
| - **[MSTEST0025](mstest0025.md)**: Use Assert.Fail instead of Assert.IsTrue(false). | ||
|
|
||
| ### Test quality | ||
|
|
||
| - **[MSTEST0015](mstest0015.md)**: Flag ignored tests (opt-in rule). | ||
| - **[MSTEST0036](mstest0036.md)**: Avoid shadowing base class members. | ||
| - **[MSTEST0044](mstest0044.md)**: Use TestMethod unless data-driven testing is needed. | ||
| - **[MSTEST0045](mstest0045.md)**: Enable cancellation tokens for timeout handling. | ||
|
|
||
| ## Related documentation | ||
|
|
||
| - [Write tests with MSTest](../unit-testing-mstest-writing-tests.md) | ||
| - [Lifecycle](../unit-testing-mstest-writing-tests-lifecycle.md) | ||
| - [Attributes](../unit-testing-mstest-writing-tests-attributes.md) | ||
| - [Assertions](../unit-testing-mstest-writing-tests-assertions.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.