Reputation: 27803
I have a log parser that I am working on, and this log parser has an ILogStore
interface that defines the base methods for any log record storage engine (in memory, in a database, etc..). The idea is that developers and users can add or remove log storage engines via the MEF plugin interface.
However, in order to confirm that a 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 test my implemented tasks by doing something like:
[TestClass]
public class InMemoryLogStoreTests : LogStoreBaseTests
{
[TestInitialize]
public void Setup()
{
_store = new InMemoryLogStore();
}
}
This works good except that MsTest notices that the methods in the base class have [TestMethod]
but errors because the class doesn't have [TestClass]
, which it doesn't because it's not valid tests on it's own.
How can I tell MsTest to ignore the methods when not run from a subclass?
Upvotes: 4
Views: 7325
Reputation: 5815
Not sure if you can tell MS test to ignore certain tests, but you can ask it to not run certain test categories.
for example following attribute puts the test in 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 it to mark your base class as abstract and make your tests call certain abstract methods, that way anyone who is implementing your class will implement those methods.
for example
[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;
}
}
yet another approach would be to use AOP like MSTestExtension does , code can be found here
Upvotes: 2
Reputation: 27803
Turns out MSTest has an [Ignore]
Attribute. I placed that and the [TestClass]
attribute on my base test, and it correctly ignores the test methods for the base tests while using the base test methods when run under a subclass
Upvotes: 14
Reputation: 62504
Never saw such approach when one tests fixture inheriting an other one... I would suggest considering an other approach rather than working around such tests infrastructure.
Consider either:
[TestClass]
ORUpvotes: 0