Given:
Auto-tests(Java + TestNG + Allure).
Steps (@Step) - return nothing, all are void.
There are data-driven tests.
The task:
a. Do not execute the steps;
b. Get list of all tests;
c. For each test - get list of steps with parameters' values.
A straightforward solution:
1. Use aspects (AspectJ);
2. Link @Around to ("anyMethod() && withStepAnnotation()");
3. Use org.aspectj.lang.reflect to get steps annotations and parameters names and values;
4. Inside the @Around advice - get the information needed but do not execute intercepted methods, just return null.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.testng.ITestResult;
import org.testng.Reporter;
import ru.yandex.qatools.allure.annotations.Step;
...
@Pointcut("@annotation(ru.yandex.qatools.allure.annotations.Step)")
public void withStepAnnotation() {}
@Pointcut("execution( (..))")
public void anyMethod() {}
...
@Around("anyMethod() && withStepAnnotation()")
public Object logTestStepDataAndSkipExecution(ProceedingJoinPoint point) throws Throwable {
if (isDryRun) {
MethodSignature methodSignature = MethodSignature.class.cast(point.getSignature());
String paramNamesAndValues = getParamNamesAndValues(point, methodSignature);
Util.addStepDescriptionEntry(createTitle(point) + " | " + paramNamesAndValues);
return null;
} else {
return point.proceed();
}
}
...
private String getParamNamesAndValues(JoinPoint point, MethodSignature methodSignature) {
String[] paramNames = methodSignature.getParameterNames();
Object[] paramValues = point.getArgs();
StringBuilder sb = new StringBuilder();
for (int i=0; i< paramNames.length; i++) {
sb.append(paramNames[i]).append("=").append(paramValues[i]).append(" | ");
}
return sb.toString();
}
Links:
Mainly inspired by https://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html
Generally - https://duckduckgo.com/?q=Java+AspectJ+examples&t=ffab&ia=web
Auto-tests(Java + TestNG + Allure).
Steps (@Step) - return nothing, all are void.
There are data-driven tests.
The task:
a. Do not execute the steps;
b. Get list of all tests;
c. For each test - get list of steps with parameters' values.
A straightforward solution:
1. Use aspects (AspectJ);
2. Link @Around to ("anyMethod() && withStepAnnotation()");
3. Use org.aspectj.lang.reflect to get steps annotations and parameters names and values;
4. Inside the @Around advice - get the information needed but do not execute intercepted methods, just return null.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.testng.ITestResult;
import org.testng.Reporter;
import ru.yandex.qatools.allure.annotations.Step;
...
@Pointcut("@annotation(ru.yandex.qatools.allure.annotations.Step)")
public void withStepAnnotation() {}
@Pointcut("execution( (..))")
public void anyMethod() {}
...
@Around("anyMethod() && withStepAnnotation()")
public Object logTestStepDataAndSkipExecution(ProceedingJoinPoint point) throws Throwable {
if (isDryRun) {
MethodSignature methodSignature = MethodSignature.class.cast(point.getSignature());
String paramNamesAndValues = getParamNamesAndValues(point, methodSignature);
Util.addStepDescriptionEntry(createTitle(point) + " | " + paramNamesAndValues);
return null;
} else {
return point.proceed();
}
}
...
private String getParamNamesAndValues(JoinPoint point, MethodSignature methodSignature) {
String[] paramNames = methodSignature.getParameterNames();
Object[] paramValues = point.getArgs();
StringBuilder sb = new StringBuilder();
for (int i=0; i< paramNames.length; i++) {
sb.append(paramNames[i]).append("=").append(paramValues[i]).append(" | ");
}
return sb.toString();
}
Links:
Mainly inspired by https://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html
Generally - https://duckduckgo.com/?q=Java+AspectJ+examples&t=ffab&ia=web
Комментариев нет:
Отправить комментарий