在接口测试工作中,数据的传入顺序,会影响到测试结果,于是需要对接口参数进行排列操作。可以使用排列组件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();
}
测试执行结果: