2016-01-27 24 views
5

私はSpockフレームワーク(1.0-groovy-2.4リリース)を使用しています。 JUnitは(Mavenを持つ)、コマンドラインを使用して、特定のテストを実行するには、このoptionを提供しています:SpockとMavenを使用して単一のテストクラスで特定のテストを実行してください

mvn -Dtest=TestCircle#mytest test 

質問:どのように私はスポックでこれを行うことができますか?

このバージョンでは、JUnitの4.12に依存している、それはこの機能はのみJUnitの4.xのでサポートされていることをJUnitのドキュメントに記載されている、基本的にスポックは似たものを提案している必要があります。

答えて

7

を、答えはおそらくです:いいえ、あなたはそのシュアプラグイン機能を使用して、スポックと、特定のテストメソッドを呼び出すことはできません。その理由の下に。

import spock.lang.Specification 

class HelloSpec extends Specification { 

    def hello = new Main(); 

    def sayHello() { 
     given: "A person's name is given as a method parameter." 
     def greeting = hello.sayHello("Petri"); 

     expect: "Should say hello to the person whose name is given as a method parameter" 
     greeting == "Hello Petri"; 

     println "hello from HelloSpec" 
    } 
} 

そして、以下のプラグインの設定与えられた:それはmvn clean testを実行するMavenのtest相の一部として正常に動作

<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> 

を:

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running com.sample.HelloSpec 
hello from HelloSpec 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.314 sec - in com.sample.HelloSpec 
次スポックのテストケースを考えると

mvn clean test -Dtest=HelloSpec上記と全く同じ結果が得られ、正常に実行されます。

なぜ、私たちは1つの方法を実行できないのですか?

我々は次のような出力を得る上で、我々はHelloSpecのサンプルテストにjavapコマンドを実行した場合:

Compiled from "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); 
} 

スポックができるようにするために、メソッド名が変更されるためので、生成されたバイトコードには何のsayHello方法は、ありませんそれらのスペース。だからあなたが書いたメソッド名は実際にはコンパイルされたクラスの一部として実際のメソッド名ではありません。

この場合、メソッド名はおそらく$spock_feature_0_0であり、本当に親切ではありません。

これはまたthis answerと実際にスポックの著者のPeter Niederwieserのコメントで確認されていますので、信頼できるソース以上です。

あなたは次のことを実行してみてください:確かに本当のメソッド名と一致する必要があります

mvn clean test -Dtest=HelloSpec#*spock* 

ていますが、しかし、エラー

------------------------------------------------------- 
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) from [email protected] 
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:35) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:275) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:149) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) 
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) 
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) 
    at 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  

になるだろうこれは、直接呼び出しに最も可能性が高いです私たちはJUnitとSpockの間の接着剤を迂回しており、そのような実行は失敗します。

+0

素敵な証拠。ここでSpockはそれがサポートしている素敵な命名規則のために敬意を表しています。 –

5

私が使用します。

Windows: mvn -Dtest="TestCircle#my test" test 
*Nix: mvn "-Dtest=TestCircle#my test" test 

それは確実な2.19.1とJUnit 4.8.1で完璧に動作します。

[INFO] Scanning for projects...                     
[INFO]                           
[INFO] ------------------------------------------------------------------------         
[INFO] Building Auto Test 1                    
[INFO] ------------------------------------------------------------------------         
[INFO]                           
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ auto-test ---      
[INFO] Using 'UTF-8' encoding to copy filtered resources.               
[INFO] Copying 5 resources                      
[INFO]                           
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ auto-test ---       
[INFO] No sources to compile                      
[INFO]                           
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ auto-test ---    
[INFO] Using 'UTF-8' encoding to copy filtered resources.               
[INFO] Copying 122 resources                      
[INFO]                           
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ auto-test ---     
[INFO] No sources to compile                      
[INFO]                           
[INFO] --- gmaven-plugin:1.4:testCompile (compile-test) @ auto-test ---         
[INFO] Compiled 42 Groovy classes                     
[INFO]                           
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ auto-test ---        

------------------------------------------------------- 
T E S T S            
------------------------------------------------------- 
Running HelloSpec          
say hi to spock from HelloSpec       
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.558 sec - in HelloSpec 

