开发者

SpecFlow - running parallel tests

I'm implementing tests using SpecFlow that have nothing to do with each other. Is there config option for SpecFlow that enables parallel test execution? I'm using VS10 and MSTest runner that supports running "up to 5 parallel unit tests" as they claim in d开发者_高级运维ocumentation.

Thanks, max.yz


I moved away from MSTest to MbUnit to achieve this. You can achieve parallelism at test fixture level with MbUnit using the ParallelizableAttribute. However as the test fixtures are generated from the .feature Gherkin files I had to grab the SpecFlow source code and modify the MbUnitTestGeneratorProvider class in the TechTalk.SpecFlow.Generator project to output the ParallelizableAttribute. So you end up with something like this:

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

If you compile this up and use it you'll end up with parallelizable test fixtures:

[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
{

The only problem with this as it stands is that you will need to make sure that test fixtures do not conflict with one another. That is to say, a test from one fixture adds or modifies a database row that breaks a test that is running at the same time as it. There are ways around this but that is probably out of scope of your original question.

Alex.


There is a new tool called SpecRun that was recently released by the makers of SpecFlow. SpecRun will allow you to run these tests in parallel. If you use the SpecRun.Nunit package with it, you can run NUnit tests in parallel. We use SpecRun on our CI server to run the tests in parallel, but the developers use whatever their test runner of choice.

Changing your test framework can be disruptive. Because all our tests were in NUnit to start, we simply added the new SpecRun runner and nothing else changed. Very simple and transparent to the developers. And since it's available on NuGet, it was very easy to install.


I created a solution that generates a nant build file, which uses nunit in a custom parallel nant task:

https://github.com/MartyIce/SpecflowParallelizer

Due to how my legacy tests were written, I get backend concurrency problems, so it hasn't been succesful for me (yet) but hopefully this will work for someone else.


There is an option in the MSTest .testsettings file for the test project. By default the test runner will only run 1 test at a time, by changing the parallelTestCount attribute of the Execute node to 0 it will run on as many threads as are avalable (for some reason limited to a max of 5)

Just right click the .teststtings file and pick open with; choose an XML editor and off you go.

You must not run any Coded UI tests or configure any datacollectors for this to work.

For a more detailed explanation see this article

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