SpecFlow - 并行运行测试

8
我正在使用SpecFlow实现与彼此无关的测试。是否有SpecFlow的配置选项可以启用并行测试执行?我正在使用支持运行“最多5个并行单元测试”的VS10和MSTest运行程序,正如他们在文档中所声称的那样。
谢谢, max.yz

有一个名为SpecRun的新专业测试运行器可用于SpecFlow,它允许并行测试执行以及其他一些智能集成测试执行功能。你可以在http://www.specrun.com找到更多信息。 - chr99ha
4个回答

2

我从MSTest转向MbUnit来实现这一点。使用ParallelizableAttribute可以在测试夹具级别上实现并行性。然而,由于测试夹具是从.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
{

作为现状,唯一的问题是你需要确保测试夹具之间不会发生冲突。也就是说,一个夹具的测试添加或修改了一个数据库行,导致同时运行的另一个测试失败。虽然有解决方法,但这可能超出了你最初的问题范围。
Alex.

2
我创建了一个解决方案,生成一个nant构建文件,其中使用自定义并行nant任务中的nunit。

https://github.com/MartyIce/SpecflowParallelizer

由于我的旧测试是如何编写的,我遇到了后端并发问题,所以对我来说还没有成功,但希望这对其他人有用。

2
有一个叫做 SpecRun 的新工具,最近由 SpecFlow 的制造商发布。SpecRun 可以让你并行运行这些测试。如果你使用 SpecRun.Nunit 包,你可以并行运行 NUnit 测试。我们在 CI 服务器上使用 SpecRun 并行运行测试,但开发人员使用他们选择的任何测试运行器。

更改测试框架可能会产生干扰。因为我们所有的测试最初都是用 NUnit 编写的,所以我们只需添加新的 SpecRun 运行器,其他什么也不需要改变。对开发人员来说非常简单和透明。而且,由于它可以在 NuGet 上获得,安装也非常容易。


这是付费软件。 - HamsterWithPitchfork

0
在 MSTest 的 .testsettings 文件中有一个选项适用于测试项目。 默认情况下,测试运行器一次只会运行 1 个测试,通过将 Execute 节点的 parallelTestCount 属性更改为 0,它会在尽可能多的线程上运行(出于某种原因最多限制为 5)。
只需右键单击 .testsettings 文件并选择“打开方式”;选择 XML 编辑器即可开始使用。
为使其工作,您不能运行任何 Coded UI 测试或配置任何数据收集器。
要获取更详细的说明,请 参阅本文

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