Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to log spock feature method names and clause labels?

Tags:

spock

geb

I'd like to be able to log the spock feature names and clause labels when running some automated tests. This would help with debugging test issues when using a headless browser for automation, specifically phantomjs. Reason being, phantomjs does not always behave the same way as when using the chrome WebDriver. It would also be nice to have if this is even possible.

def "Login logout test"(){
    given: "Go to login page"
        ...

    when: "Submit username and password"
        ...
    then: "Dashboard page displayed"
        ...

    when: "logout"
        ...
    then: "Returned to login page"
        ...
}

For example, It would be cool if I could get the above sample spock feature method to log the labels like this.

Login logout test
Go to login page
Submit username and password
logout
Returned to login page
like image 331
WontonJon Avatar asked Jul 01 '16 12:07

WontonJon


2 Answers

Step1: Create a Your own spock extension Class

package com.example.spock.exetension;
public class MySpockExtension implements IGlobalExtension {
    @Override
    public void start() {
    }

    @Override
    public void visitSpec(SpecInfo spec) {
        spec.addListener(new MyCustomSpockRunListener());
    }

    @Override
    public void stop() {

    }
}

Step2: Create a RunListener that can listen to a spock run

package com.example.spock.exetension;
public class MyCustomSpockRunListener extends AbstractRunListener {

    private boolean specFailed;
    private boolean featureFailed;
       @Override
    public void beforeSpec(SpecInfo spec) {
        // TODO Auto-generated method stub
        specFailed = false;
    }
    @Override
    public void beforeFeature(FeatureInfo feature) {
        // TODO Auto-generated method stub
        featureFailed = false;
    }
    @Override
    public void beforeIteration(IterationInfo iteration) {

    }
    @Override
    public void afterIteration(IterationInfo iteration) {
    }
    @Override
    public void afterFeature(FeatureInfo feature) {
        // TODO Auto-generated method stub
        for ( BlockInfo block : feature.getBlocks() ) {
            System.out.println(block.getKind().name() + " : " + block.getTexts() ); 
        }
    }
    @Override
    public void afterSpec(SpecInfo spec) {
        // TODO Auto-generated method stub
        System.out.println(spec.getName() + " : STATUS : " + specFailed != null ? "failure":"success");

    }
    @Override
    public void error(ErrorInfo error) {
        specFailed = true;
        FeatureInfo feature = error.getMethod().getFeature();
        if (feature != null) {
            featureFailed = true;
            System.out.println(error.getMethod().getName() + " : " + error.getException());
        }else {
        }
    }
    @Override
    public void specSkipped(SpecInfo spec) {
    }
    @Override
    public void featureSkipped(FeatureInfo feature) {
    }
}

Step3: Register your new Spock extension

  • In your classpath or resource path create a below folder structure META-INF/services/org.spockframework.runtime.extension.IGlobalExtension
  • Have this as the content of file com.example.spock.exetension.MySpockExtension

Step4: Run your spock test and you should see output something like this.

given: "Go to login page"
when: "Submit username and password"
then: "Dashboard page displayed"
when: "logout"
then: "Returned to login page"
Login logout test : STATUS : success
like image 81
Raghu Kiran Avatar answered Oct 14 '22 15:10

Raghu Kiran


You can get the name of every feature method by following :

import spock.lang.Specification
import org.junit.Rule
import org.junit.rules.TestName
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class MySpec extends Specification{
    private static Logger logger = LoggerFactory.getLogger(ClassName.class)
    @Rule TestName testName = new TestName()

    void setup(){
       def featureMethodName = testName.methodName
       logger.info("feature method : " + featureMethodName)
    }
}
like image 21
Afzal Avatar answered Oct 14 '22 16:10

Afzal