MSTest 中忽略从通用基继承的测试类

发布于 2024-11-07 05:39:19 字数 1087 浏览 0 评论 0原文

在 MSTest 中创建通用基测试类并继承它时,我无法运行所有继承类的测试。

Unit test results

BaseDependencyPropertyFactoryTest 位于 Whathecode.PresentationFramework.Tests > 组装。它是通用基类。 (BaseDependencyPropertyFactoryTest

两个程序集都有一个继承自该基类的测试,称为DependencyPropertyFactoryTest。继承类所做的只是传递特定类型参数。

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

似乎只有位于与基类相同的程序集中的继承测试才能运行。 Whathecode.PresentationFramework.Aspects.Tests 程序集中继承的测试似乎完全被忽略。

我做错了什么?如果需要,我可以上传所有必需的源代码,但您将需要 PostSharp 来进行方面程序集。


作为测试,我尝试向方面程序集中的继承测试类添加一个测试,该测试调用基测试类中的所有测试。

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

这给出了以下结果。奇怪的是这个测试被执行了!目前,这可能至少可以作为运行它们的一种方式,但当然,我不想每次在基类中添加额外的测试时都编辑此测试。

编辑后的单元测试结果

为什么会跳过这些基本测试,以及为什么指示“中止”?

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'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十年不长 2024-11-14 05:39:19

其原因与泛型无关,而是与测试位于不同的程序集中有关。

Microsoft Connect 建议描述了该问题:"Visual Studio Test (MSTest) 以及缺乏对驻留在不同组件中的基类的继承支持程序集。” 它被标记为“已修复”,但在 Visual Studio 2010 中似乎还没有修复,也许它还需要发布?

对于这个问题有一个有趣的解决方法:

您可以通过以下方式解决此问题
编译源文件包含
所有测试项目的基类
希望从该基础获得的
班级。将项目添加为“链接”,以便
你最终不会得到多个
基础源文件的副本
类。

这对我有用,而且我不认为解决方法太丑陋。

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.

七婞 2024-11-14 05:39:19

没什么特别的,但是通过调用基本方法解决问题的另一种方法是:

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();
    }
}

希望 VS 2012 能够解决这个问题......

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

翻身的咸鱼 2024-11-14 05:39:19

Steven 的答案是将基类源文件添加为链接,然后将其编译到测试 dll 中,这也对我有用。

然而,从 VS 2013 Update 2 开始,出现了“共享项目”的概念,这是一种形式化将源代码从另一个项目拉入您的项目,然后将它们编译为一个项目的想法的方法。

这就是我所做的

  1. 创建新的“共享项目”项目
  2. 将当前测试基类(和其他需要的文件)移动到共享项目中
  3. 添加对测试项目中共享项目的引用(更多内容如下)
  4. 编译,测试,然后快乐

至少在 VS2015 Update 2 上,步骤 3 并不像我想象的那么简单。根据这个答案,Visual Studio 没有为您提供一种将共享项目链接到测试项目的简单方法(见图... )。这就是我必须做的:

  1. 卸载 .csproj 文件,
  2. 右键单击并编辑 .csproj 文件
  3. 一直到底部并将其添加到 分组(根据需要修复路径和名称,确保添加 Label="Shared"!):

    <导入项目=“..\SharedProject\SharedProject.projitems”标签=“共享”/>/>
    
  4. 保存并关闭文件

  5. 重新加载项目

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文