Visual Studio 测试资源管理器与播放列表

6
这可能与以下问题相关:Visual Studio中的动态单元测试播放列表
我想要能够创建一个或多个测试播放列表,并且不必将每个新测试都添加到特定的播放列表中。
目前,我有一个包含所有单元测试的播放列表。但是,在未来,我想要一个自动化集成测试的播放列表,在提交到TFS之前运行,但不是每次应用程序构建时都运行。
是否有一种方法可以实现这一点?

你使用NUnit还是MSTest? - nozzleman
我使用mstest。@nozzleman - Alexander
1个回答

10

我不了解在TFS中可以使用哪些设置类型,因为我没有使用TFS,但我知道在NUnitMSTest中都可以使用Categories

使用NUnit的解决方案

对于NUnit,你可以使用Category属性来标记单个测试甚至整个测试夹具:

namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  [Category("IntegrationTest")]
  public class IntegrationTests
  {
    // ...
  }
}
或者
namespace NUnit.Tests
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class IntegrationTests
  {
    [Test]
    [Category("IntegrationTest")]
    public void AnotherIntegrationTest()
    { 
      // ...
    }
  }
}

并且只运行使用nunit-console.exe的那些测试:

nunit-console.exe myTests.dll /include:IntegrationTest

MSTest解决方案

MSTest的解决方案非常类似:

namespace MSTest.Tests
{
    [TestClass]
    public class IntegrationTests
    {
        [TestMethod]
        [TestCategory("IntegrationTests")
        public void AnotherIntegrationTest()
        {
        }
    }
}

但是在这里,您必须使用该属性标记所有测试,不能用它来装饰整个类。

然后,就像使用 NUnit 一样,在 IntegrationTests 类别中只执行那些测试:

使用 VSTest.Console.exe

Vstest.console.exe myTests.dll /TestCaseFilter:TestCategory=IntegrationTests

使用 MSTest.exe

mstest /testcontainer:myTests.dll /category:"IntegrationTests"

编辑

你也可以使用VS的TestExplorer来执行特定的测试类别。

输入图像描述
(来源:s-msft.com)

如上图所示,在TestExplorer的左上角可以选择一个类别。选择Trait并只执行想要的类别。

有关更多信息,请参见MSDN


这基本上就是我想做的。我现在已经成功使用mstest运行了测试。这将在命令提示符中运行测试,所以我想在Visual Studio内置的Test Explorer中没有支持这个功能?@nozzleman - Alexander
当然有,参见https://msdn.microsoft.com/zh-cn/library/hh270865.aspx。我会更新我的答案。 - nozzleman
谢谢,太棒了。基本上就是我想要的解决方案 :) - Alexander

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接