TestNG - Basic Annotations - DataProvider

@DataProvider annotation helps us write data-driven test cases. The @DataProvider annotation enables us to run a test method multiple times by passing different data-sets.
The following is a list of attributes supported by the @DataProvider annotation:
name
The name of this data provider. If it's not supplied, the name of this data provider will automatically be set to the name of the method. The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method. 
parallel
If set to true, tests generated using this data provider are run in parallel. Default value is false.
Create a java test class, say, TestAnnotationDataProvider.java in /work/testng/src.
Create a java test class, say, TestAnnotationDataProvider.java in /work/testng/src.
Add a test method testMethod() to your test class.
Add a test method testMethod() to your test class.
Add an Annotation @Test to method testMethod().
Add an Annotation @Test to method testMethod().
Add a method dataProviderMethod to the test class with annotation @DataProvider.
Add a method dataProviderMethod to the test class with annotation @DataProvider.
Implement the test condition and check the behaviour of @DataProvider annotation.
Implement the test condition and check the behaviour of @DataProvider annotation.
Following are the TestAnnotationAfterMethod.java contents:
  import org.testng.annotations.Test;
  import org.testng.annotations.DataProvider;

  public class TestAnnotationDataProvider {
    @DataProvider(name = "data-provider")
    public Object[][] dataProviderMethod() {
        return new Object[][] { { "data one" }, { "data two" }, { "data three" } };
    }

    @Test(dataProvider = "data-provider")
    public void testMethod(String data) {
        System.out.println("Data is: " + data);
    }

  }
Next, let's create testng.xml file in /work/testng/src, to execute test case(s). This file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  <suite name="Suite">
    <test thread-count="5" name="Test">
      <classes>
        <class name="TestAnnotationDataProvider"/>
      </classes>
    </test> <!-- Test -->
  </suite> <!-- Suite -->
Compile the test case using javac.
/work/testng/src$ javac TestAnnotationDataProvider.java

Now, run the testng.xml, which will run the test case defined in <test> tag. As you can see the @DataProvider passes parameters to the test method.
/work/testng/src$ java org.testng.TestNG testng.xml

Verify the output.
  Data is: data one
  Data is: data two
  Data is: data three

  ===============================================
  Suite
  Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
  ===============================================


    38 Lectures 
     4.5 hours 

 Lets Kode It

    15 Lectures 
     1.5 hours 

 Quaatso Learning

    28 Lectures 
     3 hours 

 Dezlearn Education
 Print
 Add Notes
 Bookmark this page