Selenium Grid with parallel testing using C#/NUnit

9.9k Views Asked by At

I've got several unit tests written with NUnit that are calling selenium commands. I've got 2 win2k3 server boxes setup, one is running selenium grid hub along with 2 selenium rc's. The other box is running 5 selenium rc's. All of them are registered with the hub as running Firefox on Windows (to keep it simple). In my unit test setup method I've got it connected to the hub's hostname at port 4444.

When running the tests, they only run sequentially (as expected). I've done a lot of reading on NUnit's roadmap and how they are shooting for parallel testing abilities. I've seen lots of pointers to using PNUnit in the meantime. However this seems to completely defeat the purpose of the Selenium Grid.

Have any of you successfully implemented parallel testing using C#/NUnit connected to a Selenium Grid setup? If so, please elaborate.

I'm at a complete loss at how this will/can work using NUnit as it exists now (I'm using version 2.9.3)

5

There are 5 best solutions below

1
On BEST ANSWER

Unfortunately NUnit can not run tests in parallel, so you have to use another runner to archive all advantages of parallel testing with Selenium Grid.

I using Gallio runner for my tests on c# and have examples here:

project on c# with tests runned in parallel: http://code.google.com/p/design-of-selenium-tests-for-asp-net/

description: http://slmoloch.blogspot.com/2009/12/design-of-selenium-tests-for-aspnet_19.html

Gallio test runner: http://www.gallio.org/

0
On

Gallio is outdated today so you could stick to NUnit solution.

Since 3.7 version NUnit allows running test in parallel. Before that, it was possible to do that on a fixture level but in 3.7+ you can even for tests in one TestFixute. See my example below to demonstrate how it could be achieved.

using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace ConsoleApp1
{
    [TestFixture]
    public class Dummy
    {
        static TestCaseData Case(int i)
            => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case {i}");

        public static IEnumerable<TestCaseData> Cases()
            => Enumerable.Range(1, 5).Select(Case);

        [TestCaseSource(nameof(Cases)), Parallelizable(ParallelScope.Children)]
        public void ItShouldSleep(TimeSpan t)
            => Thread.Sleep(t);


        static TestCaseData Case2(int i)
            => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case2 {i}");

        public static IEnumerable<TestCaseData> Cases2()
            => Enumerable.Range(1, 5).Select(Case2);

        [TestCaseSource(nameof(Cases2)), Parallelizable(ParallelScope.Children)]
        public void ItShouldSleep2(TimeSpan t)
            => Thread.Sleep(t);
    }

    [TestFixture()]
    public class Dummy2
    {
        static TestCaseData Case(int i)
            => new TestCaseData(TimeSpan.FromSeconds(2)).SetName($"Case {i}");

        public static IEnumerable<TestCaseData> Cases()
            => Enumerable.Range(1, 5).Select(Case);

        [TestCaseSource(nameof(Cases)), Parallelizable(ParallelScope.Children)]
        public void ItShouldSleep(TimeSpan t)
            => Thread.Sleep(t);
    }
}
0
On

By using Selenium Grid in combination with .NET's task Parallel library and the DynamicObject class you can run the same test on multiple Selenium Grid Nodes (multiple browsers) at the same time.

http://blog.dmbcllc.com/running-selenium-in-parallel-with-any-net-unit-testing-tool/

0
On

NCrunch (http://www.ncrunch.net/) is worth looking at - it has the option of distributed processing as part of a build and one of it's main features is parallel testing.

0
On

There's also PNUnit, worth taking a look at, I haven't tried it yet for parallel testing.