Run a specific test in a single test class with Spock and Maven

前端 未结 3 708
广开言路
广开言路 2021-01-05 01:40

I am using Spock framework for testing (1.0-groovy-2.4 release). Junit offers this option to run a specific test using the command line (with Maven):

mvn -Dt         


        
3条回答
  •  礼貌的吻别
    2021-01-05 01:54

    I use:

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

    It works perfectly with surefire 2.19.1 and junit 4.8.1. The following is the code example and the execution output:

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

    Hope this help.

提交回复
热议问题