首页 > 学院 > 开发设计 > 正文

实现testng监听器统一修改用例运行次数,随机执行测试用例

2019-11-06 09:02:21
字体:
来源:转载
供稿:网友
/** * @author :xinrui.wx * @version create time:Feb 23, 2017 7:45:23 PM description */public class CommonConst {	/*** 是否启用IMETHODINTERCEPTOR监听器的总开关 **/	public final static boolean ENABLE_IMETHODINTERCEPTOR_LISTENER = true;	/******* 重置测试用例的运行次数 ***********/	public final static boolean ENABLE_RESET_INVOCATION_COUNT = true;	public final static int INVOCATION_COUNT = 3;	public final static int WHEN_ORIGINAL_INVOCATION_COUNT_LESS_THAN = 1;	public final static int WHEN_ORIGINAL_INVOCATION_COUNT_EQUALS = 1;	public final static boolean FORCE_RESET_ALLTEST_INVOCATION_COUNT = false;	/***** 是否随机执行测试用例 根据测试用例执行次数随机 优先级高 ******/	public final static boolean RANDOM_EXCUTE_TEST_METHODS_BY_CACE_INVOCATION_COUNT = true;	/***** 是否随机执行测试用例 按测试用例方法的首字母随机排列 优先级低 ******/	public final static boolean RANDOM_EXCUTE_TEST_METHODS_BY_CACE_NAME = true;}
/** * @author :xinrui.wx * @version create time:Feb 23, 2017 7:09:30 PM description */public class TestMethodListener implements IMethodInterceptor {	@Override	public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {		if (methods == null || methods.isEmpty()) {			return methods;		}		if (!CommonConst.ENABLE_IMETHODINTERCEPTOR_LISTENER) {			return methods;		}		if (CommonConst.ENABLE_RESET_INVOCATION_COUNT) {			resetInvocationCount(methods);		}		if (CommonConst.RANDOM_EXCUTE_TEST_METHODS_BY_CACE_INVOCATION_COUNT) {			methods = randomSortTestMethodList(methods);		} else if (CommonConst.RANDOM_EXCUTE_TEST_METHODS_BY_CACE_NAME) {			/** 在多个用例每个用例运行多次的情况下无法完全随机, 只能随机测试方法执行顺序,单每个测试方法的多次运行无法随机打散 **/			Collections.shuffle(methods);		}		return methods;	}	public int getAnnotationInvocationCount(IMethodInstance iMethodInstance) {		return iMethodInstance.getMethod().getInvocationCount();	}	/****	 * 按测试用例运行的次数进行随机	 * 	 * @throws InterruptedException	 ***/	public List<IMethodInstance> randomSortTestMethodList(List<IMethodInstance> methods) {		List<IMethodInstance> tmpMethodsList = new ArrayList<IMethodInstance>();		List<IMethodInstance> newMethodsList = new ArrayList<IMethodInstance>();		int methodsListSize;		int index;		int methodInvocationCount;		IMethodInstance method = null;		for (IMethodInstance iMethodInstance : methods) {			methodInvocationCount = iMethodInstance.getMethod().getInvocationCount();			if (methodInvocationCount > 0) {				for (int i = 0; i < methodInvocationCount; i++) {					tmpMethodsList.add(iMethodInstance);				}			}		}		/** 报异常不允许使用 **/		// methods.clear();		methodsListSize = tmpMethodsList.size();		while (methodsListSize > 0) {			index = (TestHelper.getRandomIntData(1, methodsListSize) - 1);			method = tmpMethodsList.get(index);			method.getMethod().setInvocationCount(1);			newMethodsList.add(method);			tmpMethodsList.remove(index);			methodsListSize = methodsListSize - 1;		}		return newMethodsList;	}	public void resetInvocationCount(List<IMethodInstance> methods) {				int lessThan = CommonConst.WHEN_ORIGINAL_INVOCATION_COUNT_LESS_THAN;		int equals = CommonConst.WHEN_ORIGINAL_INVOCATION_COUNT_EQUALS;		if (!CommonConst.FORCE_RESET_ALLTEST_INVOCATION_COUNT) {			for (IMethodInstance iMethodInstance : methods) {				Test test = iMethodInstance.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);				System.out.PRintln(						"测试用例:" + iMethodInstance.getMethod().getMethodName() + " 原始运行次数:" + test.invocationCount());				if (!iMethodInstance.getMethod().getEnabled()) {					continue;				}								if (lessThan > 1 && equals > 1) {					if (getAnnotationInvocationCount(iMethodInstance) == equals && equals > lessThan) {						iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);					}					if (getAnnotationInvocationCount(iMethodInstance) <= lessThan) {						iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);					}					continue;				} else if (lessThan > 1) {					if (getAnnotationInvocationCount(iMethodInstance) <= lessThan) {						iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);						continue;					}				} else if (equals > 1) {					if (getAnnotationInvocationCount(iMethodInstance) == equals) {						iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);						continue;					}				} else {					if (getAnnotationInvocationCount(iMethodInstance) <= 1) {						iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);						continue;					}				}			}		} else {			for (IMethodInstance iMethodInstance : methods) {				iMethodInstance.getMethod().setInvocationCount(CommonConst.INVOCATION_COUNT);			}		}	}}

上一篇:AI与CV公司

下一篇:vs code 自动排版

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表