testing - Run a specific test in a single test class with Spock and Maven -
i using spock framework testing (1.0-groovy-2.4 release). junit offers option run specific test using command line (with maven):
mvn -dtest=testcircle#mytest test
question: how can spock?
this version has dependency on junit 4.12, stated in junit documentation feature supported junit 4.x, spock should propose similar.
after further investigation, answer probably: no, can't invoke specific test methods spock using surefire plugin feature. below reason.
given following spock test case:
import spock.lang.specification class hellospec extends specification { def hello = new main(); def sayhello() { given: "a person's name given method parameter." def greeting = hello.sayhello("petri"); expect: "should hello person name given method parameter" greeting == "hello petri"; println "hello hellospec" } }
and given following plugins configuration:
<plugin> <groupid>org.codehaus.gmavenplus</groupid> <artifactid>gmavenplus-plugin</artifactid> <version>1.5</version> <executions> <execution> <goals> <goal>addtestsources</goal> <goal>testcompile</goal> </goals> </execution> </executions> <configuration> <sources> <fileset> <directory>${pom.basedir}/src/test/java</directory> <includes> <include>**/*.groovy</include> </includes> </fileset> </sources> </configuration> </plugin> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-surefire-plugin</artifactid> <version>2.18.1</version> <configuration> <includes> <include>**/*test.java</include> <include>**/*spec.java</include> </includes> </configuration> </plugin>
it runs fine part of maven test
phase executing mvn clean test
:
------------------------------------------------------- t e s t s ------------------------------------------------------- running com.sample.hellospec hello hellospec tests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 0.314 sec - in com.sample.hellospec
even executing mvn clean test -dtest=hellospec
same result above, execution.
so, why can't run single method?
if execute javap
command on hellospec sample test above following output:
compiled "hellospec.groovy" public class com.sample.hellospec extends spock.lang.specification implements groovy.lang.groovyobject { public static transient boolean __$stmc; public com.sample.hellospec(); public void $spock_feature_0_0(); protected groovy.lang.metaclass $getstaticmetaclass(); public groovy.lang.metaclass getmetaclass(); public void setmetaclass(groovy.lang.metaclass); public java.lang.object invokemethod(java.lang.string, java.lang.object); public java.lang.object getproperty(java.lang.string); public void setproperty(java.lang.string, java.lang.object); public java.lang.object gethello(); public void sethello(java.lang.object); }
so in generated bytecode there no sayhello
method, because spock changes methods name in order allow spaces in them. method name write never real method name part of compiled class.
in case, method name $spock_feature_0_0
, not friendly.
this confirmed in this answer , comments of peter niederwieser, author of spock actually, more reliable source.
you try running following:
mvn clean test -dtest=hellospec#*spock*
which should indeed match real method name, error
------------------------------------------------------- t e s t s ------------------------------------------------------- running com.sample.hellospec tests run: 1, failures: 0, errors: 1, skipped: 0, time elapsed: 0.007 sec <<< failure! - in com.sample.hellospec initializationerror(org.junit.runner.manipulation.filter) time elapsed: 0.006 sec <<< error! java.lang.exception: no tests found matching method $spock_feature_0_0(com.sample.hellospec) org.junit.internal.requests.classrequest@282ba1e @ org.junit.internal.requests.filterrequest.getrunner(filterrequest.java:35) @ org.apache.maven.surefire.junit4.junit4provider.execute(junit4provider.java:275) @ org.apache.maven.surefire.junit4.junit4provider.executewithrerun(junit4provider.java:173) @ org.apache.maven.surefire.junit4.junit4provider.executetestset(junit4provider.java:149) @ org.apache.maven.surefire.junit4.junit4provider.invoke(junit4provider.java:128) @ org.apache.maven.surefire.booter.forkedbooter.invokeproviderinsameclassloader(forkedbooter.java:203) @ org.apache.maven.surefire.booter.forkedbooter.runsuitesinprocess(forkedbooter.java:155) @ org.apache.maven.surefire.booter.forkedbooter.main(forkedbooter.java:103) results : tests in error: filter.initializationerror » no tests found matching method $spock_feature_0_... tests run: 1, failures: 0, errors: 1, skipped: 0
this because direct invocation name bypassing glue between junit , spock , such execution fails.
Comments
Post a Comment