Is there a way to execute several test methods in order using different data from the data provider?
e.g.
@DataProvider(name = "test1")
public Object[][] createData1() {
return new Object[][] {
{ "Cedric", new Integer(36) },
{ "Anne", new Integer(37)},
};
}
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
System.out.println(n1 + " " + n2);
}
@Test
public void verifyData2() {
System.out.println("Verify2");
}
And the output of running this would be
Cedric 36
Verify2
Anne 37
Verify2
Here is one way to do it: use an annotation to specify which method should receive which data:
public class A {
@DataProvider
public Object[][] dp(Method m) {
if (m.getAnnotation(Different.class) != null) {
return new Object[][] {
new Object[] { "different-a", "different-b" },
new Object[] { "different-c", "different-d" },
};
} else {
return new Object[][] {
new Object[] { "c", "d" },
new Object[] { "a", "b" },
};
}
}
@Test(dataProvider = "dp")
public void test1(String a, String b) {
System.out.println("test1: " + a + " " + b);
}
@Different
@Test(dataProvider = "dp")
public void test2(String a, String b) {
System.out.println("test2: " + a + " " + b);
}
}
The annotation:
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface Different {}
The output:
test1: c d
test1: a b
test2: different-a different-b
test2: different-c different-d