Quick revision
The short answer
A TestNG @Factory is a method or constructor that produces test class instances, returned as Object[], and TestNG then runs all the test methods of each instance. We use it when a whole class should run once per configuration, such as once per browser or once per tenant, with that configuration set in the constructor. A @Factory can be fed by a data provider on a constructor, and the difference is one of scope: a data provider varies the arguments of a single method, while a factory varies whole instances, which should implement ITest or override toString so reports show which configuration each result belongs to.
What to remember
In this answer, we saw that a TestNG @Factory produces test class instances and TestNG runs all test methods of each one, which suits running a whole class per browser, tenant or other configuration set in the constructor. A factory can be fed by a data provider on its constructor. A data provider varies the arguments of one method, while a factory varies whole instances, and instances should implement ITest or override toString so reports show which configuration each result belongs to.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A TestNG @Factory is a method or constructor that produces test class instances, returned as Object[], and TestNG then runs all the test methods of each instance. We use it when a whole class should run once per configuration, such as once per browser or once per tenant, with that configuration set in the constructor. A @Factory can be fed by a data provider on a constructor, and the difference is one of scope: a data provider varies the arguments of a single method, while a factory varies whole instances, which should implement ITest or override toString so reports show which configuration each result belongs to.
Why this question is asked
Data-driven testing in TestNG usually starts with @DataProvider, and sooner or later a team needs something bigger: the same twenty tests against three browsers, or every test of a class against each tenant. Copying classes or adding a parameter to every method both scale badly. Interviewers ask about @Factory to see whether we know the right tool for varying a whole class, how it differs from a data provider, and how to keep the reports readable. In this answer, we will recap the two features, write a factory, combine it with a data provider, compare the two, and look at naming instances in reports.
1A Quick Recap: Two Ways to Vary Tests
A data provider supplies rows of arguments to one test method, and TestNG calls that method once per row. That is ideal when one rule must hold for many inputs, such as an email validator checked against twenty strings.
A factory works one level up. It creates several instances of a test class, each set up with different state, and TestNG treats each instance as a separate set of tests: it runs the instance's configuration methods and all its test methods. So if a class has 15 test methods and the factory creates three instances, TestNG runs 45 tests.
2Writing a Factory
The simplest factory is a method annotated @Factory that returns an array of instances. Here the same checkout tests run against three browsers.
Notice that the configuration lives in the constructor and in fields, which is natural: each instance is one complete configuration. @BeforeClass runs once per instance, so each browser starts once and serves that instance's three tests. Without a factory, we would need three copies of the class or a browser parameter on every method, and the browser would be started in each method instead of once.
3Combining a Factory With a Data Provider
Listing instances by hand is fine for three browsers but not for a list that comes from configuration. TestNG lets us put @Factory on a constructor and name a data provider. Each row of the provider becomes the constructor arguments of one instance.
- Call the factorymethod or constructor with a data provider
- Receive instancesone per configuration
- For each instance: @BeforeClasse.g. start that browser
- Run all test methods on the instance
- For each instance: @AfterClass
- Report results per instancenamed by ITest or
toString
6 steps in order. Play them, or step through them one at a time.
A factory multiplies whole classes; a data provider multiplies one method's calls.
The data provider is static because TestNG calls it before any instance exists. Adding a tenant is now one new row, and every test in the class runs for it automatically.
4Factory or Data Provider: How to Choose
The question to ask is what varies. If the input to one behaviour varies, such as different emails for one validator, a data provider is simpler and keeps everything in one method. If the environment of many behaviours varies, such as the browser, the tenant, the locale or the API version, and that environment needs its own setup and clean-up, a factory fits, because the setup naturally belongs to an instance.
| Aspect | @DataProvider | @Factory |
|---|---|---|
| What it varies | Arguments of one test method | Whole test class instances |
| Where the variation lives | Method parameters | Constructor and fields |
| Setup per variation | @BeforeMethod per row | @BeforeClass per instance |
| Typical use | Many inputs for one rule | Same tests per browser, tenant, locale |
| Result count | Rows for that method | Instances x methods in the class |
5Readable Names in Reports
There is one practical problem with factories. All instances are of the same class, so by default the report shows CheckoutTest.addsToCart three times, and when one fails we cannot tell which browser it was. The fix is to give each instance a name. Implementing ITest lets the instance supply a test name; overriding toString also helps in many reporters and IDEs.
How exactly each reporter uses the name differs between TestNG versions, IDEs and CI plugins, so it is worth checking the output once. The principle is the same everywhere: a failure in a factory-created instance must say which configuration failed, or it costs time to investigate.
6Follow-up questions
Can a factory create instances of different classes?
Yes. A @Factory method returns Object[], so it can mix instances of several test classes, for example a configured API test and a configured UI test for the same tenant. In practice, keeping one class per factory is easier to understand.
Do factory instances run in parallel?
They can. With parallel="instances" in testng.xml, TestNG runs different instances in parallel while methods of the same instance stay on one thread, which suits per-browser instances that each own a driver. The thread-count attribute limits how many run at once.
When would you avoid a factory?
When the variation is small and local, a data provider is simpler and easier to read. Factories also make it harder to run a single configuration from the IDE, so for a single parameter of one method I would not use one.
7Conclusion
In this answer, we saw that a TestNG @Factory produces test class instances and TestNG runs all test methods of each one, which suits running a whole class per browser, tenant or other configuration set in the constructor. A factory can be fed by a data provider on its constructor. A data provider varies the arguments of one method, while a factory varies whole instances, and instances should implement ITest or override toString so reports show which configuration each result belongs to.
Open with
A @Factory is a method, or a constructor, that creates instances of a test class, and TestNG then runs every test method on each instance, with its own @BeforeClass and @AfterClass.
Then, if they want more
So if a class has three tests and the factory returns three instances, I get nine results. I use it when a whole class should run once per configuration, for example the checkout tests once per browser, or the billing tests once per tenant.
The configuration goes into the constructor and fields, and the expensive setup for it, like starting the browser, runs once per instance in @BeforeClass.
Instead of listing instances by hand, I can put @Factory on the constructor with dataProvider equals tenants, and each row of a static data provider becomes the constructor arguments of one instance, so adding a tenant is one row.
The difference from a data provider is scope: a data provider varies the arguments of a single test method and calls it once per row, while a factory varies whole instances of the class. If the input of one rule varies, I use a data provider; if the environment of many tests varies, I use a factory.
One practical point: all instances share a class name, so I implement ITest or override toString to include the browser or tenant, otherwise the report cannot tell which configuration failed.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a TestNG @Factory, and when would you use it instead of a @DataProvider?": https://scrivio-interview-library.netlify.app/java/testng/e26fd2d79b/