排列在接口测试中的应用

在接口测试工作中,数据的传入顺序,会影响到测试结果,于是需要对接口参数进行排列操作。可以使用排列组件Permutation对参数集合进行排列操作

引入组件依赖:

       <dependency>
            <groupId>org.raistlic.lib</groupId>
            <artifactId>commons-core</artifactId>
            <version>1.4</version>
        </dependency>


排列实例:

    @Test
    public void testName() throws Exception {
        List<String> list = Arrays.asList("a", "b", "c");
        for (List<String> tempList : Permutation.of(list, 2)) {
            System.out.println(tempList);
        }
        System.out.println("从3个不同元素中取出2个元素,排列数="+Permutation.of(list, 2).getPermutationCount());
    }


执行结果:

[a, b]
[b, a]
[a, c]
[c, a]
[b, c]
[c, b]
从3个不同元素中取出2个元素,排列数=6

业务实例:

  /**
     * 排列不同sku,验证创建结果
     * 业务约束:1.主商品已参加过附件促销不可继续创建;2.主品和附件库房属性必须一致
     * @param venderId  商家id
     * @param mainSkuId 主商品id
     * @param attachSkuId 附件商品id
     * @throws Exception
     */
    @Test(dataProvider = "skuInfo")
    @Description(description = "主品和附件顺序不同时创建附件促销,验证创建结果")
    public void testcreate_10(Long venderId,Long mainSkuId, Long attachSkuId) throws Exception {
        deleteAttachPromotionBySkuId(venderId,mainSkuId);
        PromoUtils.removePromotionBySkuId(GiftPromotionReadService.class, GiftPromotionWriteService.class, venderId, mainSkuId);

        Logger.log("设置促销信息");
        GiftPromotion giftPromotion = getGiftPromotion();

        Logger.log("设置主品和附件");
        List<PromotionSku> list = new ArrayList<PromotionSku>();
        list.addAll(buildPromoSkuList(BindType.MAIN, mainSkuId, 1));
        list.addAll(buildPromoSkuList(BindType.ATTACHMENT, attachSkuId, 1));
        giftPromotion.setPromotionSkus(list);


        Result<Long> create = this.invoke(attachPromotionWriteService, "create", Long.class, clientInfo, giftPromotion);

        boolean testResult = false;
        if (!create.isSuccess()) {
            if (create.getCode().equals("ware.attachment.stock.not.match")) {
                testResult = true;
                System.out.println("库房属性不一致");
            }else if (create.getCode().equals("attach.promo.already.exist")) {
                testResult = true;
                System.out.println("主品已经存在附件促销");
            }

            Assert.assertTrue(testResult);
        }else
            Assert.assertTrue(create.isSuccess());
    }


构造参数排列:

    public static Object[][] buildBasicDataList() throws Exception {
        HashMap<Long, List<Long>> map = new HashMap<Long, List<Long>>();
        map.put(35881L, Arrays.asList(10000132915L, 10000132916L, 10000132917L));
//        map.put(venderId, Arrays.asList(mainSkuId, attachSkuId1, attachSkuId2));


        Set<Map.Entry<Long, List<Long>>> entrySet = map.entrySet();
        List<Object[]> tempList = new ArrayList<Object[]>();
        for (Map.Entry<Long, List<Long>> entry : entrySet) {
            for (List<Long> list : Permutation.of(entry.getValue(), 2)) {
                List<Long> arrayList = new ArrayList<Long>();
                arrayList.add(entry.getKey());
                arrayList.addAll(list);
                System.out.println(arrayList);

                tempList.add(arrayList.toArray());
            }

        }


        Object[][] objects = new Object[tempList.size()][];
        for (int i = 0; i < tempList.size(); i++) {
            objects[i] = tempList.get(i);
        }


        System.out.println(objects.length);
        return objects;


    }


注入接口参数:

    @DataProvider(name = "skuInfo")
    public static Object[][] skuInfo() throws Exception {
        return buildBasicDataList();

    }


测试执行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值