接口自动化测试实战核心代码(泛型重构测试方法,参数泛型化,精简代码)

需要知识,反射,泛型

package com.test.dataprovider;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.testng.Assert;




/**
 * @author chenya
 *
 *         May 18, 2018-11:28:50 AM
 */
public class CommonGeneric<T extends Object> {

	private static String urlpath;
	private static String excelpath;
	private static String resultexcel;
	private static String assertres;
	private static int index;
	private static boolean runresult;
	private static String loginfilepath;
	private static String user;

	public static String getUser() {
		return user;
	}

	public static void setUser(String user) {
		CommonGeneric.user = user;
	}

	public static String getPass() {
		return pass;
	}

	public static void setPass(String pass) {
		CommonGeneric.pass = pass;
	}

	private static String pass;

	public static String getLoginfilepath() {
		return loginfilepath;
	}

	public static void setLoginfilepath(String loginfilepath) {
		CommonGeneric.loginfilepath = loginfilepath;
	}

	public static <T> void CommonTest(List<T> list, String method) throws Exception {

		System.out.println("执行编号为" + list.get(CommonEnum.caseNo.getValue()) + "测试用例" + "--->"
				+ list.get(list.size() - CommonEnum.description.getValue()) + "--》预期结果-----》"
				+ list.get(list.size() - CommonEnum.expectation.getValue()));

		String tempStr = list.get(CommonEnum.caseNo.getValue()).toString().split("-v1.0-")[1];

		String str = tempStr;

		String newStr = str.replaceFirst("^0*", "");

		System.out.println("No:" + "【" + newStr + "】");

		runresult = (boolean) CommonGeneric
				.getMethodAndparamslist(Thread.currentThread().getStackTrace()[1].getClassName(), "CommonClass", list);

		if (runresult == true) {

	

		} else if (runresult == false) {


			Assert.assertTrue(runresult);

		} 

	}

	public static String getExcelpath() {
		return excelpath;
	}

	public static void setExcelpath(String excelpath) {
		CommonGeneric.excelpath = excelpath;
	}

	// get CommonClass
	public static <T extends Object> Object getMethodAndparamslist(String cla, String method, List<T> list)
			throws Exception {

		Class<?> clazz = Class.forName(cla);

		Object object = clazz.newInstance();

		Method Tmethod = clazz.getDeclaredMethod(method, List.class);

		Tmethod.setAccessible(true);

		return Tmethod.invoke(object, new Object[] { list });

	}
	
	public static  Map<String, String> CommonData(List<String> names) throws Exception {

		Map<String, String> mapforParams = new TreeMap<String, String>();

		names.forEach(n -> System.out.print(n + "\t"));

		for (int i = 3; i < names.size() - CommonEnum.description.getValue(); i++) {

			mapforParams.put(names.get(i), names.get(i + 1));

			i = i + 1;

		}
		return mapforParams;
		
	}

	public static List<String> CommonClass(List<String> values) throws Exception {

		List<String> Params = new ArrayList<>();

		values.forEach(n -> System.out.print(n + "\t"));

		for (int i = 1; i < values.size() - CommonEnum.description.getValue(); i++) {

			Params.add(values.get(i));


		}
		return Params;



	}

	// get excel data for CommonTest‘s list

	public static <T> List<Object> GetParams(Object... params) {

		List<Object> list = new ArrayList<>();

		synchronized (list) {

			for (Object t : params) {

				list.add(t);
			}

			return list;
		}

	}

	public static String getUrlpath() {

		return urlpath;
	}

	public static synchronized void setUrlpath(String urlpath) {

		CommonGeneric.urlpath = urlpath;
	}

	public static synchronized String getAssertres() {
		return assertres;
	}

	public static synchronized void setAssertres(String assertres) {
		CommonGeneric.assertres = assertres;
	}

	public static int getIndex() {
		return index;
	}

	public static synchronized void setIndex(int index) {
		CommonGeneric.index = index;
	}

	public static synchronized String getResultexcel() {
		return resultexcel;
	}

	public static synchronized void setResultexcel(String resultexcel) {
		CommonGeneric.resultexcel = resultexcel;
	}
}

 

 

package com.test.dataprovider;


public enum CommonEnum {

	caseNo("用例编号",0), 
	description("用例描述",4), 
	expectation("预期结果",3),
	methodname("接口名称",2),
	username("用户名",1);
	private String name;
	private int value;

	CommonEnum(String name, int value) {
		this.name = name;
		this.value = value;
	}

	public String getName() {

		return name;
	}

	public int getValue() {

		return value;
	}

	public void setName(String name) {

		this.name = name;

	}

	public void setValue(int value) {

		this.value = value;

	}
}
	@Test(dataProvider = "CommonData", dataProviderClass = FlyDataProviderClass.class)
	public void smslogin(T... params) throws Exception {
	
			CommonGeneric.CommonTest(2, CommonGeneric.GetParams(params),
					Thread.currentThread().getStackTrace()[1].getMethodName());

	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值