Parallel test execution in Visual Studio using NUnit

Similar to TestNG and JUnit frameworks for Eclipse, you likely want to run tests on more than one device at the same time. NUnit allows you to do this quite easily.

Important: This document includes references to a third-party product, NUnit. The user interface and usage of third-party products are subject to change without notice. For the latest published information about NUnit, see https://docs.nunit.org.

Create a test project

To use the parallelization options provided by NUnit, create an NUnit test project, as described in Run Perfecto Lab tests with NUnit in Visual Studio.

Run scenarios in parallel once

To run several scenarios (TestFixtures) in parallel once, do one of the following:

  •  Add the following line to the AssemblyInfo.cs file that is located under Properties in the project directory:

    Copy
    [assembly: Parallelizable(ParallelScope.Fixtures)]

    This adds parallel execution at fixture level for the entire assembly.

  • Specify the scope directly underneath the TestFixture attribute, as shown here:

Run scenarios in parallel multiple times

To run the same test fixture twice, in parallel, you need to pass strings as parameters into the TestFixture constructor. For example, the following code runs the test fixture once against an iOS device on a certain version and with a certain device description and a second time against an Android device.

Copy
[TestFixture("Android", "6.0.1", "My Description")]
[TestFixture("iOS", "9.2.1", "Another description")]
[Parallelizable(ParallelScope.Fixtures)]

The following example creates a constructor that expects these parameters:

Copy
public class RemoteWebDriverTest{

        private RemoteWebDriverExtended driver;
        String deviceOS, deviceVersion, deviceDescription;
    
    public RemoteWebDriverTest(String deviceOS, String deviceVersion, String deviceDescription){
        this.deviceOS = deviceOS;            
        this.deviceVersion = deviceVersion;            
        this.deviceDescription = deviceDescription;
    }

You can then use the class properties deviceOS,deviceVersion, anddeviceDesciption when defining device capabilities in your PerfectoOpenConnection(). For example:

Copy
capabilities.SetCapability("platformName", deviceOS);
capabilities.SetCapability("platformVersion", deviceVersion);
capabilities.SetCapability("description", deviceDescription);

The attached example goes one step further and passes 2 sets of data into the TestCase itself. Because it is not specified to run in parallel, it will be executed sequentially.

The ExpectedResult asserts the value returned by the test method to pass or fail the test:

Copy
[TestCase("myuser", "mypassword", ExpectedResult = true)]
[TestCase("user2", "mypassword", ExpectedResult = false)]
public Boolean NavigatingToUrl(String appUsername, string appPassword)

With this, the resulting code:

  • Runs two test fixtures defining two different devices

  • Runs the NavigatingToUrl test with the parameters myuser and mypassword first on both devices in parallel, expecting to pass an assertion for both

  • Runs again on each device with the user and mypassword parameters expecting to fail 

The easiest way to test this test is to create a default project (like in Run Perfecto Lab tests with NUnit in Visual Studio). Then just download (from GitHub) and add the RemoteWebDriverTest.cse (modifying it to fit the namespace).