Search code examples
c#seleniumselenium-webdrivernunit

Working example for running Selenium tests in parallel within the same class using NUnit attribute FixtureLifeCycle value InstancePerTestCase


Starting from NUnit 3.13.1 (I'm trying 3.13.1) a new attribute was introduced for TestFixture isolation when running tests in parallel within the class. Has anybody managed to use [Parallelizable(ParallelScope.All)] + [FixtureLifeCycle(LifeCycle.SingleInstance)] and run Webdriver tests in parallel within the same class?

After activating this feature I started to get unpredictable errors, like I used to have without the new attribute. Looks like the Fixture is not isolated.

NOTE: everything works fine when running WebDriver test classes in parallel.

WebDriver is initialized in TestFixture base class looks like the following

[SetUp]
    protected void Initialize()
    {
       //InitializeWebDriver();
       Driver = new MyDriver();
    }

        [TearDown]
        public void TestFixtureTearDown()
        {
            try
            {
                //...
            }
            finally
            {
                Driver.Quit();
                Driver = null;
    
            }
        }
    }

Tests look like this:

[TestFixture]
[Parallelizable(ParallelScope.All)]
[FixtureLifeCycle(LifeCycle.SingleInstance)]
public class TestClassA : TestBase
{
    [Test]

    public void TestA1()
    {
    }
}

Solution

  • The mistake in the code was very obvious (used SingleInstance instead of InstancePerTestCase)

    Created a template project with 2 classes with 3 tests each. All 6 may be executed simultaneously without any failures. https://github.com/andrewlaser/TestParallelNUnit