10

Gradle allows me to start multiple jvms for testing like so:

test {
   maxParallelForks = 10
}

Some of the tests for an application I have requires a fake ftp server which needs a port. This is quite easy to do with one jvm:

test {
   systemProperty 'ftpPort', 10000
}

However, when running in parallel I would need to start 10 fake ftp servers. How do I add a custom system property for each jvm spawned by gradle?

Something like:

test {
   maxParallelForks 10
   customizeForks { index ->
       systemProperty 'ftpPort', 10000 + index
   }
}
1
  • 1
    Do you really need an exclusive fake ftp server for each test? Or is it possible to use a random port between 10000 and 11000? Not very elegant, but easy to implement. Apr 13, 2019 at 18:39

1 Answer 1

1

There's no setup task to do before an after the fork. However, you can override the test task in your project to achieve the behavior (here in Java), as the Test task is simply a class:

  public class ForkTest extends org.gradle.api.tasks.testing.Test {
        private final AtomicInteger nextPort = new AtomicInteger(10000); 
        public Test copyTo(JavaForkOptions target) {
            super.copyTo(target);
            target.systemProperty("ftpPort", nextPort.getAndAIncrement());
            return this;
        }
    }

Then in your build.gradle :

task testFork(Type: ForkTest){
   forkEvery = 1
   maxParallelForks = 10
   ... 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.