Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 -Dtest=TestCircle#mytest test

Question: How can I do this with Spock?

This version has a dependency on Junit 4.12, it is stated in Junit documentation that this feature is supported only for Junit 4.x, basically Spock should propose something similar.

like image 629
Rafik EL YAAGOUBI Avatar asked Jan 27 '16 08:01

Rafik EL YAAGOUBI


1 Answers

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.

like image 65
Bill Chen Avatar answered Sep 30 '22 18:09

Bill Chen