Lajos
Lajos

Reputation: 2827

Shall I create Unit test base

Does it make sense of if I create a base test class of the same tests? What is the scenario in this case. For example I have almost identical viewmodels: XReport, YReport etc. and I create a base test:

public abstract  class ReportTestBase
{
    public T UC_ReportUserControl_Create<T>() where  T : class, IViewModel
    {
        return NinjectService.Get<T>();
    }
}

then I create derivatives:

[TestClass]
public class PsoriasisReport : ReportTestBase
{
    [TestMethod]
    public void UC_PsoriasisReportUserControl_Create()
    {
        Assert.IsNotNull(UC_ReportUserControl_Create<IPsoriasisReportUserControl>());
    }
}

These pieces of code are just samples. I am interested in methodology.

Upvotes: 1

Views: 732

Answers (2)

pav
pav

Reputation: 480

If you have some commonality in your [Initialize] ([SetUp] in NUnit) methods, subclassing might be useful for test classes where you need the same set up over and over.

Beware the order that these are called in MSTest.

Otherwise, I would create a hepler class as Pragmateek suggests.

Upvotes: 4

Pragmateek
Pragmateek

Reputation: 13396

You could do that but as noticed by StuffHappens inheritance is maybe too much.

Can't you simply create a "TestsCommon" class with static helper methods to factorize all the redundant code?

I've already used this approach and it works well without making the code too obscur.

Upvotes: 1

Related Questions