0

As in the title, is it possible to run test methods within the same test class in parallel to speed up the testing process?

Currently using Junit but planning to switch to TestNG eventually so feel free to use either for your answer.

Using the tests in the context of Functional Testing (Selenium) and since the tests check the production server there would be no issue to using multiple threads to speed up the testing process.

From my search so far I have established that with TestNG I can run different classes(containing test methods) in parallel, what I wanted to know is if I can run the methods inside the same test class in a parallel and not serialized way.

Leon
  • 408
  • 1
  • 6
  • 17
  • Possible duplicate of [How to make JUnit test cases execute in parallel?](https://stackoverflow.com/questions/5529087/how-to-make-junit-test-cases-execute-in-parallel) – borowis May 26 '17 at 12:20
  • @borowis while I havent seen that thread before posting, I would like to know of TestNG's implementation as well since thats the end goal. Also that answer's suggested implentation is about running multiple classes' in parallel streams. I am asking how to run multiple methods inside the same test class in parallel. – Leon May 26 '17 at 12:31
  • `feel free to use either for your answer`, but alright, I understand. there was how to parallel methods for junit either. – borowis May 26 '17 at 12:32
  • 1
    @borowis if one had the implementation and the other didnt yeah I would go with that as a matter of necessity but since I deem testNg as better solution (with my little knowledge on the matter so far as well as what google search lead me to), if both have this, then I would definitely would like to know so I can back my decision to move to TestNG even better. – Leon May 26 '17 at 12:35

1 Answers1

3

The parallelization in TestNG depends on the mode specified in your suite.

<suite name="My suite" parallel="methods" thread-count="5">

Will make 5 methods at the same time run in paralell, as long as they don't depend on each other. Dependend methods will wait until all dependencies has been runned before they are started. Other modes are "tests", "classes" and "instances".

If you want different threading for different tests inside the same xml you can add the parellel property to the test tag instead e.g.

<suite name="default" thread-count="4">
    <test name="asa" parallel="methods">
        <classes>
            <class name="multiThreaded"/>
            <class name="multiThreaded"/>
        </classes>
    </test>
    <test name="asa" parallel="false">
        <classes>
            <class name="singleThreaded"/>
            <class name="singleThreaded"/>
        </classes>
    </test>
</suite>

See http://testng.org/doc/documentation-main.html#parallel-tests for more information

Jotunacorn
  • 496
  • 1
  • 4
  • 12
  • Havent finished implementing the selenium wrapper framework yet in order to migrate to testng so I know only a little about testng specifics yet. Thanks for the link. Seems like an elegant way to handle this without much boilerplate code added either. Just a specification out of curiosity, can you have both in the same xml or the parallel attribute's value has to be unique? – Leon May 26 '17 at 12:37
  • 1
    the `` tag is is unique for the xml but you can specify threading for each group of tests e.g. – Jotunacorn May 26 '17 at 13:44