Results : 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS               
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 17.169s              
[INFO] Finished at: Fri Jul 01 14:19:20 CST 2016        
[INFO] Final Memory: 31M/736M             
[INFO] ------------------------------------------------------------------------ 

#1 MVN試験「-Dtest =

import spock.lang.Specification 

class HelloSpec extends Specification { 
    def sayHello() { 
     given: "A person's name is given as a method parameter." 
     def greeting = "Hello Petri"; 


     expect: "Should say hello to the person whose name is given as a method parameter" 
     greeting == "Hello Petri"; 

     println "hello from HelloSpec" 
    } 

    def sayHi() { 
     expect: 
     1==1 

     println "sayHi from HelloSpec" 

    } 

    def "say hi to spock"() { 
     expect: 
     true 
     println "say hi to spock from HelloSpec" 

    } 
} 

#1 MVN試験 "-Dtest = HelloSpec#言うHIスポックする": は、次のコード例と実行出力でありますHelloSpec#sayHi "

[INFO] Scanning for projects...                   
[INFO]                         
[INFO] ------------------------------------------------------------------------       
[INFO] Building Auto Test 1                  
[INFO] ------------------------------------------------------------------------       
[INFO]                         
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ auto-test ---    
[INFO] Using 'UTF-8' encoding to copy filtered resources.            
[INFO] Copying 5 resources                    
[INFO]                         
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ auto-test ---    
[INFO] No sources to compile                   
[INFO]                         
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ auto-test ---  
[INFO] Using 'UTF-8' encoding to copy filtered resources.            
[INFO] Copying 122 resources                   
[INFO]                         
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ auto-test ---  
[INFO] No sources to compile                   
[INFO]                         
[INFO] --- gmaven-plugin:1.4:testCompile (compile-test) @ auto-test ---       
[INFO] Compiled 42 Groovy classes                  
[INFO]                         
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ auto-test ---      

------------------------------------------------------- 
T E S T S            
------------------------------------------------------- 
Running HelloSpec          
sayHi from HelloSpec         
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.539 sec - in HelloSpec 

Results : 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS               
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 12.187s              
[INFO] Finished at: Fri Jul 01 14:19:49 CST 2016        
[INFO] Final Memory: 31M/736M             
[INFO] ------------------------------------------------------------------------ 

#MVNテスト" -Dtest = HelloSpe C#のsayHello」

[INFO] Scanning for projects...                   
[INFO]                          
[INFO] ------------------------------------------------------------------------       
[INFO] Building Auto Test 1                  
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ auto-test --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 5 resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ auto-test --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ auto-test --- 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] Copying 122 resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ auto-test --- 
[INFO] No sources to compile 
[INFO] 
[INFO] --- gmaven-plugin:1.4:testCompile (compile-test) @ auto-test --- 
[INFO] Compiled 42 Groovy classes 
[INFO] 
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ auto-test --- 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running HelloSpec 
hello from HelloSpec 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.515 sec - in HelloSpec 

Results : 

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 11.170s 
[INFO] Finished at: Fri Jul 01 14:20:14 CST 2016 
[INFO] Final Memory: 31M/736M 
[INFO] ------------------------------------------------------------------------ 

・ホープこのヘルプ。

+0

クール。これは不可能であると主張した他の答えを反証しているようです。 – MarkHu

+0

Gradleからもこれをやることができるのだろうかと思います。 – MarkHu

+1

私のテストでは、 'maven-surefire-plugin'のバージョン2.18.1、新しいバージョン[' 2.19'](https://issues.apache.org/jira/browse/SUREFIRE-745?jql=project% 20%3D20%20%20%20%20%3D20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20% .apache.org/jira/browse/SUREFIRE-1185?jql =プロジェクト%20%3D%20SUREFIRE%20AND%20fixVersion%20%3D%202.19.1%20DER%20BY%20DESC%2C%20優先順位%20DESC%2C %20created%20ASC)は '-Dtest'プロパティのいくつかの問題を明らかに修正しました。これは' 2.19.1'でなぜあなたのために働いたのかを説明するかもしれません。 –

関連する問題