Skip to main content

Run XUnit Tests in Parallel

Run automated tests on multiple browsers in parallel with XUnit using BrowserStack Automate.

Introduction

On BrowserStack, you can run multiple XUnit tests at the same time across various browser, device, and OS combinations. This is Parallel Testing. Parallel Testing gives you the same benefits as running a multi-threaded application.

With Parallel Testing, you can run the same test on different browser/device combinations i.e. cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing will help you reduce the run time of your test suite, resulting in faster build times and faster releases.

In this guide, you will learn about:

  1. Prerequisites to run tests in BrowserStack
  2. Steps to run your first parallel test

Prerequisites

If you have already run your first test, you can skip the prerequisites.

Run your first parallel tests

To run Selenium tests in parallel with XUnit on BrowserStack Automate, complete the following steps:

  1. Clone the xunit-browserstack sample repository on GitHub, if not already done:

    git clone https://github.com/browserstack/xunit-browserstack.git
    
  2. Open the solution XUnit-BrowserStack.sln in Visual Studio.

  3. Set your BrowserStack credentials and browser-device combinations where you want to run your test in the config.json file as follows:

    xunit-browserstack/XUnit-BrowserStack/config.json
     {
         "server": "hub.browserstack.com",
         "user": "YOUR_USERNAME",
         "key": "YOUR_ACCESS_KEY",
    
         "capabilities": {
             "bstack:options": {
             "buildName": "xunit-browserstack",
             "sessionName": "test",
             "debug": "true"
             }
         },
    
         "environments": [
             {
             "browserName": "chrome"
             },
             {
             "browserName": "firefox"
             },
             {
             "browserName": "safari"
             }
         ],
    
         "localOptions": {
             "forceLocal": true
         }
         }
    

    You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

    The following code runs your tests in parallel with XUnit on BrowserStack.

       using Xunit;
       using OpenQA.Selenium;
       using OpenQA.Selenium.Remote;
       using OpenQA.Selenium.Support.UI;
    
       namespace XUnit_BrowserStack
       {
           public abstract class ParallelBaseTest
           {
               private readonly BaseFixture baseFixture;
    
               public ParallelBaseTest(BaseFixture baseFixture)
               {
                   this.baseFixture = baseFixture;
               }
    
               public void BaseTest(string platform)
               {
                   try
                   {
                       RemoteWebDriver driver = baseFixture.GetDriver(platform, "parallel");
                       WebDriverWait webDriverWait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(2000));
                       driver.Manage().Window.Maximize();
                       driver.Navigate().GoToUrl("https://bstackdemo.com/");
                       Assert.Equal("StackDemo", driver.Title);
    
                       string productOnPageText = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"1\"]/p"))).Text;
    
                       webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"1\"]/div[4]"))).Click();
                       bool cartOpened = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@class=\"float-cart__content\"]"))).Enabled;
                       Assert.True(cartOpened);
                       string productOnCartText = webDriverWait.Until(driver => driver.FindElement(By.XPath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).Text;
                       Assert.Equal(productOnCartText, productOnPageText);
                       baseFixture.SetStatus(cartOpened && productOnCartText.Equals(productOnPageText));
                   }
                   catch (Exception)
                   {
                       baseFixture.SetStatus(false);
                       throw;
                   }
               }
           }
    
           public class ChromeTest : ParallelBaseTest, IClassFixture<BaseFixture>
           {
               public ChromeTest(BaseFixture baseFixture): base(baseFixture) { }
    
               [Fact]
               [Trait("profile", "parallel")]
               public void Test()
               {
                   BaseTest("chrome");
               }
           }
    
           public class FirefoxTest : ParallelBaseTest, IClassFixture<BaseFixture>
           {
               public FirefoxTest(BaseFixture baseFixture) : base(baseFixture) { }
    
               [Fact]
               [Trait("profile", "parallel")]
               public void Test()
               {
                   BaseTest("firefox");
               }
           }
    
           public class SafariTest : ParallelBaseTest, IClassFixture<BaseFixture>
           {
               public SafariTest(BaseFixture baseFixture) : base(baseFixture) { }
    
               [Fact]
               [Trait("profile", "parallel")]
               public void Test()
               {
                   BaseTest("safari");
               }
           }
    
       }
    
  4. Run your parallel test on BrowserStack using the following command:
         dotnet test --filter "profile=parallel"
    
Note: Achieve your test coverage and build execution time goals by using our calculator to understand how many parallel sessions you need.

Next steps

After you have successfully run your local test on BrowserStack, you might want to do one of the following:

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy