Friday, July 6, 2018

How to execute test methods in parallel in TestNG?

Executing the test cases in parallel is one of the important automation feature. TestNG framework has this feature in built, we don't need to write separate code for this. Just with simple configuration in testng.xml file we can achieve the parallel execution at different levels.

We all know that the idea of having parallel execution is to reduce the execution time, with reduced execution time we can provide early feedback then we can save our efforts and money.

In this tutorial we will learn executing the test methods (test cases) in a java class file in parallel

  • Let's create a java class file and some test cases in it as shown in the below screenshot

  • Create a testng.xml (you can give any name to testng.xml file, parallelmethods.xml in this post) file to execute the test case in java class file as shown in the below screenshot
  • If we run the above .xml file, the test methods will be executed in sequential order
  • To run the test methods in parallel, in the parallelmethods.xml file, in the <Suite> tag add the attribute parallel="methods". And we can specify number of instances or threads that should be started using 'thread-count' attribute. In our case as we have two test cases let us give thread-count as "2", then all the test methods will be executed in two threads in parallel.
  • Save the file and run it as TestNG suite, we should see the test methods being executed in two instances/threads at a time. Check the results screenshot below
  • From the above screenshot it is clear that two test are started - executed - ended in two instances at a time
  • Now increase the number of test cases to 4
  • Run the parallelmethods.xml ad TestNG suite again and see the results in the below screenshot
  • Let us analyze the above results, if we observe the above screenshot though the two threads/instances started at a time the execution of test cases is slightly delayed between them but finally test methods are executed in parallel in both the cases.
This is about test methods execution in parallel in TestNG.

Thanks for reading. #qababu #HappyLearning #HappyTesting

No comments:

Post a Comment

Java Program 5: How to reverse given string and each word of it?

Let us understand the problem with an example, suppose "Learn Core Java" is the given string then the expected output should be &...