How to tell mstest to ignore tests in the base class, but not in subclasses?

I have a log analyzer that I'm working on, and this log parser has an ILogStore interface that defines the basic methods for any mechanism for storing log entries (in memory, in the database, etc.). The idea is that developers and users can add or remove log storage mechanisms through the MEF plugin interface.

However, to confirm that the ILogStore implementation can correctly store, filter, and retrieve log entries, I created a base class for unit / integration / API testing:

 public class LogStoreBaseTests { protected ILogStore _store; [TestMethod] public void Can_Store_And_Retrieve_Records() { } [TestMethod] public void Can_Filter_Records_By_Inclusive_Text() { } [TestMethod] public void Can_Filter_Records_By_Exclusive_Text() { } // etc... } 

I am testing my implemented tasks by doing something like:

 [TestClass] public class InMemoryLogStoreTests : LogStoreBaseTests { [TestInitialize] public void Setup() { _store = new InMemoryLogStore(); } } 

This works well, except that MsTest notices that the methods in the base class have [TestMethod] , but errors because the class does not have [TestClass] , which does not happen because it is not a valid test on it.

How can I tell MsTest to ignore methods when they are not executed from a subclass?

+4
source share
3 answers

Turns off MSTest has an attribute [Ignore] . I put this and the [TestClass] attribute in my base test and correctly ignore the test methods for the basic tests, using the basic test methods when run under a subclass

+13
source

Not sure if you can tell the MS test to ignore some tests, but you can ask it not to run certain categories of tests.

for example, the following attribute places the test in the integration category

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class IntegrationTestAttribute : TestCategoryBaseAttribute { public IList<string> categories; public IntegrationTestAttribute() { this.categories = new List<String> { "Integration" }; } public override IList<string> TestCategories { get { return this.categories; } } } 

Another approach you could take is to mark your base class as abstract and make your tests call certain abstract methods, so anyone who implements your class will implement these methods.

eg

 [TestClass] public abstract class BaseUnitTest { public BaseUnitTest(){} private TestContext testContextInstance; public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } [TestMethod] public void can_run_this_test_for_each_derived_class() { Assert.IsNotNull(this.ReturnMeSomething()); } protected abstract string ReturnMeSomething(); } [TestClass] public class Derived1 : BaseUnitTest { protected override string ReturnMeSomething() { return "test1"; } } [TestClass] public class Derived2 : BaseUnitTest { protected override string ReturnMeSomething() { return null; } } 

another approach is to use AOP, for example MSTestExtension, the code can be found here

+2
source

I have never seen such an approach when one is testing a device that inherits the other ... I would suggest considering a different approach rather than working on such a test infrastructure.

Consider either:

  • Move all tests in one class marked with [TestClass] OR
  • Create two completely separate tests so that they are not related to each other and do not affect each other.
0
source

Source: https://habr.com/ru/post/1382319/


All Articles