'SuppressMessage' for a whole namespace
Solution 1:
Suppression of a code analysis warning for a namespace and all its descendant symbols is possible since Visual Studio 2019:
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
Justification = "Test methods require underscores for readability."
Scope = "namespaceanddescendants", Target = "Company.Product.Tests")]
Scope - The target on which the warning is being suppressed. If the target is not specified, it is set to the target of the attribute. Supported scopes include the following:
...
namespaceanddescendants
- (New for Visual Studio 2019) This scope suppresses warnings in a namespace and all its descendant symbols. Thenamespaceanddescendants
value is only valid for Roslyn analyzers, and is ignored by binary, FxCop-based static analysis.
Suppress code analysis warnings#SuppressMessage attribute @ MS Docs
Solution 2:
You can use the "module" scope for this, which is supported by older compilers than the ones that support the newer "namespaceanddescendants" scope. The module scope impacts everything in the project, and it does not require a target specification.
Example Usage:
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
Justification = "Test methods require underscores for readability."
Scope = "module")]
Solution 3:
I have managed to ignore that particular warning specifically for Test projects (I follow a naming convention in which these always end up with "Tests.cs") by using a .editorconfig file, containing the following rule:
[*Tests.cs]
dotnet_diagnostics.CA1707.severity = none
More information on my answer here