编写一个以 Hashmap 作为集合的数据提供程序类并将其传递给 API 测试中的多个参数

我试图通过以 TestNG DataProvider 的形式编写可重用组件来最小化测试中的代码行。需要发送到服务器的我的测试规范接受 Map>。


 @Test(dataProvider = "provideData")

 public void TestMethod(Map<String,Object> map) throws Exception {

RequestSpecification spec = generateCommonReqSpecJsonWithQueryParams(map);

Response res = RestOperationUtils.get(url, spec, null);

}



@DataProvider(name="provideData")

    public static Object[][] getData() throws Exception {

        Map<String, ArrayList<String>> map = new HashMap<>();

        ArrayList<String> a1 = new ArrayList<>();

        a1.add("First Value");

        a1.add("Second Value);

        a1.add("Third Value");

        a1.add("Fourth Value");

        map.put("Test[]", a1);

        map.put("month_start", new ArrayList(Arrays.asList("2019-06-01")));

        map.put("month_end", new ArrayList(Arrays.asList("2019-06-30")));

        map.put("viewers[]", new ArrayList(Arrays.asList("ESPN")));

        ArrayList<String> b1 = new ArrayList<>();

        b1.add("Fifth Value");

        b1.add("Sixth Value");

        b1.add("Seventh Value");

        map.put("Result[]", b1);

由于 TestNG 要求我们从 DataProvider 返回 Object[][],以下是我尝试过的不同方法:


方法一:


String[] keys = new String[map.size()];

        ArrayList<ArrayList<String>> values = new ArrayList<>();

        int index = 0;

        for (Map.Entry<String, ArrayList<String>> mapEntry : map.entrySet()) {

            keys[index] = mapEntry.getKey();

            values.add(index, (ArrayList<String>) mapEntry.getValue());

            //   x[index] = mapEntry.getValue();

            index++;

        }

        Object[][] result = new Object[values.size()][];

        index = 0;

        int index2;

        for (List<String> list : values) {

            result[index] = new Object[list.size()];

            index2 = 0;

            for (String item : list) {

                result[index][index2] = item;

                index2++;

            }

            index++;

        }

return result ;



慕丝7291255
浏览 72回答 1
1回答

HUH函数

方法 3 对我来说似乎完全有效。object[][] 只是保存所有测试用例的一种方式,其中每个 object[] 索引只是测试用例。然后,每个测试用例都应与测试方法预期的参数数量和类型相匹配。选择 Object[][] 来保存测试用例,因为 java 中的所有对象要么从 Object 扩展,或者在基元的情况下可以自动装箱到它的对象形式中,它确实从 Object 扩展。然后,TestNG 将处理将数据提供者连接到它的每个测试,以及为每个测试应用和转换测试用例参数。例如:@Test(dataProvider="getTestCases")public void test(List<Integer> list, double d){&nbsp; &nbsp; // test code}会期待这样的事情:@DataProviderpublic Object[][] getTestCases(){&nbsp; &nbsp; return new Object[][] {&nbsp; &nbsp; &nbsp; &nbsp; {Arrays.asList(1, 2, 3), 1.0},&nbsp; &nbsp; &nbsp; &nbsp; {Arrays.asList(4, 5, 6), 2.0}&nbsp; &nbsp; };}其中 {Arrays.asList(1, 2, 3), 1.0} 是测试用例 1,{Arrays.asList(4, 5, 6), 2.0} 是测试用例 2。编辑:为了解决清理数据提供程序的代码更改,Holger 提出了以下内容:@DataProvider(name="provideData")public static Object[][] getData() {&nbsp; &nbsp; Map<String, List<String>> map = new HashMap<>();&nbsp; &nbsp; map.put("Test[]", Arrays.asList("First Value", "Second Value", "Third Value", "Fourth Value"));&nbsp; &nbsp; map.put("month_start", Arrays.asList("2019-06-01"));&nbsp; &nbsp; map.put("month_end", Arrays.asList("2019-06-30"));&nbsp; &nbsp; map.put("viewers[]", Arrays.asList("ESPN"));&nbsp; &nbsp; map.put("Result[]", Arrays.asList("Fifth Value", "Sixth Value", "Seventh Value"));&nbsp; &nbsp; return new Object[][]{{map}};}至于方法 1 和 2 对您不起作用的原因,它与 DataProvider 类型/返回测试用例数量不匹配有关。您的测试需要向其提供地图public void TestMethod(Map<String,Object> map)&nbsp;它需要Map<String,Object>来自数据提供者的一个类型参数,但是您正试图为方法 1 传递一个字符串和可变大小的字符串,或者为方法 2 传递一个字符串和字符串列表。两者的类型和参数数量都与一个不同地图。我建议将测试更改为 acceptMap<String, List<String>>以提高测试的清晰度,除非后者是被测试的功能本身所需要的。在测试中,简单是首选,因为如果你给测试本身增加了太多的复杂性。测试可能比它正在测试的东西更容易出错。因此,简单的地图返回应该就足够了。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java