Thursday, June 12, 2014

Data Provider using annotation

package DataProvider.Annotation;

public class CharUtils {
/**
* Convert the characters to ASCII value
*
* @param character character
* @return ASCII value
*/
public static int CharToASCII(final char character) {
return (int) character;
}

/**
* Convert the ASCII value to character
*
* @param ascii ascii value
* @return character value
*/
public static char ASCIIToChar(final int ascii) {
return (char) ascii;
}
}


package DataProvider.Annotation;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class CharUtilsTest {
 // data provided through annotation
@DataProvider
public Object[][] ValidDataProvider() {
return new Object[][]{
{ 'A', 65 },{ 'a', 97 },
{ 'B', 66 },{ 'b', 98 },
{ 'C', 67 },{ 'c', 99 },
{ 'D', 68 },{ 'd', 100 },
{ 'Z', 90 },{ 'z', 122 },
{ '1', 49 },{ '9', 57 }
};
}

@Test(dataProvider = "ValidDataProvider")
public void CharToASCIITest(final char character, final int ascii) {

  int result = CharUtils.CharToASCII(character);
  Assert.assertEquals(result, ascii);

}

@Test(dataProvider = "ValidDataProvider")
public void ASCIIToCharTest(final char character, final int ascii) {

  char result = CharUtils.ASCIIToChar(ascii);
  Assert.assertEquals(result, character);

}
}

package DataProvider.Annotation;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestParameterDataProvider {

@Test(dataProvider = "provideNumbers")
public void test(int number, int expected) {
Assert.assertEquals(number + 10, expected);
}

@DataProvider(name = "provideNumbers")
public Object[][] provideData() {

return new Object[][] {
{ 10, 20 },
{ 100, 110 },
{ 200, 210 }
};
}

}

package DataProvider.Annotation;

import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestParameterDataProviderITestContext {
// in case of ITestContext it consult with xml file for which group to
// provide which data
@Test(dataProvider = "dataProvider", groups = { "groupA" })
public void test1(int number) {
System.out.println("I am executing groupA");
Assert.assertEquals(number, 1);
}

@Test(dataProvider = "dataProvider", groups = "groupB")
public void test2(int number) {
Assert.assertEquals(number, 2);
}

@DataProvider(name = "dataProvider")
public Object[][] provideData(ITestContext context) {

Object[][] result = null;

// get test name
// System.out.println(context.getName());

for (String group : context.getIncludedGroups()) {

System.out.println("group : " + group);

if ("groupA".equals(group)) {
result = new Object[][] { { 1 } };
break;
} else {
result = new Object[][] { { 2 } };
}

}
// /Group is not included .So,it gave null value .
if (result == null) {
result = new Object[][] { { 2 } };
}
return result;

}

}



package DataProvider.Annotation;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestParameterDataProviderMap {

@Test(dataProvider = "dbconfig")
public void testConnection(Map<String, String> map) {

for (Map.Entry<String, String> entry : map.entrySet()) {
 System.out.println("[Key] : " + entry.getKey()
                              + " [Value] : " + entry.getValue());
}

}

@DataProvider(name = "dbconfig")
public Object[][] provideDbConfig() {
Map<String, String> map = readDbConfig();
return new Object[][] { { map } };
}

public Map<String, String> readDbConfig() {

Properties prop = new Properties();
InputStream input = null;
Map<String, String> map = new HashMap<String, String>();

try {
 input = getClass().getClassLoader().getResourceAsStream("db.properties");

 prop.load(input);

 map.put("jdbc.driver", prop.getProperty("jdbc.driver"));
 map.put("jdbc.url", prop.getProperty("jdbc.url"));
 map.put("jdbc.username", prop.getProperty("jdbc.username"));
 map.put("jdbc.password", prop.getProperty("jdbc.password"));

} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return map;

}

}


package DataProvider.Annotation;

import java.lang.reflect.Method;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestParameterDataProviderMethod {

@Test(dataProvider = "dataProvider")
public void test1(int number, int expected) {
Assert.assertEquals(number, expected);
}

@Test(dataProvider = "dataProvider")
public void test2(String email, String expected) {
Assert.assertEquals(email, expected);
}

@DataProvider(name = "dataProvider")
public Object[][] provideData(Method method) {

Object[][] result = null;

if (method.getName().equals("test1")) {
result = new Object[][] {
{ 1, 1 }, { 200, 200 }
};
} else if (method.getName().equals("test2")) {
result = new Object[][] {
{ "test@gmail.com", "test@gmail.com" },
{ "test@yahoo.com", "test@yahoo.com" }
};
}

return result;

}

}

testng.xml

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="test-parameter">

  <test name="example1">

<groups>
<run>
<include name="groupB" />
</run>
</groups>
 <!-- In this case DataProvider.Annotation.TestParameterDataProviderMap
 ,DataProvider.Annotation.TestParameterDataProviderMethod is not simultaneously executed only one is exceuted
  -->
<classes>
  <class
   name="DataProvider.Annotation.TestParameterDataProviderITestContext" />
    <class
   name="DataProvider.Annotation.TestParameterDataProviderMap" />
    <class
   name="DataProvider.Annotation.TestParameterDataProviderMethod" />
</classes>

  </test>

</suite>

No comments:

Post a Comment