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:
#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.
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.