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:
Prerequisites
If you have already run your first test, you can skip the prerequisites.
- BrowserStack Username and Access key, which you can find in your account settings. If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.
-
XUnit
andXUnit.Runner
is installed from the NuGet Gallery. - Visual Studio is installed.
Run your first parallel tests
To run Selenium tests in parallel with XUnit on BrowserStack Automate, complete the following steps:
- Clone the xunit-browserstack sample repository on GitHub, if not already done:
git clone https://github.com/browserstack/xunit-browserstack.git
-
Open the solution
XUnit-BrowserStack.sln
in Visual Studio. - 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"); } } }
- Run your parallel test on BrowserStack using the following command:
dotnet test --filter "profile=parallel"
Next steps
After you have successfully run your local test on BrowserStack, you might want to do one of the following:
- Run Local tests on BrowserStack
- Generate a list of capabilities that you want to use in tests
- Find information about your Projects, Builds and Sessions using our REST APIs
- Set up your CI/CD: Jenkins, Bamboo, TeamCity, Azure, CircleCI, BitBucket, TravisCI, GitHub Actions.
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
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!