Reputation: 561
I have a base class with Unit Tests, and I have two subclasses that are initialized with different data that need to be known when constructing the class.
Sample code for the setup would be this:
[TestClass]
public class BaseClass
{
public BaseClass(object param){ }
[TestMethod]
public BaseTest() { /* Do Stuff */ }
}
[TestClass]
public class SubClass1 : BaseClass
{
public SubClass1() : base("data1") {}
}
[TestClass]
public class SubClass2 : BaseClass
{
public SubClass2() : base("data2") {}
}
Ideally I would have wanted two unit tests to show, SubClass1.BaseTest() and SubClass2.BaseTest() being able to be run. However, I only see BaseClass.BaseTest. If I make BaseClass abstract, the Test Explorer does not show any tests at all.
The base test does different stuff depending on the data passed during construction of the class, however they have the same code. Can I avoid having to copy the tests? Can I avoid having to write a method for each test in the subclass that redirects to the base class?
Upvotes: 1
Views: 2653
Reputation: 561
While Matias answer in general seems good, I cannot use that approach in my current state. I found the solution however why the base tests did not show up in the Text Explorer. For Base tests to show up in the subclass in the Test Explorer, base and sub class need to be in the same project. I had them split up, which made it not work.
Upvotes: 1
Reputation: 64923
AFAIK, this is an unsupported scenario by Visual Studio Test (formerly MSTest).
Anyway, providing base test cases sounds more like a testing design flaw rather than an actual advantage.
A test class should provide concrete test methods to check that some tested class does the job in the right way.
You should really use a test base class to share common test helper methods, properties, and if you want to share some steps across many test cases, you should implement them in a static class TestAbbreviations
.
Test should keep as simple as possible and you should avoid overengineering in them. If you implement your test suite using KISS principle, it will also work as documentation of how to use your code.
Upvotes: 2