Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created October 27, 2016 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krmahadevan/cc2c473a0a117d821b06c9ea793fb361 to your computer and use it in GitHub Desktop.
Save krmahadevan/cc2c473a0a117d821b06c9ea793fb361 to your computer and use it in GitHub Desktop.
Use Data Provider in Test Scenario [ Sample that backs my response for the SO thread : http://stackoverflow.com/a/40199773/679824
...
... TestNG 6.9.13.8 by Cédric Beust (cedric@beust.com)
...
[TestNG] Running:
/Users/krmahadevan/githome/PlayGround/playSelenium/src/test/resources/factory.xml
TestClass.testOne()
TestClass.testTwo()
TestClass.testOne()
TestClass.testTwo()
[Utils] Attempting to create /Users/krmahadevan/githome/PlayGround/testbed/test-output/factory-suite/factory-test.xml
[Utils] Directory /Users/krmahadevan/githome/PlayGround/testbed/test-output/factory-suite exists: true
PASSED: testOne
PASSED: testTwo
PASSED: testOne
PASSED: testTwo
===============================================
factory-test
Tests run: 4, Failures: 0, Skips: 0
===============================================
===============================================
factory-suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="factory-suite" verbose="2" group-by-instances="true" >
<test name="factory-test">
<classes>
<class name="org.rationale.emotions.TestClass"/>
</classes>
</test>
</suite>
package org.rationale.emotions;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class TestClass {
private int type = - 1;
private String name;
@Factory (dataProvider = "mock")
public TestClass(int type, String name) {
this.type = type;
this.name = name;
}
@DataProvider
public static Object[][] mock() {
return new Object[][] {{1, "Hello"}, {2, "World"}};
}
@Test (groups = {"A"})
public void testOne() {
System.out.println("TestClass.testOne()");
switch (type) {
case 1:
type = 1;
Assert.assertEquals(name, "Hello");
break;
case 2:
type = 2;
Assert.assertEquals(name, "World");
break;
default:
break;
}
}
@Test (groups = {"A"}, dependsOnMethods = "testOne")
public void testTwo() {
Assert.assertEquals(type != - 1, true);
System.out.println("TestClass.testTwo()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment