【问题标题】:Run a specific test in a single test class with Spock and Maven使用 Spock 和 Maven 在单个测试类中运行特定测试
【发布时间】:2016-01-27 08:07:39
【问题描述】:

我正在使用Spock 框架进行测试(1.0-groovy-2.4 版本)。 Junit 提供此 option 以使用命令行(使用 Maven)运行特定测试:

mvn -Dtest=TestCircle#mytest test

问题:我怎样才能用 Spock 做到这一点?

这个版本依赖于Junit 4.12,在Junit文档中说明这个功能只支持Junit 4.x,基本上Spock应该提出类似的东西.

【问题讨论】:

    标签: maven testing groovy spock maven-surefire-plugin


    【解决方案1】:

    我使用:

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

    它与surefire 2.19.1 和junit 4.8.1 完美配合。 以下是代码示例和执行输出:

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

    # mvn test "-Dtest=HelloSpec#say hi to spock"

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

    # mvn test "-Dtest=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 test "-Dtest=HelloSpec#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] ------------------------------------------------------------------------
    

    希望对您有所帮助。

    【讨论】:

    • 酷。这似乎反驳了另一个声称这是不可能的答案。
    • 我的测试使用了maven-surefire-plugin2.18.1 版本,较新的版本2.192.19.1 显然修复了-Dtest 属性上的几个问题,这可以解释为什么它对@ 有效987654333@.
    • 是的,它适用于 2.19.1。这种方式对我来说似乎更自然: mvn test -Dtest="TestCircle#my test"
    【解决方案2】:

    经过进一步调查,答案很可能是:不,您不能使用 Spock 使用 Surefire 插件功能调用特定的测试方法。原因如下。

    给定以下 Spock 测试用例:

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

    并给出以下插件配置:

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

    它作为 Maven test 阶段执行 mvn clean test 的一部分运行良好:

    -------------------------------------------------------   
     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,也会得到与上面完全相同的结果,成功执行。

    那么,为什么我们不能运行一个方法呢?

    如果我们在上面的 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 更改方法名称是为了在其中允许空格。所以你写的方法名永远不是作为编译类的一部分的真正的方法名。

    在这种情况下,方法名称很可能是$spock_feature_0_0,不是很友好的东西。

    this answerPeter Niederwieser 的 cmets 也证实了这一点,实际上是 Spock 的作者,所以这是一个非常可靠的来源。

    您可以尝试运行以下命令:

    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 org.junit.internal.requests.ClassRequest@282ba1e  
        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 之间的粘合,因此执行失败。

    【讨论】:

    • 很好的证明。在这里,Spock 为它支持的良好命名约定付出了高昂的代价。
    • @Bill Chen 有办法!
    【解决方案3】:

    你有没有试过这个(https://groups.google.com/forum/#!topic/spockframework/KVDkA9QxC4U):

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

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多