There are a lot of times that you need to run the same test case with different test data. This is called parameterized testing. TestNG provide two option that you can choose to pass test data to your test method.
- Pass test data when define test case in testng.xml.
- Use DataProvider to read test data from configuration file or database at runtime.
In this article we will introduce both of them with example step by step.
Pass Test Data In testng.xml
TestNG provide @Parameters annotation to import parameters from testng.xml to test method. In this example there are four parameters in this test case. They are used for database connection issue.
- ParameterTest.java (Test method should has same parameter name and count with definition in testng.xml)
public class ParameterTest { /* This test method will use four configured parameters in testng.xml file. * The parameters is used to connect to db via jdbc. */ @Test @Parameters({ "jdbcDriver" , "jdbcUrl", "jdbcUserName", "jdbcPassword" }) public void connectToDB(String jdbcDriver, String jdbcUrl, String jdbcUserName, String jdbcPassword) { System.out.println("jdbcDriver : " + jdbcDriver); System.out.println("jdbcUrl : " + jdbcUrl); System.out.println("jdbcUserName : " + jdbcUserName); System.out.println("jdbcPassword : " + jdbcPassword); Connection conn = null; try { Class.forName(jdbcDriver); conn = DriverManager.getConnection(jdbcUrl, jdbcUserName, jdbcPassword); }catch (Exception ex) { ex.printStackTrace(); }finally { if(conn!=null) { try { conn.close(); }catch(Exception ex) { ex.printStackTrace(); }finally { conn = null; } } } } }
- testng.xml (Define test parameters with name and value attribute.)
<suite guice-stage="DEVELOPMENT" name="Default suite"> <test verbose="2" name="Test DB Connection"> <parameter name="jdbcDriver" value="com.mysql.jdbc.Driver" /> <parameter name="jdbcUrl" value="jdbc:mysql://localhost:3306/dev2qaMysql" /> <parameter name="jdbcUserName" value="jerry zhao" /> <parameter name="jdbcPassword" value="dev2qa.com" /> <classes> <class name="com.dev2qa.testng.ParameterTest"/> </classes> </test> </suite>
- Output
jdbcDriver : com.mysql.jdbc.Driver jdbcUrl : jdbc:mysql://localhost:3306/dev2qaMysql jdbcUserName : jerry zhao jdbcPassword : dev2qa.com
Pass Test Data In Array Via DataProvider
- Use @DataProvider annotation to define a method which return a 2D object array. Give it a name at the same time. You can write java code in this method to import data from file or database.
@DataProvider(name = "dbConnSettings") public Object[][] provideDBSettings() { ...... }
- Set @Test annotation’s dataProvider attribute to the data provider just created.
@Test(dataProvider = "dbConnSettings") public void connectToDB() { ...... }
- DataProviderTest.java
public class DataProviderTest { /* Import test data from dataprovider with specified name. * Please note the input parameter type and count should match dataprovider provided row data array. * */ @Test(dataProvider = "dbConnSettings") public void connectToDB(String driver, String url, String uerName, String password) { System.out.println("Driver : " + driver + " , Url : " + url + " , UerName : " + uerName + " , Password : " + password); } /* DataProvider method read database connection settings data from a properties file. * Return 2D array, only one row and four column in the array. * */ @DataProvider(name = "dbConnSettings") public Object[][] provideDBSettings() { List<List<Object>> retList = new ArrayList<List<Object>>(); Properties properties = new Properties(); InputStream inputStream = null; try { /* Get database connection data from configure file. */ String dbSettingFilePath = "C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/testng/dbSettings.properties"; File dbSettingFile = new File(dbSettingFilePath); inputStream = new FileInputStream(dbSettingFile); properties.load(inputStream); String jdbcDriver = properties.getProperty("jdbcDriver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String jdbcUserName = properties.getProperty("jdbcUserName"); String jdbcPassword = properties.getProperty("jdbcPassword"); List<Object> rowList = new ArrayList<Object>(); rowList.add(jdbcDriver); rowList.add(jdbcUrl); rowList.add(jdbcUserName); rowList.add(jdbcPassword); retList.add(rowList); }catch(Exception ex) { ex.printStackTrace(); }finally { if(inputStream!=null) { try { inputStream.close(); }catch(Exception ex) { ex.printStackTrace(); } } return this.translateListToArray(retList); } } /* Translate a 2D List to 2D Object Array. */ private Object[][] translateListToArray(List<List<Object>> dataList) { Object ret[][] = null; if(dataList!=null) { /* Get row count. */ int rowSize = dataList.size(); if(rowSize>0) { List<Object> rowList = dataList.get(0); /* Get column count. */ int colSize = rowList.size(); if(colSize>0) { /* Init the 2D array with the row and column count.*/ ret = new String[rowSize][colSize]; /* Loop for rows. */ for(int i=0;i<rowSize;i++) { /* Loop the columns for each row. */ rowList = dataList.get(i); for(int j=0;j<colSize;j++) { /* Assign column data in one row.*/ ret[i][j] = rowList.get(j); } } } } } return ret; } }
- dbSettings.properties ( Please note this file should saved at C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/testng/dbSettings.properties in above java code).
jdbcDriver=oracle.jdbc.driver.OracleDriver jdbcUrl=jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa jdbcUserName=system jdbcPassword=manager
- Output
Driver : oracle.jdbc.driver.OracleDriver , Url : jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa , UerName : system , Password : manager PASSED: connectToDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa", "system", "manager")
Pass Test Data In Pojo Via DataProvider
We can make input parameter simple and clear by using Pojo(Plain object Java object) instead of object array.
- DBSettingDTO.java (This is just the pojo which transfer db settings information between dataprovider and test method.)
/* This pojo used to save jdbc connection data.*/ public class DBSettingDTO { private String jdbcDriver; private String jdbcUrl; private String jdbcUserName; private String jdbcPassword; public String getJdbcDriver() { return jdbcDriver; } public void setJdbcDriver(String jdbcDriver) { this.jdbcDriver = jdbcDriver; } public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getJdbcUserName() { return jdbcUserName; } public void setJdbcUserName(String jdbcUserName) { this.jdbcUserName = jdbcUserName; } public String getJdbcPassword() { return jdbcPassword; } public void setJdbcPassword(String jdbcPassword) { this.jdbcPassword = jdbcPassword; } }
- DataProviderPoJoTest.java
public class DataProviderPoJoTest { /* Import test data from dataprovider with specified name. * Please note the input parameter is just a PoJo now. * Because we return a one row one column array and only one PoJo in the column. * */ @Test(dataProvider = "dbConnSettings") public void connectToDB(DBSettingDTO dbSettingDto) { String driver = dbSettingDto.getJdbcDriver(); String url = dbSettingDto.getJdbcUrl(); String uerName = dbSettingDto.getJdbcUserName(); String password = dbSettingDto.getJdbcPassword(); System.out.println("DB info in PoJo. Driver : " + driver + " , Url : " + url + " , UerName : " + uerName + " , Password : " + password); } /* Read database connection settings data from a properties file. * Return 2D array, one row and one column in the array. * The object in the column is an instance of DBSettingDTO. * */ @DataProvider(name = "dbConnSettings") public Object[][] provideDBSettings() { DBSettingDTO dbSettingDto = new DBSettingDTO(); Properties properties = new Properties(); InputStream inputStream = null; try { /* Get database connection data from configure file. */ String dbSettingFilePath = "C:/WorkSpace/dev2qa.com/Code/src/com/dev2qa/testng/dbSettings.properties"; File dbSettingFile = new File(dbSettingFilePath); inputStream = new FileInputStream(dbSettingFile); properties.load(inputStream); String jdbcDriver = properties.getProperty("jdbcDriver"); String jdbcUrl = properties.getProperty("jdbcUrl"); String jdbcUserName = properties.getProperty("jdbcUserName"); String jdbcPassword = properties.getProperty("jdbcPassword"); dbSettingDto = new DBSettingDTO(); dbSettingDto.setJdbcDriver(jdbcDriver); dbSettingDto.setJdbcUrl(jdbcUrl); dbSettingDto.setJdbcUserName(jdbcUserName); dbSettingDto.setJdbcPassword(jdbcPassword); }catch(Exception ex) { ex.printStackTrace(); }finally { if(inputStream!=null) { try { inputStream.close(); }catch(Exception ex) { ex.printStackTrace(); } } return new Object[][] {{dbSettingDto}}; } } }
- Output
DB info in PoJo. Driver : oracle.jdbc.driver.OracleDriver , Url : jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa , UerName : system , Password : manager PASSED: connectToDB(com.dev2qa.testng.DBSettingDTO@685cb137)
Pass Test Data In Map Object Via DataProvider
You should treate Map object as a kind of pojo. Then the java code is same as above.
Return Test Data By Test Method Name In DataProvider
Below data provider return different test data to different test method by test method name. The Method input parameter is used to check which method call this data provider.
- DataProviderByMethodNameTest.java
public class DataProviderByMethodNameTest { @Test(dataProvider = "testDataProvider") public void testDB(String driver, String url, String userName, String password) { System.out.println("DB info : driver = " + driver + " , url = " + url + " , userName = " + userName + " , password = " + password); } @Test(dataProvider = "testDataProvider") public void testEmail(String email, String password) { System.out.println("Email info : email = " + email + " , password = " + password); } @DataProvider(name = "testDataProvider") public Object[][] provideData(Method method) { Object[][] result = null; if (method.getName().equals("testDB")) { result = new Object[][] { { "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/dev2qaMysql", "jerry zhao" , "dev2qa.com" }, { "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa", "system" , "manager" } }; } else if (method.getName().equals("testEmail")) { result = new Object[][] { { "[email protected]", "666666" }, { "[email protected]", "888888" }, { "[email protected]", "999999" } }; } return result; } }
- Output
DB info : driver = com.mysql.jdbc.Driver , url = jdbc:mysql://localhost:3306/dev2qaMysql , userName = jerry zhao , password = dev2qa.com DB info : driver = oracle.jdbc.driver.OracleDriver , url = jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa , userName = system , password = manager Email info : email = [email protected] , password = 666666 Email info : email = [email protected] , password = 888888 Email info : email = [email protected] , password = 999999 PASSED: testDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/dev2qaMysql", "jerry zhao", "dev2qa.com") PASSED: testDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@//dev2qa.com:1521/Dev2qa", "system", "manager") PASSED: testEmail("[email protected]", "666666") PASSED: testEmail("[email protected]", "888888") PASSED: testEmail("[email protected]", "999999")
[download id=”1439″]