下面列出了怎么用org.testng.annotations.ITestAnnotation的API类实例代码及写法,或者点击链接到github查看源代码。
public static void setQAFDataProvider(ITestAnnotation testAnnotation, Method method) {
if ((null != method) && null != method.getParameterTypes() && (method.getParameterTypes().length > 0)) {
String dataProvider = testAnnotation.getDataProvider();
boolean hasDataProvider = isNotBlank(dataProvider);
// other than qaf data provider
if (hasDataProvider && !dataProvider.startsWith(QAFDataProvider.NAME)) {
// keep actual data-provider details with description
Map<String, String> desc = new HashMap<String, String>();
desc.put("description", testAnnotation.getDescription());
desc.put("dataProvider", testAnnotation.getDataProvider());
Class<?> dpClass = testAnnotation.getDataProviderClass();
if (null != dpClass) {
desc.put("dataProviderClass", dpClass.getName());
}
testAnnotation.setDescription(new JSONObject(desc).toString());
}
boolean globalParallelSetting = getBundle().getBoolean("global.datadriven.parallel", false);
boolean parallel = getBundle().getBoolean(method.getName() + ".parallel", globalParallelSetting);
dataProvider = parallel ? QAFDataProvider.NAME_PARALLEL : QAFDataProvider.NAME;
testAnnotation.setDataProvider(dataProvider);
testAnnotation.setDataProviderClass(QAFInetrceptableDataProvider.class);
}
}
@Override
public void transform(
final ITestAnnotation annotation,
final Class testClass,
final Constructor testConstructor,
final Method testMethod
) {
if (testMethod != null ) {
final DisableOnWindows disableOnWindows = testMethod.getAnnotation(DisableOnWindows.class);
if (disableOnWindows != null && Os.isMicrosoftWindows()) {
annotation.setEnabled(false);
LOG.info(String.format("Disabled: %s.%s - %s",
testMethod.getDeclaringClass().getName(),
testMethod.getName(),
disableOnWindows.reason()));
}
}
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
Class retry = annotation.getRetryAnalyzerClass();
if (retry != FailRetryAnalyzer.class) {
annotation.setRetryAnalyzer(FailRetryAnalyzer.class);
}
}
@Override
public void transform(ITestAnnotation testannotation, Class testClass,
Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = testannotation.getRetryAnalyzer();
if (retry == null) {
testannotation.setRetryAnalyzer(Retry.class);
}
}
@SuppressWarnings("rawtypes")
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = annotation.getRetryAnalyzer();
if (retry == null) {
annotation.setRetryAnalyzer(TestngRetry.class);
}
// 设置 默认循环次数
ConfigUtil property = ConfigUtil.getInstance();
int count = Integer.valueOf(property.getProperty("loopCount"));
LogUtil.info("默认每个方法循环" + count + "次");
annotation.setInvocationCount(count);
// 设置 需要特殊处理方法的循环次数
String excepLoopCount = property.getProperty("excepLoopCount");
String[] excepCount = excepLoopCount.split(";");
for (int i = 0; i < excepCount.length; i++) {
String[] temp = excepCount[i].split(",");
if (testMethod.getName().equals(temp[0])) {
LogUtil.info("该方法循环" + temp[1] + "次");
annotation.setInvocationCount(Integer.valueOf(temp[1]));
}
}
}
public void transform(
ITestAnnotation annotation,
Class testClass,
Constructor testConstructor,
Method testMethod ) {
annotation.setDependsOnMethods(null);
annotation.setDependsOnGroups(null);
}
protected static boolean isEnabled(Method m, IAnnotationFinder finder) {
ITestAnnotation annotation = AnnotationHelper.findTest(finder, m);
// If no method annotation, look for one on the class
if (null == annotation) {
annotation = AnnotationHelper.findTest(finder, m.getDeclaringClass());
}
return isEnabled(annotation);
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class);
// Enforce default test timeout
if (annotation.getTimeOut() == 0) {
annotation.setTimeOut(DEFAULT_TEST_TIMEOUT_MILLIS);
}
}
public void transform(final ITestAnnotation annotation, final Class testClass, final Constructor testConstructor,
final Method testMethod) {
IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer();
if (retryAnalyzer == null) {
annotation.setRetryAnalyzer(TestRetryAnalyzer.class);
}
}
@Override
public void transform(ITestAnnotation iTestAnnotation,
Class aClass, Constructor constructor, Method method) {
Class<? extends IRetryAnalyzer> retry = iTestAnnotation.getRetryAnalyzerClass();
if (retry == null) {
iTestAnnotation.setRetryAnalyzer(Retry.class);
}
}
@Override
@SuppressWarnings("rawtypes")
public void transform(
ITestAnnotation annotation,
Class testClass,
Constructor testConstructor,
Method testMethod) {
if (testMethod == null) {
return;
}
String className = testMethod.getDeclaringClass().getName();
String methodName = testMethod.getName();
TestDescription description = new TestDescription(className, methodName);
TestSelector matchingSelector = testSelectorList.findSelector(description);
if (!matchingSelector.isInclusive()) {
// For tests that have been filtered out, record it now and don't run it
if (shouldExplainTestSelectors) {
String reason = "Excluded by filter: " + matchingSelector.getExplanation();
results.add(TestResult.forExcluded(className, methodName, reason));
}
annotation.setEnabled(false);
return;
}
if (!annotation.getEnabled()) {
// on a dry run, have to record it now -- since it doesn't run, listener can't do it
results.add(TestResult.forDisabled(className, methodName));
return;
}
if (isDryRun) {
// on a dry run, record it now and don't run it
results.add(TestResult.forDryRun(className, methodName));
annotation.setEnabled(false);
return;
}
}
protected static boolean isEnabled(Class<?> objectClass, IAnnotationFinder finder) {
ITestAnnotation testClassAnnotation = AnnotationHelper.findTest(finder, objectClass);
return isEnabled(testClassAnnotation);
}
@Override
public void transform(
ITestAnnotation annotation,
Class testClass,
Constructor testConstructor,
Method testMethod) {}
@Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
testFilter.excludeTestOfImproperGroup(annotation);
}