7

I am using Allure2 with TestNG. I want to write my own listener which prints @Steps in the console output.

I saw the interface "StepLifecycleListener" in allure but I am not able to implement this listener in TestNg. Any pointers ?

@Override
public void beforeStepStart(final StepResult result) {
    System.out.format("Starting step: %s", result.getName());

}


@Override
public void afterStepStop(final StepResult result) {
    System.out.format("Completed step: %s", result.getName());

}
Max Voitko
  • 1,542
  • 1
  • 17
  • 32
user3290656
  • 339
  • 1
  • 3
  • 10

2 Answers2

15

Allure 2 listeners are managed by SPI mechanism. So there're several steps you need to do to make it works:

  • Implement StepLifecycleListener interface and override required methods.
  • Create META-INF/services folders in your project's resources root.
  • Create a new file by the full name of this interface in the above folder.
  • Add the full path to your implementation class into this file.

You can find an example in the the following project: https://github.com/sskorol/allure2-testng-report/blob/master/src/test/resources/META-INF/services/io.qameta.allure.listener.StepLifecycleListener

More info about SPI: http://docs.oracle.com/javase/tutorial/sound/SPI-intro.html

Serhii Korol
  • 843
  • 7
  • 15
0

You could easily achieve that with AspectJ as well.

public class StepAspect {
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @SuppressWarnings("EmptyMethod")
    @Pointcut("@annotation(io.qameta.allure.Step)")
    public void withStepAnnotation() {
        //pointcut body, should be empty
    }

    @SuppressWarnings("EmptyMethod")
    @Pointcut("execution(* *(..))")
    public void anyMethod() {
        //pointcut body, should be empty
    }

    @Before("anyMethod() && withStepAnnotation()")
    public void stepStart(final JoinPoint joinPoint) {
        if (logger.isInfoEnabled()) {
            final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            final Step step = methodSignature.getMethod().getAnnotation(Step.class);
            logger.info("Step: {}: {}", joinPoint.getSignature()
                    .toShortString(), AspectUtils.getName(step.value(), joinPoint));
        }
    }
}