18

When creating a generic base test class in MSTest, and inheriting from it, I'm unable to run the tests of all the inheriting classes.

Unit test results

BaseDependencyPropertyFactoryTest is located in the Whathecode.PresentationFramework.Tests assembly. It is the generic base class. (BaseDependencyPropertyFactoryTest<TTestClass>)

Both assemblies have a test inheriting from this base class, called DependencyPropertyFactoryTest. All the inherited class does is passing a specific type argument.

[TestClass]
public class DependencyPropertyFactoryTest
    : BaseDependencyPropertyFactoryTest<ASpecificClass>
{
}

Only the inheriting test located in the same assembly as the base class seems to run. The inherited test in the Whathecode.PresentationFramework.Aspects.Tests assembly seems to be ignored entirely.

What am I doing wrong? When desired I could upload all the required source code, but you will need PostSharp for the aspects assembly.


As a test, I tried adding a test to the inherited test class in the aspects assembly, which calls all the tests in the base test class.

[TestMethod]
public void AllBaseTests()
{
    ClrGetterSetterTest();
    DependencyPropertyGetterSetterTest();
}

This gives the following result. Strangely enough this test is executed! For now this might work as a way to at least run them, but of course I don't want to edit this test each time I add extra tests in the base class.

Unit test results after edit

Why are those base tests skipped, and why the indication 'Aborted'?

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • 1
    FWIW, MSTest just sucks in so many ways that it's not even funny. AFAIR inherited tests classes are not supported... That's just one of many issues. – Mark Seemann May 14 '11 at 16:46
  • @Mark: What do you advise, should I give NUnit a try? I was quite pleased with MSTest up until this particular case, so if you'd say NUnit is definitely able to do this, I'll give it a try. – Steven Jeuris May 15 '11 at 00:02
  • Just found [this relevant bug report](https://connect.microsoft.com/VisualStudio/feedback/details/466528/visual-studio-test-mstest-and-lack-of-inheritance-support-for-base-classes-that-resides-in-different-assemblies) on Microsoft Connect. – Steven Jeuris May 15 '11 at 00:38
  • 1
    I'm very happy with xUnit.net: http://blog.ploeh.dk/2010/04/26/WhyImMigratingFromMSTestToXUnitnet.aspx while Roy Osherove seems to prefer NUnit: http://osherove.com/blog/2010/3/5/nunit-vs-mstest-nunit-wins-for-unit-testing.html – Mark Seemann May 15 '11 at 07:39

4 Answers4

13

The cause of this doesn't have to do with generics, but with the tests being in different assemblies.

A Microsoft Connect suggestion describes the problem: "Visual Studio Test (MSTest) and lack of Inheritance support for base classes that resides in different assemblies." It is marked as 'fixed', but doesn't seem to be fixed in Visual Studio 2010 yet, perhaps it still needs to be released?

There is one interesting workaround to this problem:

You can work around this problem by compiling the source file containing the base class into all test projects that wish to derive from that base class. Add the item as a "link" so that you don't end up with multiple copies of the source file for the base class.

This worked for me, and I don't find the workaround too ugly.

Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
  • 2
    VS 2013 still seems to be broken. Filed new bug https://connect.microsoft.com/VisualStudio/feedback/details/989781 – Nathan Oct 02 '14 at 19:25
  • 4
    VS 2015 Update 1 - still broken – Bohumil Janda Jan 29 '16 at 06:36
  • 1
    VS 2015 Update 3 - annnnnnnnnnnnd still broken – Mike Christensen Apr 19 '17 at 04:08
  • Curious if anyone has tried out the new [open sourced version](https://github.com/Microsoft/testfx) of MSTest to see if it has the same issue. If not, I'd be happy to try to fix the issue and submit a pull request (when I have some time).. This limitation is bugging me! – Mike Christensen Apr 19 '17 at 05:06
  • Gave MSTest2 a shot, and it still has the same issue. The good news is this is open source. I've [submitted a bug report](https://github.com/Microsoft/testfx/issues/158) and might take a stab at a fix. – Mike Christensen Apr 19 '17 at 05:30
  • 1
    Looks like there's [already a pull request](https://github.com/Microsoft/testfx/pull/147/files) out for this!!! Woohoo!! – Mike Christensen Apr 19 '17 at 05:39
2

Steven's answer of adding the base class source file as a link and then compiling it into the test dll worked for me as well.

However, starting in VS 2013 Update 2 there is now a concept of a "Shared Project" which is a way to formalize the idea of pulling in source code from another project into your project and then compiling them as one.

Here's what I did

  1. Create new "Shared Projects" project
  2. Move current test base class (and other needed files) into the shared project
  3. Add a reference to the shared project from your test project (more on this below)
  4. Compile, test, and be merry

At least on VS2015 Update 2, step 3 isn't as straight forward as I think it should be. Per this answer Visual studio doesn't provide you an easy way to link shared projects to test projects (go figure...). This is what I had to do:

  1. Unload the .csproj file,
  2. Right-click and edit the .csproj file
  3. Go all the way to the bottom and add this to the start of the <Import ...> grouping (fix path and name as needed, make sure to add Label="Shared"!):

    <Import Project="..\SharedProject\SharedProject.projitems" Label="Shared" />
    
  4. Save and close the file

  5. Reload the project
Community
  • 1
  • 1
Matt Klein
  • 7,856
  • 6
  • 45
  • 46
2

Nothing special, but another way of solving the problem by calling base methods is:

public abstract class AccountBaseTest
{
    protected abstract IAccountRepository GetAccountRepository();

    public void _submitAccountToLMS_BlankAccount_NewLmsID()
    {
       Account account = new Account(GetAccountRepository());
       account.FirstName = Faker.FirstName();
       account.LastName = Faker.LastName();
       account.SubmitToLms();
       Assert.IsTrue(account.LmsID > 0);
    }
}



[TestClass]
public class AccountIntegrationTest
{
    protected override IAccountRepository GetAccountRepository()
    {
        return new AccountRepository();
    }

    [TestMethod]
    public void SubmitAccountToLMS_BlankAccount_NewLmsID()
    {
       base._submitAccountToLMS_BlankAccount_NewLmsID();
    }
}

Hopefully VS 2012 will fix this problem....

jaredmdobson
  • 4,601
  • 1
  • 17
  • 11
1

This has been fixed, and is shipping in the 1.1.17 release here:

Framework: https://www.nuget.org/packages/MSTest.TestFramework/1.1.17

Adapter: https://www.nuget.org/packages/MSTest.TestAdapter/1.1.17

References:

  1. GitHub issue: https://github.com/Microsoft/testfx/issues/23
  2. UV item: https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/6030736-support-test-inheritance-for-base-classes-in-differ
pvlakshm
  • 1,365
  • 7
  • 7