How to set different working directory per junit test class

I’m trying to make each test class in my system test run in a different working directory:

task systemTest(type: Test) {
    def packageName = 'somepackage/name'
      beforeTest({ testDescriptor ->
        mkdir td.getClassName()
        setWorkingDir
td.getClassName()
    })
      ext.suiteClasses = [
        "suiteOne**",
        "suiteTwo**",
    ]
      ext.suiteClasses.each { suite ->
        include packageName + suite
    }
}

But setting the directory this way is simply ignored. Is there a better and working way? The alternative I had in mind is creating a test task for each test class, but that gets ugly as the list with test classes grows. Thanks!

If you truly need a different working directory per test class, creating a separate test task per test class is the only way. (You can create tasks in a loop.)

Actually, ‘test { forkEvery = 1; workingDir = { computeWorkingDir() } }’ might work as well, except that there is no easy way to tell what the “current” test class is when computing the working dir.

Here’s how you (could) do it:

task systemTest(type: Test) {
    def packageName = 'somepackage/name/'
    ext.suiteClasses = [
        "suiteOne",
        "suiteTwo",
    ].each { suite ->
    tasks.create ( name: "$suite}", type: Test) {
         forkEvery = 1
         def myWorkingDir = 'build/run/' + suite
         mkdir myWorkingDir
         workingDir myWorkingDir
         include packageName + suite + '.class'
    }
    ext.suiteClasses.each { suite ->
        tasks[suite].execute()
    }
}

Problem with this solution is that no matter what build I start, these test suites are executed for any gradle task. This makes it unsuitable for the task at hand.