I am working on a project, which has some issues with tests. Basically, they all run quite slow and I was curious, how can I improve the performance of these tests?
Well, one improvement is simple – run tests in parallel. Of course, not all tests could be run in parallel, but some, especially unit tests, could be.
My project uses Junit Jupiter (aka Junit 5) framework. And this framework has an interesting feature, called “Parallel Execution“, which I used.
So, to use this feature, you need to:
- Create a file src/test/resources/junit-platform.properties with the following content:
# Absolute minimum - enable Parallel Execution
junit.jupiter.execution.parallel.enabled = true
# Run all tests in parallel for the project
# Disabling it in my project, as my Spring Boot tests were failing
# Try it in your project
#junit.jupiter.execution.parallel.mode.default = concurrent
- Simply run tests.
However, I was unable to run all my tests in parallel, as some of my Spring Boot integration tests were failing. Therefore, I’d enabled Concurrent execution for some of my tests by simply creating a base class with a required annotation and extending base class in my tests, which I wanted to run in parallel. Like so:
import org.junit.jupiter.api.parallel.Execution;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
@Execution(CONCURRENT)
public class BaseConcurrentTest {
}
And, that was it! Absolutely for free, I got a test execution performance improvement of ~ 10%. Nice!
As a side benefit, concurrent tests run before other tests. Therefore, I can get a response really quickly if my code stopped working as expected.
Be First to Comment