I'm trying to disable a code analysis rule across an entire class, but NOT for the entire project, just a single class. In the example below, the build generates a CA1822 warning because it thinks that the unit test methods should be static.
The fix is to add the following attribute to each unit test method:
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
However, that's cumbersome and clutters a class with many unit tests.
I've tried:
- Moving the attribute to the class
- Wrapping all of the methods in
#pragma warning disable CA1822
#pragma warning restore CA1822
Neither of these two approaches have worked.
public class TestClass
{
public TestClass()
{
// some setup here
}
[Fact]
public void My_Unit_Test1()
{
// the 'this' parameter is never used, causes CA warning 1822
}
[Fact]
public void My_Unit_Test2()
{
// the 'this' parameter is never used, causes CA warning 1822
}
}
Using VS2015 Update 2, .net 4.61, and the new Code Analysis Analyzers.
This is not exactly what you want but might be a lesser evil than the situation you have.
You can tell code analysis to ignore a particular class with the following attribute:
Note that your project needs to have the Visual Studio option "Suppress results from generated code (managed only)" checked.
While annoying, you can still choose to run code analysis on the file if you temporarily comment the attribute.
Right click the error under the error list tab, and you can choose either 'In Source' and 'In Suppression File'.
SuppressMessageAttibute will be added to the source code(method or class level) if you select 'In Source'.
'[assembly:SUppressMessage' will be added to GlobalSupressions.cs file, and you can config the 'Target' of the attribute.
snapshot here