# Custom test fork action

**URL:** https://discuss.gradle.org/t/custom-test-fork-action/26078
**Category:** Help/Discuss
**Created:** [March 3, 2018, 6:25am UTC](https://discuss.gradle.org/t/custom-test-fork-action/26078 "2018-03-03T06:25:45Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [March 3, 2018, 6:25am UTC](https://discuss.gradle.org/t/custom-test-fork-action/26078/1 "2018-03-03T06:25:45Z")

</div>

There’s times where I’d like to perform an action for each test fork, for example give each fork a unique port number that can be referenced in the tests to avoid clashes with other forks.

Currently the only way I’ve found to do this is to extend the `Test` class and override `copyTo(JavaForkOptions target)`

Eg:

```gradle
public class Test2 extends org.gradle.api.tasks.testing.Test {
    private final AtomicInteger nextPort = new AtomicInteger(9000); 
    public Test copyTo(JavaForkOptions target) {
        super.copyTo(target);
        target.systemProperty("port", nextPort.getAndIncrement());
        return this;
    }
}

```

This works but requires me to disable the default `Test` task and configure a `Test2` task instance.

I’d like to request the following method added to `Test` task.

```gradle
public void beforeFork(Action<JavaForkOptions> action) 

```

This would make it much cleaner

```gradle
apply plugin: 'java' 
test {
    def nextPort = new AtomicInteger(9000); 
    beforeFork { target ->
        target.systemProperty("port", nextPort.getAndIncrement())
    } 
} 

```

Related Stack Overflow Q&A [here](https://stackoverflow.com/a/47757630/1089967)

---

<div class="post-metadata">

### Author: ![Kanti1702](https://avatars.discourse-cdn.com/v4/letter/k/ce7236/32.png) [@Kanti1702](https://discuss.gradle.org/u/Kanti1702)
#### Post date: [September 18, 2019, 2:16pm UTC](https://discuss.gradle.org/t/custom-test-fork-action/26078/2 "2019-09-18T14:16:20Z")

</div>

> [@Lance](#):
>
> target.systemProperty(“port”, nextPort.getAndIncrement());

This didn’t works for me.  
When I ran my test2 task, for every test class it gives me same port. I have 2 test classes to be run in parallel.

Here is my configuration:

> [@](#):
>
> ```
> test{
> enabled = false
> }
> 
> ```
> 
> task test2(Type: Test2){  
> maxParallelForks = 2  
> }

Command I use to run : gradle test2

Did I miss something?

---

<div class="post-metadata">

### Author: ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)
#### Post date: [September 18, 2019, 4:59pm UTC](https://discuss.gradle.org/t/custom-test-fork-action/26078/3 "2019-09-18T16:59:13Z")

</div>

You’ll have to set [forkEvery](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle.api.tasks.testing.Test:forkEvery) eg

```gradle
task test2(Type: Test2){
   forkEvery = 1
   ... 
}

```
