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
MSTEST0012 and MSTEST0016 contradict each other. One wants the containing class to be static, the other wants a non-static class.
MSTEST0012
The class shouldn't be static.
MSTEST0016
A test class should have at least one test method or be static and have methods that are attributed with [AssemblyInitialization] or [AssemblyCleanup].
Can I satisfy both rules or is this a bug?
Steps To Reproduce
Copy this basic example into a new cs-file
The methods AssemblyInitialize and AssemblyCleanup are within a base class which other normal test classes derive from. Naturally, there are no tests within that base class. I don't know why MSTEST0012 wants me to mark a base class with [TestClass], but I'm okay with that.
using Microsoft.VisualStudio.TestTools.UnitTesting;[TestClass]publicclassBaseClass{publicTestContextTestContext{get;set;}=null!;[AssemblyInitialize]publicstaticvoidAssemblyInitialize(TestContexttestContext){}[AssemblyCleanup]publicstaticvoidAssemblyCleanup(){}[TestInitialize]publicvoidTestInitialize(){}[TestCleanup]publicvoidTestCleanup(){}}
Expected behavior
No analyzer suggestions or warnings
Actual behavior
Code Analyzer suggestion MSTEST0016 is displayed
Additional context
This also affects MSTEST0013 (AssemblyCleanup).
The text was updated successfully, but these errors were encountered:
Thanks for ticket! There are indeed multiple things that are wrong on our side.
The documentation for MSTEST0012 and MSTEST0013 are incorrect (it's valid to have static class for [AssemblyInitialize] and [AssemblyCleanup] methods). The analyzers themselves are fine.
The documentation for MSTEST0016 should be improved to say that the class should be either abstract, have some test or be static (if it contains only [AssemblyInitialize] and/or [AssemblyCleanup] methods). We will also need to update the analyzer description.
There is nothing mandatory but I would probably recommend to have a different class for the AssemblyInitialize/AssemblyCleanup methods as the code is only executed once which could be confusing for some people when they see the base class.
Assuming this base class is really meant to have no tests, I would recommend to mark the class has abstract as it gives a clearer intent. You should not mark the class as static otherwise TestContext, TestInitialize and TestCleanup would not work (@engyebrahim could you double check if we would be reporting on these cases?).
Describe the bug
MSTEST0012 and MSTEST0016 contradict each other. One wants the containing class to be static, the other wants a non-static class.
MSTEST0012
MSTEST0016
Can I satisfy both rules or is this a bug?
Steps To Reproduce
The methods
AssemblyInitialize
andAssemblyCleanup
are within a base class which other normal test classes derive from. Naturally, there are no tests within that base class. I don't know why MSTEST0012 wants me to mark a base class with[TestClass]
, but I'm okay with that.Expected behavior
No analyzer suggestions or warnings
Actual behavior
Code Analyzer suggestion MSTEST0016 is displayed
Additional context
This also affects MSTEST0013 (AssemblyCleanup).
The text was updated successfully, but these errors were encountered: