TestNG Parallel Execution With Examples

We can run TestNG tests in parallel. This feature is only available when we are using TestNG XML suite for executing our test cases.

TestNG Default Tests Execution

When we run TestNG class, methods are executed in order of the alphabetical appearance of their names. Let’s look at a simple TestNG test class.

When we run above class, it will generate output like below.

Notice that bar() method is getting executed first, once all test executions are over then foo() methods are executed.

We will get the similar result with below XML suite file.

TestNG Methods in Parallel

Since both the test methods are unrelated, we can run them in parallel and save time. We will have to add parallel and thread-count to our test to achieve this.

Below is our updated XML suite file.

When we run the test suite, we will get following output.

Notice that we almost cut the test execution time to half by running test methods in parallel.

TestNG Parallel Execution Important Points

  1. We can use parallel attribute for both test and suite elements.
  2. Possible values for parallel attribute are: methods, tests, classes and instances. We have already seen how to execute methods in parallel.
  3. In older TestNG versions, true and false were also allowed for parallel values, they have been deprecated now.

TestNG Running Classes in Parallel

Let’s say we want to run our test classes in parallel. We also want our methods inside the test classes to be running in parallel. We can do this by using parallel="classes" at suite level and parallel="methods" at test level.

TestNG Suite Running Parallel Tests

Let’s look at a more complex example, where we are running TestNG suite tests in parallel. Again some of these tests are configured to execute classes/methods in parallel.

That’s all for running TestNG test suites in parallel, in different modes.

By admin

Leave a Reply

%d bloggers like this: