Is it possible to register the names of function labels and clause expressions?
I want to be able to record function names and clause names when doing some automated tests. This will help with debugging test problems when using a mute browser for automation, phantomjs in particular. The reason is that phantomjs does not always behave the same as when using Chrome WebDriver. It would be nice if it was 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 great if I could use the pattern recognition method above to write these marks like this.
Login logout test
Go to login page
Submit username and password
logout
Returned to login page
source to share
Step1: Create Your Own Class Extender
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() {
}
}
Step 2: Create a RunListener that can listen for scrolling
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) {
}
}
Step 3: Register New Spock Extension
- In your classpath or resource path create the following folder structure
META-INF/services/org.spockframework.runtime.extension.IGlobalExtension
- Have this as file content
com.example.spock.exetension.MySpockExtension
Step 4: Run the 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
source to share
PiggyBacking at @Raghu Kirans replied, I had to do a little more to get this to work the way I wanted with Data Driven tests. In the BeforeIteration method of your RunListener, I did the following:
@Override
public void beforeIteration(IterationInfo iteration) {
Optional.of(iteration)
.map(feature -> iteration.getFeature())
.map(FeatureInfo::getBlocks)
.ifPresent( blocks -> blocks.forEach(
blockInfo -> log.info(blockInfo.getKind().name() + " : " + blockInfo.getTexts())));
}
This just prints out everything before each iteration. Also note that getKind (). The Name () on the BlockInfo object does not print out the given when and then the spook block in our test, instead it prints out SETUP, WHEN, THEN and WHERE instead. getTexts () will print the combined block texts.
Example:
: "I wake up" and: "I will have a cup of coffee"
Will display as
SETUP: ["I wake up", "I will have a cup of coffee"]
source to share
After searching continuously, I found this solution to get the test name. But it doesn't seem to find anything on the when and then labels. So far so good.
import org.junit.Rule
import org.junit.rules.TestName
class MySpec extends Specification {
@Rule TestName name = new TestName()
def "some test"() {
expect: name.methodName == "some test"
}
}
source to share
You can get the name of each function method by doing the 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)
}
}
source to share