0

Using:
C#
NUnit 3.9
Selenium WebDriver 3.11.0
Chrome WebDriver 2.35.0

How do I maintain the context of my WebDriver while running parallel tests in NUnit?

  • When I run my tests with the ParallelScope.All attribute, my tests reuse the driver and fail

  • The Test property in my tests does not persist across the [Setup] - [Test] - [TearDown] without the Test being given a higher scope.

Test.cs

public class Test{
    public IWebDriver Driver;
    //public Pages pages;
    //anything else I need in a test

    public Test(){
        Driver = new ChromeDriver();
    }

    //helper functions and reusable functions
}

SimpleTest.cs

[TestFixture]
[Parallelizable(ParallelScope.All)]
class MyTests{

    Test Test;

    [SetUp]
    public void Setup()
    {
        Test = new Test();
    }

    [Test]
    public void Test_001(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_002(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_003(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }
    [Test]
    public void Test_004(){
        Test.Driver.Goto("https://www.google.com/");
        IWebElement googleInput = Test.Driver.FindElement(By.Id("lst-ib"));
        googleInput.SendKeys("Nunit passing context");
        googleInput.SendKeys(Keys.Return);
    }

    [TearDown]
    public void TearDown()
    {

        string outcome = TestContext.CurrentContext.Result.Outcome.ToString();
        TestContext.Out.WriteLine("@RESULT: " + outcome);
        if (outcome.ToLower().Contains("fail"))
        {
            //Do something like take a screenshot which requires the WebDriver
        }
        Test.Driver.Quit();
        Test.Driver.Dispose();
    }
}



  • The docs state: "SetUpAttribute is now used exclusively for per-test setup."

  • Setting the Test property in the [Setup] does not seem to work.

  • If this is a timing issue because I'm re-using the Test property. How do I arrange my fixtures so the Driver is unique each test?

  • One solution is to put the driver inside the [Test]. But then, I cannot utilize the TearDown method which is a necessity to keep my tests organized and cleaned up.

  • I've read quite a few posts/websites, but nothing solves the problem. [Parallelizable(ParallelScope.Self)] seems to be the only real solution and that slows down the tests.

Thank you in advance!

1

1 Answer 1

3

The ParallelizableAttribute makes a promise to NUnit that it's safe to run certain tests in parallel, but it doesn't do anything to actually make it safe. That's up to you, the programmer.

Your tests (test methods) have shared state, i.e. the field Test. Not only that, but each test changes the shared state, because the SetUp method is called for each test. That means your tests may not safely be run in parallel, so you shouldn't tell NUnit to run them that way.

You have two ways to go... either use a lesser degree of parallelism or make the tests safe to run in parallel.

Using a lesser degree of parallelism is the easiest. Try using ParallelScope.Fixtures on the assembly or ParallelScope.Self (the default) on each fixture. If you have a large number of independent fixtures, this may give you as good a throughput as you will get doing something more complicated.

Alternatively, to run tests in parallel, each test must have a separate driver. You will have to create it and dispose of it in the test method itself.

In the future, NUnit may add a feature that will make this easier, by isolating each test method in a separate object. But with the current software, the above is the best you can do.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for quick and great response Charlie! I figured my options where limited. I will just have to break the tests up into different fixtures. That will work! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.