当前位置: 首页 > 知识库问答 >
问题:

使用Spock和Maven在单个测试类中运行特定测试

翟俊
2023-03-14

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

mvn -Dtest=TestCircle#mytest test

问题:我怎样才能和斯波克做到这一点?

这个版本依赖于Junit 4.12,Junit文档中指出,只有Junit 4才支持此功能。x、 基本上,斯波克应该提出类似的建议。

共有3个答案

苍和裕
2023-03-14

你试过这个吗(https://groups.google.com/forum/#!主题/spockframework/KVDkA9QxC4U):

Windows: mvn -Dtest="TestCircle#my test" test 
*Nix: mvn "-Dtest=TestCircle#my test" test 
廖臻
2023-03-14

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

鉴于以下斯波克测试案例:

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>

它作为Maventest阶段的一部分运行良好:

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

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

如果我们在上面的HelloSec示例测试中执行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,而不是真正友好的名称。

这一点在这个答案和《史波克》的作者彼得·尼德维泽的评论中也得到了证实,因此这是一个非常可靠的来源。

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

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之间的粘合,因此执行失败。

司徒俊良
2023-03-14

我使用:

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测试“-Dtest=HelloSec#向斯波克问好”

[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测试“-Dtest=HelloSec#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=HelloSec#说你好”

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

希望这有帮助。

 类似资料:
  • 在以前的一个项目中,我使用Spock测试框架对Java代码进行单元测试。我发现这非常有效,所以我尝试将Spock测试添加到我当前的项目中,该项目使用Maven作为构建工具(前一个项目使用Gradle)。虽然我可以让Maven编译我的Spock测试(使用),但我无法让Maven运行这些测试。 我做了一个简单的例子来演示我在两个文件中的问题: null 当我执行(或)时,我希望运行单个单元测试并失败。

  • 我正在尝试用Maven/Intellij运行spock测试。但是maven和intellij都没有接受测试类。它肯定会拾取类,但不会在类中执行任何测试。 2)Surefire插件配置正确,因为它会拾取文件进行测试 3)target/test-classs文件夹中生成的测试类 我需要帮助我在这里错过了什么。

  • 我创建了maven项目,它由junit和spock测试组成。两个测试的代码。 在我的本地机器中,当我运行mvn测试时,它会欺骗两个测试类。我在git仓库上部署了项目,并为maven项目配置了jenkins。我推这个项目的存储库和执行作业,但是詹金斯只检测JUnit测试的AppTest类。我已经改变了pom.xml并添加了文件regadring到。我的项目结构。 文件组织的内容。spockframe

  • 我正在通过Maven运行测试用例。使用以下命令: 获取成功: 获取失败: 类名为LoginTest 堆栈跟踪: [错误]无法执行目标org.apache.maven.plugins:maven-surefire-plugin:2.12.4:project Web上的test(default-test):未执行任何测试!(设置-DFAILIFNOTESTS=FALSE忽略此错误。)->[帮助1]or

  • 问题内容: 当我使用以下命令在Maven中运行单个测试时: 我得到以下结果: 它在几分钟前工作了,但是现在由于某种原因停止了工作。在运行测试之前,我尝试运行几次,但没有帮助。 测试看起来像这样: }} 更新: 这是由于将此依赖项添加到POM: 当我删除它时,一切正常。即使我添加以下两个依赖关系而不是上一个依赖关系,一切也都可以正常工作: 真奇怪 问题答案: 您可能正在类路径上的某个地方拾取了JUn

  • 只启动jUnit测试和集成测试。从不执行。 下面是Maven输出的一部分: 我在StackOverflow上发现了几乎相同或相似的问题,但在我的项目中没有一个响应起作用。 null