{"id":135,"date":"2017-03-03T13:20:15","date_gmt":"2017-03-03T13:20:15","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=135"},"modified":"2019-04-26T08:45:37","modified_gmt":"2019-04-26T08:45:37","slug":"how-to-use-the-dataprovider-in-testng-with-a-java-example","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2017\/03\/how-to-use-the-dataprovider-in-testng-with-a-java-example","title":{"rendered":"How to Use the DataProvider in TestNG with a JAVA Example"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>A DataProvider feeds many input sets into one test method, which keeps cases out of the code.<\/li>\n<li>Each row runs as a separate test, so a failure names the exact input.<\/li>\n<li>Keep the data external once it grows, so non-developers can extend the cases.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr -->We are going to show how to use the DataProvider in your test cases created with the TestNG unit testing framework.<br \/>\nDataProvider are used in order to create data-driven tests. Basically, it will help you to run the same test case, but with different data sets.<\/p>\n<h3>Examples of DataProviders<\/h3>\n<p>We are going to use the two dimensional object array Object[][] in order to return data and make use of it in the test case.<\/p>\n<p>In order to create a DataProvider, you need to:<br \/>\n&#8211; create a new method with the two dimensional object array Object[][] as a return type<br \/>\n&#8211; add the DataProvider annotation to the method<br \/>\n&#8211; give a name for the DataProvider<br \/>\n&#8211; return a new two dimensional object array Object[][] with the desired values for the test<\/p>\n<p>Here are some examples of values that can be provided for the DataProvider in TestNG:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\n@DataProvider(name = \"provideDaysInterval\")\npublic Object[][] provideData() {\nreturn new Object[][]{{1}, {2}, {28}, {110}, {365}, {400}, {800}};\n}\n\n\n@DataProvider(name = \"invalidIds\")\npublic Object[][] provideInvalidIds() {\nreturn new Object[][]{{\"a\"}, {\"asdasdasf\"}, {\"\u00a3!@$%^&amp;amp;*^(&amp;amp;*&amp;amp;^%\u00a3$@\u00a3!\"}, {\"1\"}, {\"2332423\"}, {\"123456786543sadfgh\"}, {\"1234567890\"}};\n}\n\n@DataProvider(name = \"minMaxDates\")\npublic Object[][] provideMinMaxDateRanges() {\nreturn new Object[][]{\n{\"2013-01-04\", \"2014-01-04\", \"2014-04-04\", \"2015-07-04\"},\n{\"2013-01-04\", \"2013-04-04\", \"2014-04-04\", \"2014-07-04\"}\n};\n}\n<\/code><\/pre>\n<p>Now, you can make use of these DataProviders in your test cases by following the below steps:<\/p>\n<p>&#8211; add the dataProvider attribute to the @Test annotation, and specify which is the dataProvider that you want to use. Make sure the data types defined in the two dimensional object array Object[][] are reflected in your test method attributes, see more details about the implementation of the DataProvider in TestNG:<\/p>\n<pre class=\"lang-java\"><code class=\"lang-java\">\n@Test(groups = {\"smoke\"}, dataProvider = \"provideDaysInterval\")\npublic void test_Days_Are_Valid(int numberOfDaysInterval){\n\n\n}\n\n@Test(groups = {\"smoke\"}, dataProvider = \"minMaxDates\")\npublic void test_Data_Ranges_Validate_Min_Max(String startDateFirst, String endDateFirst, String startDateLast, String endDateLast){\n\n}\n<\/code><\/pre>\n<p>This is the way to created automated data-driven test cases with TestNG and DataProviders in Java.<\/p>\n<p>&nbsp;<\/p>\n<p>We found the use of DataProviders very useful, especially for API Testing and UI Testing with Selenium WebDriver. <a href=\"https:\/\/loadfocus.com\/blog\/2013\/09\/05\/how-to-locate-web-elements-with-selenium-webdriver\/\">More details on how to&nbsp;find web elements in&nbsp;Selenium WebDriver can be found here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>Click here for <a href=\"https:\/\/courses.loadfocus.com\/online\/the-ultimate-selenium-webdriver-testing-hands-on-guide?id=7b5779cb1ad8cca84cc324a1d21cfcb1\">Online Tutorial for using Java and TestNG for testing with Selenium WebDriver<\/a>.<!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What does a TestNG DataProvider do?<\/h3>\n<p>It feeds several sets of input into one test method. The method is annotated with the provider name and TestNG calls it once per row, which keeps the cases out of the test body.<\/p>\n<h3>How does a failure report which input broke?<\/h3>\n<p>Each row runs as a separate test, so the report names the failing parameters directly. That is the main advantage over looping inside a single test, where one bad input hides the rest.<\/p>\n<h3>What return type does the provider need?<\/h3>\n<p>A two dimensional Object array, where each inner array is one set of arguments matching the test method&#8217;s parameters. Once the data grows beyond a screenful, move it to a file so it can be extended without touching code.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/06\/how-to-clear-local-storage-using-selenium-webdriver\">How to Clear Local Storage using Selenium WebDriver?<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2015\/07\/how-to-get-all-the-options-in-a-select-tag-using-selenium-webdriver\">Get All Select Options in Selenium WebDriver<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways A DataProvider feeds many input sets into one test method, which keeps cases out of the code. Each row runs as a separate test, so a failure names the exact input. Keep the data external once it grows, so non-developers can extend the cases. We are going to show how to use the&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2017\/03\/how-to-use-the-dataprovider-in-testng-with-a-java-example\" class=\"more-link\" title=\"Read How to Use the DataProvider in TestNG with a JAVA Example\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":504,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[24,23],"class_list":["post-135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-data-driver-test-cases-with-testng","tag-use-the-dataprovider-in-testng"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":3,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":327,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/135\/revisions\/327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/504"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}