Learn to Use TestNG Factory Method for Load Testing

In this post, we are again going to teach you an out of the box feature of TestNG. It is none other than the TestNG factory method. In this TestNG tutorial, we’ll cover how to create multiple instances of test classes and generate load using the TestNG factory method.

And since we receive a lot of visitors who are just beginning their automation journey, so it’s customary to start with a little introduction on using the TestNG framework. The most simple way to use it is by adding its plugin to your Eclipse. Alternatively, you can add it as a dependency in the POM file while using Maven as a build tool.

Every TestNG project sources its settings from an XML file which usually has a name like <testng.xml>. It follows a <suite-test> like structure to group test cases.

1. At the root level, it starts with a <suite> tag which wraps a collection of <test> tags.
2. Each test can further contains multiple <class> tags inside the <classes> tag.
3. Every TestNG <class> has a linked reference to a Java class. It may contain one or more test methods.
4. With the help of @Test, you can annotate a Java method to run as a test in the TestNG suite.

If you wish to know more about the TestNG annotations, then please go through the following blog post.

And, here is a link to an express TestNG tutorial to guide you in creating an automation project from scratch.

Now, as you seem more interested to know about the TestNG factory method and its application to perform load testing. So let’s first define load testing scenarios where we’ll be using this method.

1. Run a test case multiple times which we’ve demonstrated in the previous post.
2. Set up a class with multiple test methods to run on a recurring basis. We’ll automate this scenario using the TestNG factory method in the below sections.

Table of Content.

So let’s begin by understanding the @Factory annotation followed by a review of the load testing use case.

What is TestNG Factory Method (@Factory Annotation)?

@Factory annotation is the implementation of Factory design pattern in TestNG framework. It enables the dynamic creation of tests to run them with a distinct data set which we want them to use.

What is the use case for load testing in this tutorial?

In this post, we’ll create a TestNG class <LoadTestScenario> which contains the following three test cases.

1. Test case1 – This case will run, but we are skipping it on purpose.
2. Test case2 – This case will make a web request to Google and check for the presence of the “Gmail” web link.
3. Test case3 – It is again a case which we are failing on purpose. I’ll make a web request to Google home page and check for a “Sign-in” which doesn’t exist.

Next, we’ll add an another class <LoadTestFactory> annotated with @Factory. It’ll create ten instances of the <LoadTestScenario> class and each of them would run the three test cases as stated above.

Please note, in a real-time load test case scenario, you should define the test cases as per your requirements.

This post is for educational purpose only. And the objective here is to demonstrate the ability of @Factory annotation. Also, we wanted to show a test report that could display all kind of statuses.

How to Use TestNG Factory Method for Load Testing?

Now, we’ll give you the steps to create a complete load testing project.

1. Create a WebDriver/TestNG Project Using Maven in Eclipse.

Please refer the below post to check out the required steps to create a project using Maven.

Here is the attached POM file which we’ve used in this TestNG load testing example. You would also need to copy/paste this file into your project.

1.1. POM File.


1.2. TestNG XML file.

Copy/paste this file to your project. Make sure it stays at the root of the project alongside with the POM file.


2. Define the Sample TestNG Class for Load Testing.


3. Create the TestNG Factory Method Class for Load Testing.


4. Test Execution and Reporting.

In a maven project, it is very simple to run the tests. Just, select the POM file in the Eclipse project pane. Right-click on it and choose the Run As >> Maven test option. It’ll make all the tests to hit the deck.

There is a total of thirty tests which will run. Out of them, ten tests will pass, ten would skip, and ten will get failed.

We’ve used ReportNG library for creating reports. For this, we’ve added a dependency to its library in the maven’s POM file. You might like to check out the POM file to review the reporting functionality.

Here is the ReportNG execution report attached after the test-run.

4.1. Load Test Execution Summary.

TestNG Factory Method Example - Execution Summary

4.2. Load Test Execution Detail.

TestNG Factory Method Example - Execution Detail

Summary – How to Use TestNG Factory Method for Load Testing.

We are hopeful that this post will enable you in using TestNG @Factory annotation for load testing. Also, you would’ve learned how to use ReportNG for reporting in TestNG projects.

It would be great if you let us know your feedback on this post.

Also, you can ask us to write on a topic of your choice. We’ll add it to our writing plan.

If you’d enjoyed the post, then please don’t leave without sharing it with friends and on social media.

Keep Learning,


Leave a Reply