{"id":28008,"date":"2022-03-18T18:15:21","date_gmt":"2022-03-18T18:15:21","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=28008"},"modified":"2026-03-18T18:17:26","modified_gmt":"2026-03-18T18:17:26","slug":"differences-between-factory-and-dataprovider","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/differences-between-factory-and-dataprovider\/","title":{"rendered":"Differences between @Factory and @DataProvider"},"content":{"rendered":"<p>&nbsp;<\/p>\n<style>\n        body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; }<br \/>\n        h1, h2 { color: #2c3e50; }<br \/>\n        table { border-collapse: collapse; width: 100%; margin-top: 15px; }<br \/>\n        table, th, td { border: 1px solid #333; }<br \/>\n        th, td { padding: 10px; text-align: left; }<br \/>\n        th { background-color: #f4f4f4; }<br \/>\n    <\/style>\n<p>&nbsp;<\/p>\n<h1>@Factory and @DataProvider in TestNG<\/h1>\n<p>TestNG provides powerful annotations to handle test data and test execution flow efficiently. Two commonly used annotations are <b>@Factory<\/b> and <b>@DataProvider<\/b>. Let\u2019s understand them in a simple way.<\/p>\n<h2>@Factory Annotation<\/h2>\n<ul>\n<li>@Factory is used to create multiple instances of a test class dynamically.<\/li>\n<li>It helps in running the same test class with different data sets.<\/li>\n<li>The method annotated with @Factory returns an array of objects.<\/li>\n<li>Each object represents a test class instance with different input values.<\/li>\n<li>Useful when constructor-based injection is required.<\/li>\n<\/ul>\n<h2>@DataProvider Annotation<\/h2>\n<ul>\n<li>@DataProvider is used to supply multiple sets of data to a test method.<\/li>\n<li>It returns a two-dimensional Object array (Object[][]).<\/li>\n<li>Each row represents a different test execution.<\/li>\n<li>Test method receives data as parameters.<\/li>\n<li>Useful for data-driven testing without creating multiple class instances.<\/li>\n<\/ul>\n<h2>@Factory vs @DataProvider<\/h2>\n<p>The differences between @Factory and @DataProvider annotation are as follows:<\/p>\n<table>\n<tbody>\n<tr>\n<th><\/th>\n<th>@Factory<\/th>\n<th>@DataProvider<\/th>\n<\/tr>\n<tr>\n<td>Purpose<\/td>\n<td>Create multiple instances of test classes<\/td>\n<td>Provide multiple data sets to a test method<\/td>\n<\/tr>\n<tr>\n<td>Return Type<\/td>\n<td>Object[] (array of class instances)<\/td>\n<td>Object[][] (2D array of data)<\/td>\n<\/tr>\n<tr>\n<td>Execution Level<\/td>\n<td>Class level<\/td>\n<td>Method level<\/td>\n<\/tr>\n<tr>\n<td>Data Injection<\/td>\n<td>Via constructor<\/td>\n<td>Via method parameters<\/td>\n<\/tr>\n<tr>\n<td>Use Case<\/td>\n<td>When test class needs different configurations<\/td>\n<td>When same test method runs with multiple data sets<\/td>\n<\/tr>\n<tr>\n<td>Complexity<\/td>\n<td>Slightly complex<\/td>\n<td>Easy to implement<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Conclusion:<\/b> Use <b>@DataProvider<\/b> for simple data-driven tests and <b>@Factory<\/b> when you need more control over test class instantiation.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; @Factory and @DataProvider in TestNG TestNG provides powerful annotations to handle test data and test execution flow efficiently. Two commonly used annotations are @Factory and @DataProvider. Let\u2019s understand them in a simple way. @Factory Annotation @Factory is used to create multiple instances of a test class dynamically. It helps in running the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[213],"tags":[],"class_list":["post-28008","post","type-post","status-publish","format-standard","hentry","category-testng","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/28008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=28008"}],"version-history":[{"count":2,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/28008\/revisions"}],"predecessor-version":[{"id":28010,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/28008\/revisions\/28010"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=28008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=28008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=28008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}