我有测试:
import org.junit.jupiter.api.parallel.Execution // version 5.9.0
import org.junit.jupiter.api.parallel.ExecutionMode
import spock.lang.Specification
import spock.lang.Unroll
@Execution(ExecutionMode.CONCURRENT)
class ExampleTest extends Specification {
@Unroll
@Execution(ExecutionMode.CONCURRENT)
def "test1: should get valid #testParam"() {
System.out.println("FirstParallelUnitTest first() start => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
given:
def test = testParam
expect:
test != null
System.out.println("FirstParallelUnitTest first() end => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
where:
testParam << ["one", "two", "three", "four"]
}
@Unroll
@Execution(ExecutionMode.CONCURRENT)
def "test2: should get valid #testParam"() {
System.out.println("FirstParallelUnitTest first() start => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
given:
def test = testParam
expect:
test != null
System.out.println("FirstParallelUnitTest first() end => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
where:
testParam << ["one", "two", "three", "four"]
}
@Unroll
@Execution(ExecutionMode.CONCURRENT)
def "test3: should get valid #testParam"() {
System.out.println("FirstParallelUnitTest first() start => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
given:
def test = testParam
expect:
test != null
System.out.println("FirstParallelUnitTest first() end => " + Thread.currentThread().getName() +
" id: " + Thread.currentThread().getId());
where:
testParam << ["one", "two", "three", "four"]
}
}
我在src/test/resources
中创建了junit-platform.properties
文件,其中包含:
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default = concurrent
junit.jupiter.execution.parallel.mode.classes.default = concurrent
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=2
我也试过在gradle
文件中添加systemProperties
:
test {
testLogging {
exceptionFormat = 'full'
}
useJUnitPlatform()
systemProperties([
'junit.jupiter.execution.parallel.enabled': 'true',
'junit.jupiter.execution.parallel.mode.default': 'concurrent',
'junit.jupiter.execution.parallel.mode.classes.default': 'concurrent',
])
}
我使用gradle
版本7.6
。
我添加了junit
测试依赖项:
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testImplementation("org.junit.jupiter:junit-jupiter-params")
我也试着添加到build.gradle
:
subprojects {
tasks.withType(Test) {
maxParallelForks = Runtime.runtime.availableProcessors()
}
}
没有分割,因为我想看到i7-6600U
处理器(2核)的变化。
当运行测试时,我看到这些日志:
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
FirstParallelUnitTest first() start => Test worker id: 1
FirstParallelUnitTest first() end => Test worker id: 1
thread
id
不改变,或者start/end
的顺序不改变。
我如何并行运行这个测试和其他并行测试?
您应该使用@spock.lang.Execution
和org.spockframework.runtime.model.parallel.ExecutionMode
注释/类而不是JUnit 5注释/类。例如,您的导入可能如下所示:
import org.spockframework.runtime.model.parallel.ExecutionMode
import spock.lang.Execution
import spock.lang.Specification
import spock.lang.Unroll
您还应该启用"并行执行";特性,例如添加SpockConfig。Groovy和必要的配置(有不同的选项可用,请参考该页)。例如,您的配置可能如下所示:
runner {
parallel {
enabled true
}
}
我已经把这个文件放入测试资源目录(根包)。同时,阅读那一页上的注释,上面写着:
JUnit木星也支持并行执行,两者都依赖于JUnit平台的实现,但功能彼此独立。如果你在Spock中启用并行执行,它不会影响木星,反之亦然。
一般来说,"并行执行"&;这篇文章值得一读。
一旦我完成了上面的操作,我就可以并行运行测试方法了。下面是我的输出:
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-1 id: 17
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-6 id: 22
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-5 id: 20
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-6 id: 22
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-1 id: 17
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-5 id: 20
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-4 id: 21
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-2 id: 18
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-2 id: 18
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-4 id: 21
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-1 id: 17
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-6 id: 22
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-2 id: 18
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-5 id: 20
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-3 id: 19
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-6 id: 22
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-3 id: 19
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-2 id: 18
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-5 id: 20
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-1 id: 17
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-4 id: 21
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-4 id: 21
FirstParallelUnitTest first() start => ForkJoinPool-1-worker-2 id: 18
FirstParallelUnitTest first() end => ForkJoinPool-1-worker-2 id: 18
如果没有这些步骤,我的输出和你的一样顺序。