0

有没有办法使用来自数据提供者的不同数据来执行多个测试方法?

例如

@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");
}

运行它的输出将是
Cedric 36
Verify2
Anne 37
Verify2

4

2 回答 2

1

为什么不在该类中使用多个数据提供者?

@DataProvider(name = "test1")
public Object[][] createData1() {
    return new Object[][] {
        { "Cedric", new Integer(36) },
        { "Anne", new Integer(37)}, 
    };
}

@DataProvider(name = "test2")
public Object[][] createData2() {
    return new Object[][] {
        { "Amir", new Integer(42) },
        { "Alice", new Integer(39)}, 
    };
}


@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
    System.out.println(n1 + " " + n2);
}

@Test(dataProvider = "test2")
public void verifyData2(String n1, Integer n2) {
    System.out.println(n1 + " " + n2);
}
于 2012-06-07T21:41:53.213 回答
0

这是一种方法:使用注释来指定哪个方法应该接收哪些数据:

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);
  }
}

注释:

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface Different {}

输出:

test1: c d
test1: a b
test2: different-a different-b
test2: different-c different-d
于 2012-06-05T20:23:16.200 回答