【问题标题】:SpecFlow - running parallel testsSpecFlow - 运行并行测试
【发布时间】:2015-07-16 02:52:16
【问题描述】:
我正在使用 SpecFlow 实现彼此无关的测试。 SpecFlow 是否有启用并行测试执行的配置选项?我正在使用支持运行“最多 5 个并行单元测试”的 VS10 和 MSTest 运行程序,正如他们在文档中所声称的那样。
谢谢,
最大yz
【问题讨论】:
标签:
visual-studio-2010
unit-testing
specflow
【解决方案1】:
我从 MSTest 转移到 MbUnit 来实现这一点。您可以使用 ParallelizableAttribute 使用 MbUnit 在测试夹具级别实现并行性。然而,由于测试夹具是从 .feature Gherkin 文件生成的,我必须获取 SpecFlow 源代码并修改 TechTalk.SpecFlow.Generator 项目中的 MbUnitTestGeneratorProvider 类以输出 ParallelizableAttribute。所以你最终会得到这样的结果:
public class MbUnitTestGeneratorProvider : IUnitTestGeneratorProvider
{
private const string TESTFIXTURE_ATTR = "MbUnit.Framework.TestFixtureAttribute";
private const string PARALLELIZABLE_ATTR = "MbUnit.Framework.ParallelizableAttribute";
private const string TEST_ATTR = "MbUnit.Framework.TestAttribute";
private const string ROWTEST_ATTR = "MbUnit.Framework.RowTestAttribute";
private const string ROW_ATTR = "MbUnit.Framework.RowAttribute";
private const string CATEGORY_ATTR = "MbUnit.Framework.CategoryAttribute";
private const string TESTSETUP_ATTR = "MbUnit.Framework.SetUpAttribute";
private const string TESTFIXTURESETUP_ATTR = "MbUnit.Framework.FixtureSetUpAttribute";
private const string TESTFIXTURETEARDOWN_ATTR = "MbUnit.Framework.FixtureTearDownAttribute";
private const string TESTTEARDOWN_ATTR = "MbUnit.Framework.TearDownAttribute";
private const string IGNORE_ATTR = "MbUnit.Framework.IgnoreAttribute";
private const string DESCRIPTION_ATTR = "MbUnit.Framework.DescriptionAttribute";
public bool SupportsRowTests { get { return true; } }
public void SetTestFixture(CodeTypeDeclaration typeDeclaration, string title, string description)
{
typeDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(TESTFIXTURE_ATTR)));
typeDeclaration.CustomAttributes.Add(
new CodeAttributeDeclaration(
new CodeTypeReference(PARALLELIZABLE_ATTR)));
SetDescription(typeDeclaration.CustomAttributes, title);
}
如果你编译并使用它,你最终会得到可并行化的测试夹具:
[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "1.6.1.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[MbUnit.Framework.TestFixtureAttribute()]
[MbUnit.Framework.ParallelizableAttribute()]
[MbUnit.Framework.DescriptionAttribute("Test")]
public partial class TestFeature
{
目前唯一的问题是您需要确保测试夹具不会相互冲突。也就是说,来自一个夹具的测试添加或修改了一个数据库行,该行破坏了与其同时运行的测试。有一些方法可以解决这个问题,但这可能超出了您最初问题的范围。
亚历克斯。
【解决方案2】:
SpecFlow 的制造商最近发布了一个名为SpecRun 的新工具。 SpecRun 将允许您并行运行这些测试。如果你使用 SpecRun.Nunit 包,你可以并行运行 NUnit 测试。我们在 CI 服务器上使用 SpecRun 并行运行测试,但开发人员使用他们选择的任何测试运行程序。
更改测试框架可能会造成破坏。因为我们所有的测试都是在 NUnit 中开始的,所以我们只是添加了新的 SpecRun 运行器,其他没有任何改变。对开发人员来说非常简单和透明。而且由于它在 NuGet 上可用,因此安装起来非常容易。
【解决方案4】:
测试项目的 MSTest .testsettings 文件中有一个选项。
默认情况下,测试运行器一次只运行 1 个测试,通过将 Execute 节点的 parallelTestCount 属性更改为 0,它将在可用的线程数上运行 (由于某种原因限制为最多 5 个)
只需右键单击 .teststtings 文件并选择打开方式;选择一个 XML 编辑器就可以了。
您不得运行任何编码的 UI 测试或配置任何数据收集器以使其正常工作。
更详细的解释see this article