简体   繁体   中英

Selenium C# / NUnit 3.12.0 is not running tests in parallel

I am executing my tests in Visual Studio Enterprise 2019 version 16.3.9 / Test Explorer window. My architecture is Intel(R) Core(TM) i7-7600 CPU @ 2.80Ghz (4 CPUs), ~2.9Ghz
Here is my pseudo Selenium C# code below:

[TestFixture]
internal class TestClass{

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
       initializeExtentReport()
    }

    [SetUp]
    public void Setup()
    {
       initializeChromebBrowser();
    }

    [Test]
    public void NavigatoToGmail()
    {
        bool isGmailAccessible = GoToUrl("https://www.google.com/");
        Assert.True(isGmailAccessible);
    }

    [Test]
    public void NavigatoToMicrosoft()
    {
        bool isMicrosoftAccessible = GoToUrl("https://www.microsoft.com/");
        Assert.True(isMicrosoftAccessible);
    }


    [Test]
    public void NavigatoToYahoo()
    {
        bool isYahooAccessible = GoToUrl("https://www.yahoo.com/");
        Assert.True(isYahooAccessible);
    }

    [TearDown]
    public void TearDOwn()
    {
        CloseBrowser();
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        FinishExtentReport();
    }

So here is what I tried already and it didn't work

  1. Adding [Parallelizable(ParallelScope.Children)] to the class and [Parallelizable(ParallelScope.Self)] attributes to every test method. The result - 3 browser instances open with no other actions. No URL navigating, nothing.

  2. Adding [Parallelizable(ParallelScope.All)] to class and [Parallelizable(ParallelScope.Self)] to every test method. The result - 3 browser instances open. Only the last test (NavigatoToYahoo) is executed and provides the result. The first 2 - no other action happens and the browser windows are not closed, meaning TearDown() method is not executed.

  3. Just adding [Parallelizable(ParallelScope.Self)] to every test method. The result - 3 browser instances open with no other actions. No URL navigating, nothing.

  4. Adding [Parallelizable(ParallelScope.Self)] to the class and [Parallelizable(ParallelScope.Self)] attributes to every test method. The result - 3 browser instances open. The second test (NavigateToMicrosoft) gets executed, the rest 2 - no other action, no URL navigating, nothing.

  5. Adding [Parallelizable(ParallelScope.Self)] to the class and [Parallelizable(ParallelScope.All)] attributes to every test method. The result - getting error message May not specify ParallelScope.Children on a non-parameterized test method - honestly have no idea why

I have gone through the documentation here https://github.com/nunit/docs/wiki/Parallelizable-Attribute but I feel like there is no actual way to execute unit tests in parallel using NUnit framework.

I simply want to run those 3 tests (in my actual scenario there are about 100 of them) in parallel independent of each other.

Unit tests will not run in parallel if they are in the same class. If you have 3 separate test classes, it should work.

@Neil's answer is wrong. NUnit will run the tests in an assembly in parallel according to how you specify it. However, you are dealing with two pieces of software here, each of with has it's own way of parallelizing things. I can answer WRT NUnit so at least you don't waste time playing unnecessarily with attributes.

  1. [Parallelizable] is the same as `[Parallelizable(ParallelScope.Self)]. Best style, IMO, is to use it without the attribute but the two are equal.

  2. [ParallelScope.All] is short for [ParallelScope.Self | ParallelScope.Children] [ParallelScope.Self | ParallelScope.Children] .

  3. So, you only need to worry about two settings: Self (implicit or explicit) and Children.

  4. Placing Children on a class is the same as placing Self on every test case. In both cases, the tests can run in parallel with one another.

  5. When placed on a fixture class, Self means the fixture can run in parallel with other fixtures that also have Self on them. If the tests within that fixture are also parallelizable, then they run in parallel with all the tests in all the parallel fixtures.

  6. If you were running just NUnit, ie without Selenium, you would simply put [Parallelizable] on each test or '[Parallelizable(ParallelScope.Children)]` on the fixture - either one does the same thing.

  7. If you have more than one fixture and you don't want them to run in parallel, there is no more to do. If you do want them and all their tests to run in parallel, then put [Parallelizable] on the fixture as well OR put [Parallelizabel(ParallelScope.All)] on the fixture and nothing on the test cases.

  8. Something important that many people seem to miss: [Parallelizable] makes your tests run in parallel. It does not make them capable of running in parallel. You have to do that. The attribute tells NUnit that you have made sure the test can run in parallel, so it runs it that way.

That's it for NUnit. Now, if you add Selenium to the mix it depends on how you are using it. If Selenium is starting processes for you, the things you did in NUnit have no impact. You need to understand how Selenium parallelization interacts with NUnit's. I have merely told you about what NUnit does.

An observation... unless you have very few test cases or they take a long time individually, you can probably get just as good or better performance running fixtures in parallel and letting the tests run sequentially within the fixture.

UPDATE - In your followup comment you said you only have one fixture. My answer was dealing with a much more complicated assumption. With one fixture, if you want all the tests to run in parallel, use ParallelScope.Children on the fixture. If one or two tests can't work in parallel, then mark them as NonParallelizable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM