Exclude specific tests from being run in parallel in jUnit

后端 未结 3 1153
不思量自难忘°
不思量自难忘° 2020-12-31 07:45

I recently stumbled upon a simple way to parallelize the execute of tests via jUnit by specifying the following in a java project\'s pom.xml file:



        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 07:56

    I'm a little late to the party here, and tried both of the previous answers, which work in some cases but don't provide a complete solution. Possibly relevant, I'm working with JUnit 5.

    @polaretto's answer suggests the jcip @NotThreadSafe annotation, which works with the surefire plugin in a build, but does not work with command line mvn test. @patson-luk's answer is on the right track, unfortunately by excluding the "bad test" in the default configuration, it remained excluded and was not run in the separate .

    I managed to get this working using the following configuration:

    For JUnit 5, these are sufficient

    In my src/main/resources/junit-platform.properties file:

    junit.jupiter.execution.parallel.enabled = true
    junit.jupiter.execution.parallel.mode.default = concurrent
    

    At the top of my not-thread-safe class (instead of the jcip annotation):

    import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
    
    @Execution(SAME_THREAD)
    class SingleThreadedTest {
       // ...
    }
    

    For JUnit 4, modify the accepted answer as follows:

    
      org.apache.maven.plugins
      maven-surefire-plugin
      ${maven-surefire-plugin.version}
      
        
        true
      
      
        
          single-thread-test
          test
          
            test
          
          
            
            
              **/SingleThreadedTest.java
            
            1
            false
            1
            false
          
        
        
          multi-thread-test
          test
          
            test
          
          
            
              **/SingleThreadedTest.java
            
            4
            true
            all
            true
            false
          
        
      
    
    

提交回复
热议问题