Inherited test class from generic base is ignored in MSTest

后端 未结 4 651
死守一世寂寞
死守一世寂寞 2021-01-04 00:14

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.

相关标签:
4条回答
  • 2021-01-04 00:52

    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....

    0 讨论(0)
  • 2021-01-04 00:58

    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
    0 讨论(0)
  • 2021-01-04 00:58

    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
    0 讨论(0)
  • 2021-01-04 01:02

    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.

    0 讨论(0)
提交回复
热议问题