java TestNG:一个@Test有多个@DataProvider

7d7tgy0s  于 2023-03-21  发布在  Java
关注(0)|答案(5)|浏览(208)

我用TestNG来表示Eclipse
是否可以逐步地将两个数据提供程序分配给相同的测试函数?
我可以把两个供应商放在一个,但这不是我想要的。
我需要(不像在这个例子中)独立地生成数据。

@DataProvider(name = "dataSet1")
public Object[][] createDataX() {
    return new Object[][] { { 1, 1 }, { 2, 2 } };
}

@DataProvider(name = "dataSet2")
public Object[][] createDataY() {
    return new Object[][] { { 0, 0 }, { 3, 3 } };
}

我想给予两个供应商进行相同的测试。这可能吗?

@Test(dataProvider = "dataSet1") // ??? and "dataSet2" ???
public void testThisFunction(int val1, int val2) {
    boolean solution = oracle(val1,val2);
    assert (solution);
}
pprl5pva

pprl5pva1#

不需要,但您可以将这两个数据提供程序合并为一个,并将该数据提供程序指定为您的数据提供程序:

public Object[][] dp1() {
  return new Object[][] {
      new Object[] { "a", "b" },
      new Object[] { "c", "d" },
  };
}

public Object[][] dp2() {
  return new Object[][] {
      new Object[] { "e", "f" },
      new Object[] { "g", "h" },
  };
}

@DataProvider
public Object[][] dp() {
  List<Object[]> result = Lists.newArrayList();
  result.addAll(Arrays.asList(dp1()));
  result.addAll(Arrays.asList(dp2()));
  return result.toArray(new Object[result.size()][]);
}

@Test(dataProvider = "dp")
public void f(String a, String b) {
  System.out.println("f " + a + " " + b);
}
du7egjpx

du7egjpx2#

@DataProvider
public Object[][] combinedDataProvider() {
    // Using stream to combine the two separate data providers.
    return Stream.of(dp1(), dp2())
                 .flatMap(Arrays::stream)
                 .toArray(Object[][]::new);
}
n6lpvg4x

n6lpvg4x3#

请参考这个答案:
TestNG using multiple DataProviders with single Test method
它要干净得多,将适用于更复杂的事情。
也许有人也会需要它,我以不同的方式重写了这个方法public static T[] concatAll(T[] first, T[]... rest)

public static Object[] concat(Object[] first, Object[] second) {
    Object[] result = ArrayUtils.addAll(first, second);
    return result;
}
t5zmwmid

t5zmwmid4#

这是工作修复:

@DataProvider(name = "requestValidator")
public Object[][] getRequestValidationMap() {
    ResponseEntity clientDetailsResponse = ResponseUtil.successResponse(HttpStatus.OK,
            clientDetailsEntityDao);
    ResponseEntity failureResponse = ResponseUtil.handleErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR,
            ERRCONSTANT.CUSTOM_CODE.getErrorDescription(), ERRCONSTANT.CUSTOM_CODE.getCategory(),
            ERRCONSTANT.CUSTOM_CODE.getValue());
    return new Object[][] { { "client", clientDetailsResponse, 200, HttpServletResponse.SC_OK },
            { "error", failureResponse, 500, HttpServletResponse.SC_INTERNAL_SERVER_ERROR } };
}

在效用方法中

@Test(dataProvider = "requestValidator")
public void testMultipleArgs(String key, ResponseEntity requestValidatorResponse, int statusCodeExpected, int actualStatusCode) throws Exception {
        
}
disbfnqx

disbfnqx5#

是的,
你可以写@Test(dataProvider=“name_of_first_dataprovider,name_of_second_dataprovider”)

相关问题